Question: How can I get the x/y data values from an Obervable Plot for the point the mouse is hovering over? (Or touched on mobile.)
Background: I am trying to make an interactive timeline like on https://merrysky.net. So I would like to convert the mouse’s X position to the time/temperature value from the data that has been plotted.
Current progress:
- Currently, the red rule on the plot reactively updates (try playing/dragging the radar timeline.)
- The next task is to reactively update in the other direction: the time and temperature data that is displayed at the very top change when the mouse hovers over the temperature plot. (The red rule should follow the mouse pointer and the appropriate data should be updated.)
I have examined the Observable Plot documentation. There seems to be API’s for updating certain marks when the pointer is near them, but I could not see anything for extracting data from the plot based on mouse position. The Crosshair mark seems to be the closest to what I am looking for.
Perhaps the underlying D3 API must be used?
2
Answers
I have adapted Mark's answer for my application:
The radar timeline and plot are now mutually reactive:
Notable changes/improvements to Mark's answer:
pg = d3.select(div).select('g')
. For some reasond3.select('g')
did not work in my app (and the selector was too broad, selecting another svg on the page).value
is outside plot's domain.render()
. (Not needed, but fixes type error.)Other major additions:
plot.addEventListener()
to update variables and elements outside the plot.Wow, Observable Plot is extremely cool. Looks like you can completely replicate your target (and what I did 8 years ago) as easy as:
Running example:
— Edits based on comment —
How about something like this. It provides an entry point to the plot so you can only tear down and recreate what you need: