Hello,
I have a scatterplot that I’m using as a way for my users to select weather stations to view more in-depth communication data while also showing the current status of the stations. So far I’ve been able to fix most of the issues, but one that has gotten me really stuck is dealing with the little ‘x’ in the dropdown. It correctly activates the callback that contains logic to restore the selectedpoints to all the points in the scatterplot. However after that is completed and the graph is restored if I shift + click a different square it returns me to my previous selection which shouldn’t exist because I just replaced it. I checked the variables and the selectedpoints values and the dropdown values were all successfully changed.
here is a gif of the problem:
Here is my relevant code:
@app.callback(
Output('station-dd', 'value'),
[Input('all-graph', 'selectedData')],
[State('station-dd', 'value')])
def add_to_dd(sel_from_graph, val_in_dd):
if sel_from_graph is None:
return None
else:
selected_list = []
for x in sel_from_graph['points']:
idx = x['customdata'][6]
selected_list.append(idx)
if val_in_dd:
if len(selected_list) == 1:
result_list = selected_list
else:
result_list = list(set(selected_list) | set(val_in_dd))
result_list = [x for x in station_df.index] if result_list is None else result_list
else:
result_list = selected_list
return result_list
@app.callback(
Output('all-graph', 'figure'),
Input('station-dd', 'value'),
State('all-graph', 'figure'))
def callback(new_selection, figure):
if figure is None or new_selection is None or len(new_selection) == 0:
selection_points = [x for x in station_df.index]
if figure is not None:
figure['data'][0]['selectedpoints'] = selection_points
return make_all_graph(selection_points)
elif figure['data'][0]['selectedpoints'] == new_selection:
return figure
else:
selection_points = list(set(new_selection) | set(figure['data'][0]['selectedpoints']))
figure['data'][0]['selectedpoints'] = selection_points
return figure
def make_all_graph(selection=None):
data = go.Scatter(x=station_df['x'],
y=station_df['y'],
customdata=station_df['customdata'],
mode='markers',
marker=dict(
size=14),
selectedpoints=selection,
marker_symbol='square',
text=station_df['customdata'],
hovertemplate='ID: %{text[0]}<br>' + 'Name: %{text[1]}<br>' +
'Circuit: %{text[''2]}<br>' + 'Last Comm.: %{text[3]}<br>' +
'Interval: %{text[4]}<br>' + 'Always Powered: %{text[5]}<br>' +
'index: %{text[6]}<br>' + '<extra></extra>',
)
layout = go.Layout(font=dict(family='Arial',
size=14,
color='dimgray'),
title=dict(text='Southern California Edison Satellite Stations',
font=dict(family='Arial Narrow',
size=20,
color='dimgray')),
xaxis=dict(tickmode='array',
tickvals=[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50,
55, 60, 65, 70, 75, 80, 85, 90, 95, 99],
zeroline=False),
yaxis=dict(tickmode='array',
tickvals=[20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
ticktext=['2001 - 2099', '2100 - 2199', '2200 - 2299',
'2300 - 2399', '2400 - 2499', '2500 - 2599',
'2600 - 2699', '2700 - 2799', '2800 - 2899',
'2900 - 2999', '3000 - 3099']),
legend=dict(orientation='h',
yanchor='top',
y=-0.1,
xanchor='right',
x=1),
hovermode='closest',
clickmode='event+select',
paper_bgcolor='#fff',
plot_bgcolor='#fff',
showlegend=True,
dragmode=False)
fig = go.Figure(data=data, layout=layout)
return fig