getPointsAtEvent and activePoints
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?
For now as a temporary fix I simply empty the array to keep only one point but it is far from optimal/precise...
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:
- 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.
- 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.
@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.
@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 ?
A fix for V1 would be to sort the array by distance from the event point and then take the first item.