Dynamic breakdown fix
This commit is contained in:
@@ -141,14 +141,21 @@ def set_drill_down_value(options):
|
|||||||
return options[0]['value']
|
return options[0]['value']
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Callback to update the breakdown graph
|
# Callback to update the breakdown graph and handle zoom events
|
||||||
@app.callback(
|
@app.callback(
|
||||||
Output('breakdown-graph', 'figure'),
|
Output('breakdown-graph', 'figure'),
|
||||||
[Input('tabs', 'value'),
|
[Input('tabs', 'value'),
|
||||||
Input('drill-down', 'value'),
|
Input('drill-down', 'value'),
|
||||||
Input('time-range', 'value')]
|
Input('time-range', 'value'),
|
||||||
|
Input('breakdown-graph', 'relayoutData')],
|
||||||
|
[dash.dependencies.State('breakdown-graph', 'figure')]
|
||||||
)
|
)
|
||||||
def update_breakdown_graph(tab, drill_down, time_range):
|
def update_breakdown_graph(tab, drill_down, time_range, relayoutData, figure):
|
||||||
|
ctx = callback_context
|
||||||
|
|
||||||
|
if not ctx.triggered:
|
||||||
|
return dash.no_update
|
||||||
|
|
||||||
conn = get_db_connection()
|
conn = get_db_connection()
|
||||||
if tab == 'room-breakdown':
|
if tab == 'room-breakdown':
|
||||||
query = f"""
|
query = f"""
|
||||||
@@ -165,11 +172,25 @@ def update_breakdown_graph(tab, drill_down, time_range):
|
|||||||
df = pd.read_sql_query(query, conn)
|
df = pd.read_sql_query(query, conn)
|
||||||
conn.close()
|
conn.close()
|
||||||
df = calculate_breakdown_kwh(df)
|
df = calculate_breakdown_kwh(df)
|
||||||
|
|
||||||
|
# Initialize fig
|
||||||
fig = px.line(df, x=df.index, y=['current', 'power', 'kWh'],
|
fig = px.line(df, x=df.index, y=['current', 'power', 'kWh'],
|
||||||
labels={'value': 'Value', 'variable': 'Metric'},
|
labels={'value': 'Value', 'variable': 'Metric'},
|
||||||
title=f'{tab.replace("-", " ").title()} Metrics')
|
title=f'{tab.replace("-", " ").title()} Metrics')
|
||||||
fig.update_layout(legend_title_text='Metrics', yaxis_type="log") # Use logarithmic scale
|
fig.update_layout(legend_title_text='Metrics', yaxis_type="log") # Use logarithmic scale
|
||||||
|
|
||||||
|
if 'time-range' in ctx.triggered[0]['prop_id'] or 'drill-down' in ctx.triggered[0]['prop_id']:
|
||||||
|
pass # fig is already initialized
|
||||||
|
|
||||||
|
elif relayoutData and 'xaxis.range[0]' in relayoutData and 'xaxis.range[1]' in relayoutData:
|
||||||
|
start = pd.to_datetime(relayoutData['xaxis.range[0]'])
|
||||||
|
end = pd.to_datetime(relayoutData['xaxis.range[1]'])
|
||||||
|
df = df.loc[start:end]
|
||||||
|
fig = px.line(df, x=df.index, y=['current', 'power', 'kWh'],
|
||||||
|
labels={'value': 'Value', 'variable': 'Metric'},
|
||||||
|
title=f'{tab.replace("-", " ").title()} Metrics')
|
||||||
|
fig.update_layout(legend_title_text='Metrics', yaxis_type="log") # Use logarithmic scale
|
||||||
|
|
||||||
# Update legend to show total values
|
# Update legend to show total values
|
||||||
for trace in fig.data:
|
for trace in fig.data:
|
||||||
if trace.name == 'kWh':
|
if trace.name == 'kWh':
|
||||||
|
|||||||
Reference in New Issue
Block a user