Black Lives Matter. Please consider donating to Black Girls Code today.

Returning specific *data* element with plotly_click function

Hi all,

for sure this question is trivial for many of you, but I totally lack in js coding.

I have a simple scatter plot and I pass, besides x and y also an ids list that contains ids of each feature. Warning this ids are different than the standard id returned by plotly.

What I’m not able to do is getting the specific element of the ids list when clicked on the corresponding point in the plot.

The plot code is here:

https://jsfiddle.net/ndhqq2d2/

hovering on the points return the value, an additional text and the corresponding ids.

Is it possible to get that value?

Many thanks for any hint

Matteo

image

Here are all the fields you can grab from the plotly click event data.

Hi Etienne,

thanks again for the answer.
My problem is that I’m not able to get the single data of the clicked point. It is easy with x, y, id, pointNumber. But with something like this:

plotly_div.on('plotly_click', function(data){
  console.log(data.points[0].data.ids)
})

I get the whole list of ids and not just the single item…

Maybe that’s more a pure js question that a plotly one…

Thanks again

what about this

plotly_div.on('plotly_click', function(data){
  console.log(data.points[0].id)
})

?

that’s exactly what you suggested me here:

the problem is that sometimes (I did not figure out when and why this happens, I think might be input data related) the id returned by plotly (what you wrote above) is not the same of the original id of the data.

So I was thinking that passing a default array with custom ids (different than standard ones) might be the solution