Plotly_selected is not triggered on selection changes because of legend events

Hello!

I am trying to perform lasso selection and want to show the number of points selected in the UI.

I can get the details of the points from plotly_selected event. But when I click on any legends that have associated points in the lasso region, then obviously the selection changes, but plotly_selected event is not triggered.

I have replicated this behavior in the codepen.

Should I need to handle this behavior differently to get the selection details? Please share your thoughts on this.

Add this

graphDiv.on('plotly_restyle', function(eventData) {
  let data = graphDiv._fullData
  let points = 0
  
  data.forEach((d, i) => {
    if(d.visible !== "legendonly") 
      points += d.selectedpoints.length
  })
  
  document.getElementById("pointsCount").value = points

  console.log("plotly_restyle is triggered");
});