ChartDirector Support Forum Home   Search
Method not in pair?
Posted by Thexder on Feb-09-2021 08:33
Hi Peter,

In some situation I found I need to use nearest data value, and we have
c->getNearestXValue(ChartViewer.getChartMouseX)  for X-axis, but there is
no c->getNearestYValue() method in pair, then I have to make higher density for Y-axis in order to get a data value close to the mouse point.

do you have any other better way to do this?

Rgds,

Willie

Re: Method not in pair?
Posted by Peter Kwan on Feb-09-2021 23:33
Hi Thexder,

The getNearestXValue is designed to track cursor applications. It does not found the "nearest data value". It finds the x-position of a vertical line (the track cursor) that passes through a data point and is nearest to the given x-coordinate. The data point can be quite far away from the mouse cursor, because it can be far away in the y direction.

Would you mind to clarify what would you like to get? What does "a data value close to the mouse point" mean?

If it means the distance between a data point and the mouse cursor point, then getNearestXValue and getNearestYValue (even if it exists) are not useful. The nearest data point may not be a point with the nearest x or y values. It is a point with the smallest (dataX - mouseDataX) * (dataX - mouseDataX) + (dataY - mouseDataY) * (dataY - mouseDataY).

You do not need to plot a chart with high resolution to distinguish the data points, because your code does not need to look at the chart.

(a) First, from the mouse (x, y) pixel coordinates, obtain the x and y data values (use XYChart.getXValue, XYChart.getYValue).

(b) Use a loop to iterate your data arrays to compute the distance between a data point to the (x, y) values at the mouse and pick the point with the distance.

Regards
Peter Kwan

Re: Method not in pair?
Posted by Thexder on Feb-10-2021 14:10
Hi Peter,

thanks for the help,

I do made a stupid mistake in my code, I used integer type instead of float for distance calculation, that's why I can't get the nearest data point Y, thanks and sorry to bother you for my simple silly question.

Rgds,

Willie