Search in sources :

Example 1 with DataPoint

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;
}
Also used : DataPoint(org.netxms.ui.eclipse.charts.api.DataPoint) ISeries(org.swtchart.ISeries) IAxis(org.swtchart.IAxis) Point(org.eclipse.swt.graphics.Point) DataPoint(org.netxms.ui.eclipse.charts.api.DataPoint) Date(java.util.Date)

Aggregations

Date (java.util.Date)1 Point (org.eclipse.swt.graphics.Point)1 DataPoint (org.netxms.ui.eclipse.charts.api.DataPoint)1 IAxis (org.swtchart.IAxis)1 ISeries (org.swtchart.ISeries)1