python plotly dash treemap 获取选定的子标签

python plotly dash treemap get selected child label

我正在尝试在 dash 中使用 plotly treemap。当用户通过单击选择树状图中的子组时,树状图会放大所选部分。有没有办法让我获得用户的选择并将其用作 Dash 回调的输入?

例如,这里是 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

我想做的事情看起来像这样:

@app.callback(
    dash.dependencies.Output('some_other_figure', 'figure'),
    [
     dash.dependencies.Input('treemap_child_selection', 'value'),
     ]
)
def update_other_figure(treemap_selection):
    pass

您可以在回调中使用 dcc.GraphclickData 属性

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...

在您的示例中单击 Noam 时,click_data 的值为

{'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'}]}

您可以从中检索 label 的值并用它做一些事情。