Legend visible after click event

Hi everyone! I’m currently tyring to rich visibility of legen in “plotly_legendclick” event. My code for now looking like this:

mainChart.on(
    "plotly_legendclick",
    function (eventData: Plotly.LegendClickEvent) {
      if (eventData.curveNumber || eventData.curveNumber === 0) {
        const trace: any = eventData.data[eventData.curveNumber];
        console.log("curveNumber", trace, typeof trace, trace.visible);
      }

      return true;
    }
  );

and for now i see in dev tools that visible is “legendonly”, but i couldnot get this value in code, this not supported? or maybe there another way to understand that i hide one of trace and do some stuff with this information. Thanx guys!

Hey KostiaStoliarskyi,

when you click on the legend and try to get the visibility of a trace you just clicked, you will NOT get the latest state of this trace.

the trace visibility is updated AFTER the plotly_legendclick event.

for example:

  1. chart with 2 trace: trace A, trace B. both are visible.
  2. you click on traceA to set it from ON to OFF in the legend.
  3. reading the data for the cureNumber for this trace (curveNumber 0) will result in value of true NOT “legendonly”.
  4. reading the data for the other trace (curveNumber 1) will result in the correct value true (since it’s state was not changed through this event).

if you are familiar with something like alpine.js, think about it as a usecase for nextTick.