def fig_contexts_use(df, months, level, **kwargs): col_count = 3 row_count = math.ceil(len(month_range(months)) / col_count) figure = make_subplots( row_count, col_count, specs=[[{ 'type': 'domain' } for c in range(col_count)] for r in range(row_count)], subplot_titles=[MONTH_NAMES[m - 1] for m in month_range(months)]) def take_month(months): for m in month_range(months): yield trace_context_use(df[df.index.month == m], level, name=MONTH_NAMES[m - 1]) pie_factory = take_month(months) try: for r in range(row_count): for c in range(col_count): figure.add_trace(next(pie_factory), r + 1, c + 1) except StopIteration as stop: pass return figure
def test_month_range(self): d = datetime(2010, 4, 16, 23, 3) start, end = utils.month_range(d) self.assertEqual(datetime(2010, 4, 1), start) self.assertEqual(datetime(2010, 4, 30, 23, 59, 59, 999999), end) d = datetime(2012, 12, 27, 0, 0) start, end = utils.month_range(d) self.assertEqual(datetime(2012, 12, 1), start) self.assertEqual(datetime(2012, 12, 31, 23, 59, 59, 999999), end)
def fig_total_capacity_2(df, month_caps, months): machine_list = df['Tipo Máquina'].unique() months = month_range(months) month_names = [MONTH_NAMES[m - 1] for m in months] figure = go.Figure() for machine in machine_list: texts = [] caps = [] for month in months: total_cap = cap_per_machine_per_month(month_caps, machine, month) hours = total_cap // 60 used_cap = df[df.index.month == month].groupby( 'Tipo Máquina')['Tiempo de uso en minutos'].sum().divide( total_cap).multiply(100).round(2).get(machine, 0) caps.append(used_cap) texts.append( f'{used_cap}% utilizado de una capacidad total de {hours} horas.' ) figure.add_trace( go.Bar(x=month_names, y=caps, name=machine, hovertext=texts)) figure.update_layout(barmode='group', yaxis=dict(type='linear', ticksuffix='%', title='Capacidad Utilizada')) return figure
def fig_records(df, months=None, stacked=False): machine_list = df['Tipo Máquina'].unique() months = month_range(months) def create_frame(df, serie_name): count = df['Tipo Máquina'].value_counts() frame = pd.DataFrame({'Tipo de Máquina': machine_list}) frame[serie_name] = [count.get(machine, 0) for machine in machine_list] return frame extras = {'barmode': 'relative' if stacked else 'group'} figure = go.Figure() for m in months: name = MONTH_NAMES[m - 1] frame = create_frame(df[df.index.month == m], name) figure.add_trace( go.Bar(x=frame['Tipo de Máquina'], y=frame[name], name=name, hoverinfo='name+y')) if stacked and months: frame = create_frame(df[df.index.month.isin(months)], 'Total') figure.add_trace( go.Scatter(x=frame['Tipo de Máquina'], y=frame['Total'], text=frame['Total'], textposition='top center', mode='text', showlegend=False, hoverinfo='skip')) figure.update_layout(yaxis={'title': 'Número de registros'}, **extras) return figure
def total_ranges(): return ( ("prev_year", utils.prev_year_range()), ("this_year", utils.year_range()), ("prev_month", utils.prev_month_range()), ("this_month", utils.month_range()), ("prev_week", utils.prev_week_range()), ("this_week", utils.week_range()), ("this_day", utils.day_range()), ("prev_day", utils.prev_day_range()))
def fig_hours(df, months=None, stacked=False): machine_list = df['Tipo Máquina'].unique() months = month_range(months) def create_frame(df, serie_name): count = df.groupby('Tipo Máquina').sum( )['Tiempo de uso en minutos'].divide(60).round(0) frame = pd.DataFrame({'Tipo de Máquina': machine_list}) frame[serie_name] = [count.get(machine, 0) for machine in machine_list] return frame if months and type(months) == list: df = df[df.index.month.isin(months)] frame = create_frame(df, 'Total') figure = go.Figure() extras = {'barmode': 'relative' if stacked else 'group'} for m in months: name = MONTH_NAMES[m - 1] frame = create_frame(df[df.index.month == m], name) figure.add_trace( go.Bar(y=frame['Tipo de Máquina'], x=frame[name], name=name, hoverinfo='name+x', orientation='h')) if stacked and months: frame = create_frame(df[df.index.month.isin(months)], 'Total') figure.add_trace( go.Scatter(y=frame['Tipo de Máquina'], x=frame['Total'], text=frame['Total'], textposition='middle right', mode='text', showlegend=False, hoverinfo='skip')) figure.update_layout( xaxis={'title': f'Horas de uso {"total" if stacked else ""}'}, **extras) return figure
def take_month(months): for m in month_range(months): yield trace_context_use(df[df.index.month == m], level, name=MONTH_NAMES[m - 1])