I have used plotly to create a sunburst plot. All of this is inside a django_dash_plotly app, which as I understand it, is just a wrapper around a dash app. It works, all good.
I have created a callback so that when a segment of the sunburst plot is clicked, I can get the label of the selected section. Drilling down, this works out of the box. When I select one of the lower segments, the plot changes to center that segment and I can retrieve the value of the label.
import pandas as pd
import plotly.express as px
from django_plotly_dash import DjangoDash
def create_tree_dataframe(tree):
<... code to generate data frame ...>
data = create_tree_dataframe(diseaseTreewithModels)
sunburst = px.sunburst(
data,
names='child',
parents='parent',
values='sampleCount',
maxdepth=3,
hover_data='cancerId',
)
sunburst.update_layout(margin=dict(l=0, r=0, t=0, b=0), autosize=True)
sunburst.update_traces( textfont=dict(family="Arial", size=12), insidetextorientation='radial')
app = DjangoDash('NavTest')
app.layout = html.Div([
html.Div(id='selectedCancer'),
dcc.Graph(id="cancerNav", figure = sunburst),
])
@app.callback(
dash.dependencies.Output('selectedCancer', 'children'),
[dash.dependencies.Input('cancerNav', 'clickData')],
)
def update_output(clickData):
print(clickData)
if clickData is not None:
clicked_point = clickData['points'][0]
return "The selected value is: {}".format(clicked_point['label'])
else:
return "Click on a section of the sunburst plot to see its value."
Once I have selected one of the lower segments however, I canât retrieve the value of the label when I click the center segment to go back up. ie. I start here:
I select Sarcoma and as expected I get this with the selected value in the top left being correctly reported as Sarcoma:
and the clickData looks like this:
{'points': [{'curveNumber': 0, 'pointNumber': 139, 'currentPath': 'ZERO2/', 'root': 'ZERO2', 'entry': 'ZERO2', 'percentRoot': 0.22387223311852059, 'percentEntry': 0.22387223311852059, 'percentParent': 0.22387223311852059, 'parent': 'ZERO2', 'label': 'Sarcoma', 'value': 242, 'customdata': [94]}]}
I then hit the middle section to return to the top level and I get this with the selected value in the top left still being reported as Sarcoma, rather than ZERO2:
and the clickData looks like this:
{'points': [{'curveNumber': 0, 'pointNumber': 139, 'currentPath': 'ZERO2/', 'root': 'ZERO2', 'entry': 'Sarcoma', 'percentRoot': 0.22387223311852059, 'percentEntry': 1, 'percentParent': 0.22387223311852059, 'parent': 'ZERO2', 'label': 'Sarcoma', 'value': 242, 'customdata': [94]}]}
When drilling down, the value reported is the value after the change, why is this not the case when drilling up?
I canât for the life of me figure this out. How do I change my call back so then when hitting the middle section to drill up, the correct reported value is selected? Sadly itâs not a matter of checking to see if one is at the root of the tree or not, as the same thing happens if I go from a level 3 node to a level 2 node - the level 3 node remains in the center, so I canât drill up.
Thanks
Ben.