Dynamic legend n yaxis log

This commit is contained in:
Radek
2025-05-22 12:35:41 +01:00
parent 6990e86859
commit 5bd170d710

View File

@@ -1,5 +1,5 @@
import dash import dash
from dash import dcc, html, Input, Output from dash import dcc, html, Input, Output, callback_context
import plotly.express as px import plotly.express as px
import pandas as pd import pandas as pd
import sqlite3 import sqlite3
@@ -73,20 +73,41 @@ app.layout = html.Div([
]) ])
]) ])
# Callback to update the building total graph # Combined callback to update the building total graph and handle zoom events
@app.callback( @app.callback(
Output('building-graph', 'figure'), Output('building-graph', 'figure'),
[Input('time-range', 'value')] [Input('time-range', 'value'),
Input('building-graph', 'relayoutData')],
[dash.dependencies.State('building-graph', 'figure')]
) )
def update_building_graph(time_range): def update_building_graph(time_range, relayoutData, figure):
ctx = callback_context
if not ctx.triggered:
return dash.no_update
df = fetch_data(time_range) df = fetch_data(time_range)
df = calculate_building_kwh(df) df = calculate_building_kwh(df)
# Initialize fig
fig = px.line(df, x=df.index, y=['total_current', 'total_power', 'kWh'], fig = px.line(df, x=df.index, y=['total_current', 'total_power', 'kWh'],
labels={'value': 'Value', 'variable': 'Metric'}, labels={'value': 'Value', 'variable': 'Metric'},
title='Building Total Metrics') title='Building Total Metrics')
fig.update_layout(legend_title_text='Metrics') fig.update_layout(legend_title_text='Metrics', yaxis_type="log") # Use logarithmic scale
# Update legend to show recent values if 'time-range' 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=['total_current', 'total_power', 'kWh'],
labels={'value': 'Value', 'variable': 'Metric'},
title='Building Total Metrics')
fig.update_layout(legend_title_text='Metrics', yaxis_type="log") # Use logarithmic scale
# Update legend to show total values
for trace in fig.data: for trace in fig.data:
if trace.name == 'kWh': if trace.name == 'kWh':
trace.name = f"{trace.name}: {df['kWh'].sum():.2f}" trace.name = f"{trace.name}: {df['kWh'].sum():.2f}"
@@ -147,9 +168,9 @@ def update_breakdown_graph(tab, drill_down, time_range):
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') fig.update_layout(legend_title_text='Metrics', yaxis_type="log") # Use logarithmic scale
# Update legend to show recent 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':
trace.name = f"{trace.name}: {df['kWh'].sum():.2f}" trace.name = f"{trace.name}: {df['kWh'].sum():.2f}"