use of org.netxms.ui.eclipse.charts.api.DataPoint in project netxms by netxms.
the class LineChart method getClosestDataPoint.
/**
* Get data point closest to given point in plot area
*
* @param px
* @param py
* @return
*/
public DataPoint getClosestDataPoint(int px, int py) {
IAxis xAxis = getAxisSet().getXAxis(0);
IAxis yAxis = getAxisSet().getYAxis(0);
double x = xAxis.getDataCoordinate(px);
double y = yAxis.getDataCoordinate(py);
double closestX = 0;
double closestY = 0;
double minDist = Double.MAX_VALUE;
ISeries closestSeries = null;
/* over all series */
ISeries[] series = getSeriesSet().getSeries();
for (ISeries s : series) {
double[] xS = s.getXSeries();
double[] yS = s.getYSeries();
/* check all data points */
for (int i = 0; i < xS.length; i++) {
/* compute distance to mouse position */
double newDist = Math.sqrt(Math.pow((x - xS[i]), 2) + Math.pow((y - yS[i]), 2));
/* if closer to mouse, remember */
if (newDist < minDist) {
minDist = newDist;
closestX = xS[i];
closestY = yS[i];
closestSeries = s;
}
}
}
return (closestSeries != null) ? new DataPoint(new Date((long) closestX), closestY, closestSeries) : null;
}