Context:
I have a dashboard where i visualise sensor data over time. Because it is a prototype I use data from an excel to imitate live data. I also added a slider, so i can visualise the data from different points of time.
Problem:
The solution I am using right now is the extenddata callback for every or every few points. Whenever I slide for another point in the future or past, I try to reset the figure with another figure callback. When I do not slide, the application will also slow down with extenddata after some time.
What I understood from reading a few posts is that plotly keeps the old figure in itβs memory. So I am asking how I could fix this, or if there any other methods I could use?
@callback(
Output({"component_name": "sensor_data_graph", "vessel_id": MATCH}, "extendData"),
Input({'component_name': 'slider_timeline_position', 'vessel_id': MATCH}, 'value'),
prevent_initial_call=True
)
def update_live_graphs(slider_value):
if slider_value % DATA_STEP != 0:
return no_update
vessel_id = get_vessel_id_from_ctx()
if vessel_id is None:
return no_update
df_sensor, slopes, residual, _ = vessel_live_data[vessel_id]
if slider_settings["slid"]:
start = slider_value - LIVE_PLOT_LENGTH
if start < 0:
start = 0
else:
slider_settings["slid"] = False
start = max(slider_value - DATA_STEP, 0)
df_new = df_sensor.iloc[start:slider_value]
if df_new.empty:
return no_update
data_array = df_new.to_numpy()
time_idx = df_sensor.columns.get_loc("time")
extend_x = [data_array[:, time_idx].tolist()] * (data_array.shape[1] - 1)
extend_y = [data_array[:, i].tolist() for i in range(data_array.shape[1]) if i != time_idx]
trace_indices = list(range(len(extend_x)))
return {"x": extend_x, "y": extend_y},trace_indices, LIVE_PLOT_LENGTH
@callback(
Output({"component_name": "sensor_data_graph", "vessel_id": MATCH}, "figure"),
Input({'component_name': 'slider_timeline_position', 'vessel_id': MATCH}, 'value'),
prevent_initial_call=True
)
def reset_graph(slider_value):
vessel_id = get_vessel_id_from_ctx()
if vessel_id is None:
return no_update
df_sensor, slopes, residual, _ = vessel_live_data[vessel_id]
if not slider_settings["slid"]:
return no_update
start = slider_value - LIVE_PLOT_LENGTH
if start < 0:
start = 0
df = df_sensor[start:]
fig = make_sensor_fig(df, title=f"Sensor Data: vessel {vessel_id}")
return fig