Hi @luggie ,
Have you try using selections
attribute in update_layout
method to get interaction when a point or some points are selected ?
You can set both box select or single point select using this attribute.
Here is an example:
from dash import Dash, dcc, html, Input, Output, callback
import plotly.express as px
import json
import pandas as pd
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = Dash(__name__, external_stylesheets=external_stylesheets)
styles = {
'pre': {
'border': 'thin lightgrey solid',
'overflowX': 'scroll'
}
}
df = px.data.stocks()
fig = px.scatter(df, x='date', y="GOOG")
fig.update_traces(marker_size=12)
fig.update_layout(clickmode='event+select')
app.layout = html.Div([
dcc.Graph(
id='basic-plot',
figure=fig
),
html.Button("Click To Box Select",id='click-button1'),
html.Button("Click Single Point",id='click-button2'),
html.Div(className='row', children=[
html.Div([
dcc.Markdown("""
**Selection Data**
"""),
html.Pre(id='selected-data', style=styles['pre']),
], className='three columns'),
])
])
@callback(
Output('basic-plot', 'figure',allow_duplicate=True),
Input('click-button1', 'n_clicks'),prevent_initial_call='initial_duplicate'
)
def programatically_select_data(n_clicks=0):
if n_clicks:
fig.update_layout(selections = [
dict(
x0 = "2018-07-24 10:51:24.0417",
x1 = "2018-09-20 00:42:21.74",
y0 = 1.01237753083344,
y1= 1.1746352235176878,
)])
return fig
@callback(
Output('basic-plot', 'figure',allow_duplicate=True),
Input('click-button2', 'n_clicks'),prevent_initial_call='initial_duplicate'
)
def programatically_click_data(n_clicks=0):
if n_clicks:
fig.update_layout(selections = [
dict(
x0 = "2018-03-05",
x1 = "2018-03-05",
y0 = 1.0524482730908842,
y1= 1.0524482730908842,
)])
return fig
@callback(
Output('selected-data', 'children',allow_duplicate=True),
Input('basic-plot', 'selectedData'),prevent_initial_call='initial_duplicate')
def display_selected_data(selectedData):
return json.dumps(selectedData, indent=2)
@callback(
Output('selected-data', 'children',allow_duplicate=True),
Input('basic-plot', 'clickData'),prevent_initial_call='initial_duplicate')
def display_clicked_data(selectedData):
return json.dumps(selectedData, indent=2)
if __name__ == '__main__':
app.run(debug=True)