Differentiate hover and click action when using view
#1806
-
Hi, I previously asked in the Observable Forum regarding how to set a value when the user interacts with the plot. While the How do I update the value only if the user clicks the map, not hovers on it? I want the hover action to only show the tooltip, not update the value. Thank you so much! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Thank you for the pointer! I found a working implementation in this Observable Notebook and modify the code to work in Observable Framework as follows: const hover = view(Plot.plot({
marks: [Plot.dot(cars, { x: "power (hp)", y: "economy (mpg)", tip: true })]
})) const click = Generators.observe((notify) => {
let lastValue = null;
const listener = (e) => {
// If e.detail is true, then this was a click (sticky) event.
// If e.detail is false, then (this feels untidy) deselect if the value is null.
if (e.sticky || (hover.value === null && lastValue !== null)) {
lastValue = hover.value;
notify(lastValue);
}
};
hover.addEventListener("input", listener);
notify(lastValue);
return () => hover.removeEventListener("input", listener);
}) However, I got the |
Beta Was this translation helpful? Give feedback.
-
Thank you, I managed to make it work with the following code: const clickEvent = (plot) => Generators.observe((notify) => {
const onClick = (e) => {
notify(plot.value);
};
plot.addEventListener('click', onClick);
return () => {
plot.removeEventListener('click', onClick);
};
}); How can I use the function scatterPlot(width) {
return Plot.plot({
marks: [Plot.dot(cars, { x: "power (hp)", y: "economy (mpg)", tip: true })]
})}; <div class="grid grid-cols-1">
<div class="card">${resize((width) => scatterPlot(width))}</div>
</div> |
Beta Was this translation helpful? Give feedback.
As mentioned in the docs:
hover
is the value generator, while what you want to listen to is the input events on the chart. You could transform your code by naming the chart:and then listen to input events on
chart