I'm trying to use a plotly treemap within dash. When the user selects a subgroup in the treemap by clicking on it, the treemap zooms in on the selected section. Is there a way for me to get the user's selection and use that as an input into a Dash callback?
For example, here is code for a treemap in Dash:
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objects as go
fig = go.Figure(go.Treemap(
labels = ["Eve","Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
parents = ["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"],
root_color="lightgrey"
))
fig.update_layout(margin = dict(t=50, l=25, r=25, b=25))
app = dash.Dash()
app.layout = html.Div([
dcc.Graph(figure=fig)
])
app.run_server(debug=True, use_reloader=False) # Turn off reloader if inside Jupyter
And what I'd like to do would look something like this:
@app.callback(
dash.dependencies.Output('some_other_figure', 'figure'),
[
dash.dependencies.Input('treemap_child_selection', 'value'),
]
)
def update_other_figure(treemap_selection):
pass
You could use the dcc.Graph
's clickData
property in your callback
clickData (dict; optional): Data from latest click event. Read-only.
@app.callback(
dash.dependencies.Output("output", "children"),
dash.dependencies.Input("graph", "clickData"),
)
def update_other_figure(click_data):
print(click_data)
# Do something with the data...
On clicking Noam
in your example the value of click_data
is
{'points': [{'curveNumber': 0, 'pointNumber': 4, 'currentPath': 'Eve/Seth/', 'root': 'Eve', 'entry': 'Eve', 'percentRoot': 0.16666666666666666, 'percentEntry': 0.16666666666666666, 'percentParent': 0.5, 'parent': 'Seth', 'label': 'Noam'}]}
You could retrieve the value of label
from this and do something with it.
For me it take some time to understund the dict structure. I put here syntax to get correct value from click_data
click_data["points"][0]["label"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.