Question about click event data for legends


#1

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() ?


#2

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

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


#3

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


#4

@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)…