I have been struggling like mad to solve an apparently basic question. Imagine you have a scatter plot, with say ... 10 markers. I suppose this plot has been generated using plotly within a Shiny environment.
One can easily get the coordinates of these markers using the event_data("plotly_click")
code.
Now imagine you do not need the coordinates of these markers, but the coordinates generated by a mouse click but precisely where no marker exists (for example because you would like to set a new marker exactly there, and you would like to re-use the information coming from that mouse click).
I cannot obtain such a behavior using onclick()
, or whatever.
Any idea ?
You could add a D3 event listener to your plot
and then
d3.event.layerX
resp.layerY
)document.getElementsByClassName('bg')[0].attributes['x']
)myPlot.layout.xaxis.range[0]
)The new
x
andy
value are then pushed to the existing graphComplete R code
Interactive Javascript example
Shiny example
The solution by Maximilian does not work on Plotly.js versions later than 1.42.0. Trying to fetch
returns undefined. The solution works using version 1.41.3.
This answer is most likely more suited to be a comment but my reputation does not meet the minimum requirement of 50.