[英]python plotly dash treemap get selected child label
I'm trying to use a plotly treemap within dash.我正在尝试在破折号内使用 plotly 树形图。 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?有没有办法让我获得用户的选择并将其用作 Dash 回调的输入?
For example, here is code for a treemap in 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
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您可以在回调中使用dcc.Graph
的clickData
属性
clickData (dict; optional): Data from latest click event. clickData (dict; optional): 来自最新点击事件的数据。 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在您的示例中单击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'}]}
You could retrieve the value of label
from this and do something with it.您可以从中检索label
的值并对其进行处理。
For me it take some time to understund the dict structure.对我来说,理解字典结构需要一些时间。 I put here syntax to get correct value from click_data我将语法放在这里以从 click_data 获取正确的值
click_data["points"][0]["label"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.