This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi Jason, Although this will work for a simple example such as the one originally provided (i.e. 2 y values for a given x value) when you have even more possible Y values for a given x value then you will notice that when you bring the mouse really close to some of the lines the tracking element will not always be shown in correct place. I have reproduced this in the test project you provided originally and it is attached to this thread. I am looking at ways of improving the algorithm as well however if you could kindly help out it would be appreciated. Thank you in advance. Kamal |
|
|
Thanks for the repro project Kamal. My apologies, comparing the distance magnitude values plus the x delta values was clearly not the right way to do this for your scenario. My previous sample was too simple so I didn't pick up on this. What you'll want to do is iterate through all the points (like it is doing currently), but instead of comparing the points themselves, interpolate each adjacent pair of points with the X value of the mouse point and then compare these interpolated values. For example, start with point 0 and point 1. If the mouse X value is between the X values of these 2 points, then interpolate the line between these 2 points to find the X,Y co-ordinate at X = mousePoint.X. Next calculate the distance from the mouse point to that interpolated point. check to see if that distance is less than the closestDist, and if so, update the closestPoint and closestDist values (similar to what is already being done in the TrackingElement). Then move on to Point 1 and Point 2 and repeat. When the loop is finished you'll have the closest interpolated point on the chart where X = mousePoint.X. Depending on how your chart is expected to be used, you may want to interpolate for Y = mousePoint.Y instead of X = mousePoint.X. Alternatively, you may want to use vector geometry to find the closest point on the line regardless of the X and Y values specifically. You may want to experiment with all 3 approaches to see which behavior feels best for your chart. Interpolating every pair of points may slow down a little for very large data sets. However, if you know that the data is sequential along the Y axis (as it is in the sample you posted), then you can simply run a binary search algorithm on the data to find the points closest to the mouse Y value. This will greatly narrow down which points you need to interpolate. Jason Fauchelle |
|