Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

getPointsAtEvent and activePoints

Open akougblenou opened this issue 11 years ago • 5 comments

I was trying to create a function driven by the click event and I wanted to use the getPointsAtEvent, only this function is (as it names indicates it) returns an array of many points and I was hoping to get a single one. My question is, is it possible to get only one point back and how to do so?

akougblenou avatar Mar 15 '15 16:03 akougblenou

For now as a temporary fix I simply empty the array to keep only one point but it is far from optimal/precise...

akougblenou avatar Mar 15 '15 16:03 akougblenou

The problem you'll run in to is: what happens if you aren't clicking exactly on a point (what if you're clicking in between points)? That said there are a couple of options:

  1. You can fiddle around with different values for the pointHitDetectionRadius option. Though this is a bit dangerous if you have a chart with very few data points.
  2. You can write you're own function to get the middle point from the array returned by getPointsAtEvent: var theFrames = theChart.getPointsAtEvent(evt); var theFrame = theFrames[Math.floor(theFrames.length / 2)];

I'm working on a way to just find the closest point to the click event. If I come up with something I'll post here.

JamesWilsonProjects avatar Mar 16 '15 05:03 JamesWilsonProjects

@JamesWilsonNovel thanks for your tips, I have lots of values (about a couple hundreds) so I tried setting the pointHitDetectionRadius to 1, but it did not seem to be more precise, I will try your hack that gets the middle point, but I guess it would be best to get the closest point to where I clicked.

akougblenou avatar Mar 17 '15 09:03 akougblenou

@JamesWilsonNovel another question would be how to put this point in emphasis as the marker for each points are usually set before hand, is there a way to style them on the fly ? Should I just get the canvas context and work from there ?

akougblenou avatar Apr 14 '15 08:04 akougblenou

A fix for V1 would be to sort the array by distance from the event point and then take the first item.

etimberg avatar Dec 25 '15 00:12 etimberg