Option to not resample traces that are not visible #325
Replies: 2 comments 1 reply
-
Hi @Olivier0330, We tried to tackle your specific issue via this PR #253. I think that I can maybe look further into it during the winter holidays! |
Beta Was this translation helpful? Give feedback.
-
Hi @jonasvdd, I already looked into the code and tried a few things to get this working. I made the following app with a dcc.Store to store a list of visible trace indices. app.layout = dash.html.Div([
dash.dcc.Graph(id='graph', figure=fig, style={'height': '100%', 'width': '100%'},
config={'scrollZoom': True}),
dash.dcc.Store(id='visible-store', data=[])
],
style={
'height': '95vh',
'width': '100%',
'margin': '0',
'padding': '0'
}
)
@app.callback(
Output('visible-store', 'data'),
Input('visible-store', 'data'),
Input('graph', 'restyleData'),
prevent_initial_call=True
)
def handle_callbacks(current_visible, restyle_data):
"""
Function stores a list off trace ID's which are visible based on changes in restyleData and stores them in
the visible-store. If no traces are visible, return [], else [0, 1, 2]. The ID's will not be ordered
:param current_visible: current list of visible traces.
:param restyle_data: restyle data from plot. Example:
[{'visible': ['legendonly']}, [0]]
[{'visible': ['legendonly', True]}, [0, 1]]
:return: list of visible trace ID's. For example [1, 3, 5, 9, 2]
"""
if restyle_data is None:
return dash.no_update
for i in range(len(restyle_data[0]['visible'])):
trace_id = restyle_data[1][i]
if restyle_data[0]['visible'][i] is True:
if trace_id not in current_visible: # do not add traces twice (occurs during double-click on the legend)
current_visible.append(trace_id)
else:
if trace_id in current_visible: # do not try to remove non-existing traces (occurs during double-click on the legend)
current_visible.remove(trace_id)
print('Visible traces: ', current_visible)
return current_visible This Store will be used as input for the resampler callback: This input is added to 'construct_update_data_patch and to '_construct_update_data': Based on the visible traces, I add the traces that are not visible to 'updated_traces_indices' to make sure they are not updated: Now it will only update visible traces. You can of course also change the visible store to invisible store which stores the traces which are not visible to avoid using the list comprehension on every resampling callback. Let me know what you think. |
Beta Was this translation helpful? Give feedback.
-
I have a plot with a significant number of traces which are by default set to visible='legendonly'. I only show a few traces at the same time, but the resampler still updates the data for all traces which makes the callback slow. Is it possible (to add an option) to not update hidden traces to improve performance?
Beta Was this translation helpful? Give feedback.
All reactions