Question about click event data for legends

Hello everyone. I have a question about https://plot.ly/javascript/click-events/#click-event-data or more precisely about https://github.com/plotly/plotly.js/pull/2581 .

When running graph.on('plotly_legendclick', function(clickData) { console.log(clickData.layout); console.log(clickData.layout.hiddenlabels); } by clicking on a pie chart legend item and thus hiding it from the plot, I do see a layout object in the console. This layout object contains the array hiddenlabels (as an attribute) with the one label legend item that I’ve hidden. But the next console output is undefined. It doesn’t find hiddenlabels as if it doesn’t exist.

Why is this happening and how can I access the hiddenlabels array in this context properly? Do I have to use .then() ?

Ha I see. plotly_legendclick is triggered before hiddenlabels is updated.

You should listen to plotly_relayout instead: https://codepen.io/etpinard/pen/vVmgxe

1 Like

Interesting! Will try this out tomorrow, thanks a bunch!

1 Like

@etienne Yes! I managed to achieve my goal by using plotly_relayout. Sadly I have another problem now, because I need to rearrange “plotly legend group titles”, which are hacky annotations, on different counts of labels now. sigh
Thank you very much for your help!

EDIT: I guess setting the font and value color to transparent is the way to go here to keep the height positioning of everything x)…

Hi @etienne,

I have a similar issue where I am attempting to restyle a trace if only one legend item is selected. While I can test to see if only one trace is selected, attempting to restyle or redraw the trace causes the plotly_relayout event to be called again entering a recursive loop.

Is there any method I can use to avoid this?