I have a scatter plot with a legend that allows you to toggle traces on/off. I’m trying to implement a callback that updates a DashTable
based on the traces/items that are toggled on in the scatter plot. I essentially want to highlight or style the corresponding rows in a table based on the items that are toggled in the plot.
Based on my understanding, using the restyleData
properties in dcc.Graph
is the way to go as it is supposed to give you the latest restyle event such as:
restyleData
( list ; optional): Data from latest restyle event which occurs when the user toggles a legend item, changes parcoords selections, or other trace-level edits. Has the form[edits, indices]
, whereedits
is an object{<attr string>: <value>}
describing the changes made, andindices
is an array of trace indices that were edited. Read-only.
However, the problem is that each time the callback is triggered, it only returns the most latest toggled event although the documentation seems to suggest that the property could return multiple indices for all the traces that were edited. Is there any workaround or way to capture / retain all items that are toggled on vs toggled off at a given time to use in a callback?
Here’s a sample snippet of the code I currently have that only updates the most recently toggled item:
@app.callback(Output('topic-table', 'style_data_conditional'),
[Input('bubble-plot', 'restyleData')])
def update_topics_highlighted(toggled_topics):
if toggled_topics[0]['visible'] == [True]:
temp_topic_num = str(toggled_topics[1][0])
style_data_conditional = [{
'if': {'filter_query': '{Topic#} = ' + temp_topic_num},
'backgroundColor': 'red'
}]
else: # item is toggled off: toggled_topics[0]['visible'] == ['legendonly']:
style_data_conditional = [{}] # toggle off , remove highlighting
return style_data_conditional