import pandas as pd
import dash
from dash import dcc, html, Input, Output
data2 = init_data(2)
graph2 = create_graphs(2,data2)
app = dash.Dash(__name__)
app.layout = html.Div([
# ...
dcc.Graph(id='live-graph2'),
])
data_updates_enabled = False
@app.callback(
Output('live-graph2', 'figure'),
Input('interval-component', 'n_intervals'),
Input('data_matrix2', 'data'),
Input('start-button', 'n_clicks'),
Input('stop-button', 'n_clicks'),
prevent_initial_call=True
)
def update_graph2(n_intervals, data2, start_btn_clicks, stop_btn_clicks):
global data_updates_enabled
global graph2
if start_btn_clicks > 0:
data_updates_enabled = True
if stop_btn_clicks > 0:
data_updates_enabled = False
if data_updates_enabled:
Comments2 = update_comments(2)
mnemonic2, color, echelle, min_values, max_values = graph_setting_data(2)
data2 = pd.DataFrame(data2)
for i, col in enumerate(mnemonic2):
graph2.update_traces(x=data2[col].values, y=data2['TIME'].values, selector=dict(name=col))
graph2.update_traces(y=Comments2['TIME'].values, text=Comments2['user_comment_2'].values,
selector=dict(name="user_comment_2"))
return graph2
Hi i have this dash application that initilize the data and the graph before the app and when start is clicked it starts to update the traces wich is what I’m looking for, but when i change the mnemonic list it isnt took in consideration in the update. how can I update the graph withouth creating a new figure for each update… i need to re update the data present the color of the lines the xaxes and the ranges.
here is also how it create the graph :
def create_graphs(n, data):
mnemonic, colors, echelle, min_values, max_values = graph_setting_data(n)
nombre_courbes = 6
mnemonic += [''] * (nombre_courbes - len(mnemonic))
data.reset_index(inplace=True)
fig = go.Figure()
# ==== ajout des commentaires==
x_text_lenght = np.full(len(data['TIME'].values), 0)
fig.add_trace(
go.Scatter(x=x_text_lenght,
y=data['TIME'].values,
mode='lines+text',
name= f'user_comment_{n}',
line=dict(color="white"),
fillcolor="white",
text=data[f'user_comment_{n}'].values,
textposition="top right",
textfont=dict(color='red')
))
fig.update_layout(xaxis=dict(gridcolor="#C5C5C5"),yaxis=dict(gridcolor="#C5C5C5"))
# ==== ajout des courbes ====
if mnemonic[0] in data.columns:
fig.add_trace(
go.Scattergl(x=data[mnemonic[0]].values,
y=data['TIME'].values,
mode='lines',
name=mnemonic[0],
xaxis='x1',
line=dict(color=colors[0]),
fillcolor=colors[0]
))
fig.update_layout(
xaxis1=dict(
nticks=10,
linewidth=3,
linecolor=colors[0],
title=mnemonic[0],
side="top",
position=0,
title_standoff=0.01,
range=[min_values[0], max_values[0]],
titlefont={"family": "Montserrat",
"color": colors[0], "size": 12}, ),
yaxis=dict( nticks=10, autorange="reversed",
domain=[6 * 0.025, 1], ), plot_bgcolor="rgba(0,0,0,0)"
)
else:
fig.add_trace(
go.Scattergl(x=[],
y=[],
mode='none',
name="Not Attributed",
))
fig.update_layout(
xaxis=dict(
nticks=10,
linewidth=3,
linecolor="black",
title="Not Attributed",
side="top",
position=0,
titlefont={"family": "Montserrat", "color": "black", "size": 12},
),
yaxis=dict(gridcolor='#C5C5C5', nticks=10,
autorange="reversed", domain=[6 * 0.025, 1]), plot_bgcolor="rgba(0,0,0,0)"
)
if mnemonic[1] in data.columns:
fig.add_trace(
go.Scattergl(
x=data[mnemonic[1]].values,
y=data['TIME'].values,
mode='lines',
name=mnemonic[1],
xaxis='x2',
textposition="top right",
line=dict(color=colors[1]),
fillcolor=colors[1]))
fig.update_layout(
xaxis2=dict(
overlaying='x',
nticks=10,
linewidth=3,
linecolor=colors[1],
title=mnemonic[1],
title_standoff=0.1,
side="top",
position=0.02,
showgrid=False,
range=[min_values[1], max_values[1]],
titlefont={"family": "Montserrat", "color": colors[1],
"size": 12}, ),
yaxis=dict( nticks=10, autorange="reversed",
domain=[6 * 0.025, 1], ), plot_bgcolor="rgba(0,0,0,0)"
)
else:
# Ajouter une courbe à zéro pour les valeurs manquantes
fig.add_trace(
go.Scattergl(x=[],
y=[],
mode='none',
name="Not Attributed",
xaxis='x2',
textposition="top right",
))
fig.update_layout(
xaxis2=dict(
overlaying='x',
nticks=10,
linewidth=3,
linecolor="black",
title="Not Attributed",
title_standoff=0.01,
side="top",
position=0.02,
showgrid=False,
titlefont={"family": "Montserrat", "color": "black", "size": 12}, ),
yaxis=dict(gridcolor='#C5C5C5', nticks=10, autorange="reversed",
domain=[6 * 0.025, 1], ), plot_bgcolor="rgba(0,0,0,0)"
)
if mnemonic[2] in data.columns:
fig.add_trace(
go.Scattergl(
x=data[mnemonic[2]].values,
y=data['TIME'].values,
mode='lines',
name=mnemonic[2],
xaxis='x3',
textposition='top right',
line=dict(color=colors[2]),
fillcolor=colors[2]))
fig.update_layout(
xaxis3=dict(
overlaying='x',
nticks=10,
linewidth=3,
linecolor=colors[2],
title=mnemonic[2],
title_standoff=0.01,
side="top",
position=0.04,
showgrid=False,
range=[min_values[2], max_values[2]],
titlefont={"family": "Montserrat", "color": colors[2], "size": 12}),
yaxis=dict( nticks=10, autorange="reversed",
domain=[6 * 0.025, 1], ), plot_bgcolor="rgba(0,0,0,0)"
)
else:
fig.add_trace(
go.Scattergl(
x=[],
y=[],
mode='none',
name="Not Attributed",
xaxis='x3',
textposition='top right',
))
fig.update_layout(
xaxis3=dict(
overlaying='x',
nticks=10,
linewidth=3,
linecolor="black",
title="Not Attributed",
title_standoff=0.1,
side="top",
position=0.04,
showgrid=False,
# range=[min_dict.get(MATRIX.columns[i]), max_dict.get(MATRIX.columns[i])],
titlefont={"family": "Montserrat", "color": "black", "size": 12}),
yaxis=dict( nticks=10, autorange="reversed",
domain=[6 * 0.025, 1], ), plot_bgcolor="rgba(0,0,0,0)"
)
if mnemonic[3] in data.columns:
fig.add_trace(
go.Scattergl(
x=data[mnemonic[3]].values,
y=data['TIME'].values,
mode='lines',
name=mnemonic[3],
xaxis='x4',
textposition="top right",
line=dict(color=colors[3]),
fillcolor=colors[3]))
fig.update_layout(
xaxis4=dict(
overlaying='x',
nticks=10,
linewidth=3,
linecolor=colors[3],
title=mnemonic[3],
title_standoff=0.1,
side="top",
position=0.06,
showgrid=False,
range=[min_values[3], max_values[3]],
titlefont={"family": "Montserrat", "color": colors[3], "size": 12},
),
yaxis=dict(nticks=10, autorange="reversed",
domain=[6 * 0.025, 1], ), plot_bgcolor="rgba(0,0,0,0)"
)
else:
fig.add_trace(
go.Scattergl(
x=[],
y=[],
mode='none',
name="Not Attributed",
xaxis='x4',
textposition="top right"))
fig.update_layout(
xaxis4=dict(
overlaying='x',
nticks=10,
linewidth=3,
linecolor="black",
title="Not Attributed",
title_standoff=0.1,
side="top",
position=0.06,
showgrid=False,
# range=[min_dict.get(MATRIX.columns[i]), max_dict.get(MATRIX.columns[i])],
titlefont={"family": "Montserrat", "color": "black", "size": 12}),
yaxis=dict(nticks=10, autorange="reversed",
domain=[6 * 0.025, 1], ), plot_bgcolor="rgba(0,0,0,0)"
)
if mnemonic[4] in data.columns:
fig.add_trace(
go.Scattergl(
x=data[mnemonic[4]].values,
y=data['TIME'].values,
mode='lines',
name=mnemonic[4],
xaxis='x5',
textposition="top right",
line=dict(color=colors[4]),
fillcolor=colors[4]))
fig.update_layout(
xaxis5=dict(
overlaying='x',
nticks=10,
linewidth=3,
linecolor=colors[4],
title=mnemonic[4],
title_standoff=0.1,
position=0.08,
side="top",
showgrid=False,
range=[min_values[4], max_values[4]],
titlefont={"family": "Montserrat", "color": colors[4], "size": 12}, ),
yaxis=dict( nticks=10, autorange="reversed",
domain=[6 * 0.025, 1], ), plot_bgcolor="rgba(0,0,0,0)"
)
else:
fig.add_trace(
go.Scattergl(
x=[],
y=[],
mode='none',
name="Not Attributed",
xaxis='x5',
textposition="top right",
))
fig.update_layout(
xaxis5=dict(
overlaying='x',
nticks=10,
linewidth=3,
linecolor="black",
title="Not Attributed",
title_standoff=0.1,
side="top",
position=0.08,
showgrid=False,
titlefont={"family": "Montserrat", "color": "black", "size": 12},
),
yaxis=dict( nticks=10, autorange="reversed",
domain=[6 * 0.025, 1], ), plot_bgcolor="rgba(0,0,0,0)"
)
if mnemonic[5] in data.columns:
fig.add_trace(
go.Scattergl(
x=data[mnemonic[5]].values,
y=data['TIME'].values,
mode='lines',
name=mnemonic[5],
xaxis='x6',
line=dict(color=colors[5]),
fillcolor=colors[5]))
fig.update_layout(
xaxis6=dict(
overlaying='x',
nticks=10,
linewidth=3,
linecolor=colors[5],
title=mnemonic[5],
title_standoff=0.1,
side="top",
position=0.1,
showgrid=False,
range=[min_values[5], max_values[5]],
titlefont={"family": "Montserrat", "color": colors[5], "size": 12},
),
yaxis=dict(nticks=10, autorange="reversed",
domain=[6 * 0.025, 1], ), plot_bgcolor="rgba(0,0,0,0)"
)
else:
fig.add_trace(
go.Scattergl(
x=[],
y=[],
mode='none',
name="Not Attributed",
xaxis='x6',
textposition="top right"))
fig.update_layout(
xaxis6=dict(
overlaying='x',
nticks=10,
linewidth=3,
linecolor="black",
title="Not Attributed",
title_standoff=0.1,
side="top",
position=0.1,
showgrid=False,
titlefont={"family": "Montserrat", "color": "black", "size": 12}, ),
yaxis=dict( nticks=10, autorange="reversed",
domain=[6 * 0.025, 1], ), plot_bgcolor="rgba(0,0,0,0)"
)
fig.update_layout(margin={"l": 2, "r": 2, "t": 5, "b": 10},
height=2000, showlegend=False, uirevision=True, plot_bgcolor="rgba(0,0,0,0)",
paper_bgcolor="rgba(0,0,0,0)")
# Affichage du graphique
return fig
I also use json file to store and read the settings wich has all the settings of a graph [names of columns, colors, range,]