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

Obtaining the data coordinates of the mouse

I’m developing an application in which I need to get the ‘x, y’ coordinates of the mouse when the user clicks the screen. By ‘x, y’ coordinates I don’t mean mousedown.clientX and mousedown.clientY, I need to know the data coordinates (or an approximation of it) of the point in which the users clicked in the graph (on the graph’s scale) and this points is not necessarily part the data array (so using myDiv.on('plotly_click', callback(data)) doesn’t help me too much either). For example:

Is there any way to map between pixel coordinates and data coordinates?

I guess I could plot an invisible grid or something and get the closest point of this grid, but this seems like a really stupid solution.

The codepen mentioned by @etienne in this topic helps a lot: How to make 'shape' track mouse motion?

2 Likes