Hi all,
I am relatively new to Dash and have a question on how to get the ‘color’ information, via a callback, where a user is hovering over a (stacked) bar chart.
I need the ‘color’ value that the user is hovering over, in order to trigger another action, using a callback function of course.
I created a simplified example version to illustrate the issue.
Here’s the layout and callback code:
app.layout = html.Div([
# main bar plot (stacked)
dcc.Graph(
id='bar_chart',
figure=fig
),
# display the info for the plot element the user is hovering over
html.Div([
html.Pre(id='hover_data', style={'paddingTop':35})
], style={'width':'30%'}
),
])
@app.callback(
Output('hover_data', 'children'),
Input('bar_chart', 'hoverData'),
)
def show_user_hover_data(hoverData):
return json.dumps(hoverData, indent=2)
Also, here is my code for the figure:
fig = px.bar(df, x="day", y="count", color="fruit",
orientation='v', color_discrete_sequence=coloring)
When I hover over, lets’ say x axis = “Tue” and y = 1.5 … this is the hoverData shown from the callback:
{
"points": [
{
"curveNumber": 0,
"pointNumber": 5,
"pointIndex": 5,
"x": "Tue",
"y": 1,
"label": "Tue",
"value": 1,
"bbox": {
"x0": 356.62,
"x1": 551.98,
"y0": 260.2,
"y1": 260.2
}
}
]
}
So, it is easy for me to extract the x and y information. That’s no problem.
However - I also need to know the ‘color’ information on the chart - ie, the “fruit” value.
When I hover over that cell w/ my mouse – it shows me:
fruit=apple
day=Tue
count=1
All of that is correct. The problem is - hoverData does not contain the value of “fruit” (in this case, it is “apple”).
I need to get that information in a callback.
How can I do that?
thanks in advance,
Craig
ps - attached is a snapshot of the dash page running and the info shown when I hover over that element
(and after that, a snippet portion of the dataframe / df I used)