Example usage for org.jfree.chart.plot PlotRenderingInfo getSubplotIndex

List of usage examples for org.jfree.chart.plot PlotRenderingInfo getSubplotIndex

Introduction

In this page you can find the example usage for org.jfree.chart.plot PlotRenderingInfo getSubplotIndex.

Prototype

public int getSubplotIndex(Point2D source) 

Source Link

Document

Returns the index of the subplot that contains the specified (x, y) point (the "source" point).

Usage

From source file:ec.util.chart.swing.Charts.java

@Nullable
public static LegendItemEntity getSeriesForPoint(@Nonnull Point pt, @Nonnull ChartPanel cp) {

    final double chartX;
    final double chartY;
    final Rectangle2D plotArea;
    final XYPlot plot;
    {//  ww w .  j  av a  2  s .  co  m
        // Let's find the X and Y values of the clicked point
        Point2D p = cp.translateScreenToJava2D(pt);
        chartX = p.getX();
        chartY = p.getY();
        // Let's find plotArea and plot
        XYPlot tmpPlot = cp.getChart().getXYPlot();
        PlotRenderingInfo plotInfo = cp.getChartRenderingInfo().getPlotInfo();
        if (tmpPlot instanceof CombinedDomainXYPlot) {
            int subplotIndex = plotInfo.getSubplotIndex(p);
            if (subplotIndex == -1) {
                return null;
            }
            plotArea = plotInfo.getSubplotInfo(subplotIndex).getDataArea();
            plot = ((CombinedDomainXYPlot) tmpPlot).findSubplot(plotInfo, p);
        } else {
            plotArea = plotInfo.getDataArea();
            plot = tmpPlot;
        }
    }

    // Let's avoid unnecessary computation
    final ValueAxis domainAxis = plot.getDomainAxis();
    final ValueAxis rangeAxis = plot.getRangeAxis();
    final RectangleEdge domainAxisEdge = plot.getDomainAxisEdge();
    final RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();
    final double x = domainAxis.java2DToValue(chartX, plotArea, domainAxisEdge);

    final double sensitivity = TOL;
    double distanceClickSeries = TOL + 1;

    Entry<XYDataset, Comparable> result = null;

    // For each series in each datasets
    for (XYDataset dataset : asDatasetList(plot)) {
        for (int series = 0; series < dataset.getSeriesCount(); series++) {
            // Index of the closest data item of the current series just left to the click
            int lp = getNearestLeftPoint(x, 0, dataset.getItemCount(series) - 1, series, dataset);

            try {
                // X and Y values of data items to the left and to the right
                double leftX = dataset.getXValue(series, lp);
                double leftY = dataset.getYValue(series, lp);
                double rightX = dataset.getXValue(series, lp + 1);
                double rightY = dataset.getYValue(series, lp + 1);

                double lx = domainAxis.valueToJava2D(leftX, plotArea, domainAxisEdge);
                double ly = rangeAxis.valueToJava2D(leftY, plotArea, rangeAxisEdge);
                double rx = domainAxis.valueToJava2D(rightX, plotArea, domainAxisEdge);
                double ry = rangeAxis.valueToJava2D(rightY, plotArea, rangeAxisEdge);

                // Distance to left point
                double distL = Point2D.distance(lx, ly, chartX, chartY);
                // Distance to right point
                double distR = Point2D.distance(rx, ry, chartX, chartY);
                // Average of both distances
                double distLRavg = (distL + distR) / 2d;
                // Distance to the segment between L and R
                //double distSeg = Line2D.ptSegDist(leftX, leftY, rightX, rightY, chartX, chartY);
                double distSeg = ptSegDist(lx, ly, rx, ry, chartX, chartY);

                // With a line renderer, this is probably a bit of overkill as
                // distSeg would be enough, but it becomes more reliable to check all these
                // if using splines
                double tmp = Math.min(Math.min(distSeg, Math.min(distL, distR)), distLRavg);

                // Are we closer than the previous series?
                if (tmp < sensitivity && tmp < distanceClickSeries) {
                    distanceClickSeries = tmp;
                    result = new SimpleEntry<>(dataset, dataset.getSeriesKey(series));
                }
            } catch (Exception ex) {
                /*
                 * An exception might happen when some series have less data
                 * than others, catching the the exception here will simply rule
                 * them out from the detection on this click
                 */
            }
        }
    }

    return result != null ? createFakeLegendItemEntity(result.getKey(), result.getValue()) : null;
}

From source file:org.talend.dataprofiler.chart.util.ToolTipChartComposite.java

/**
 * This method attempts to get a tooltip by converting the screen X,Y into Chart Area X,Y and then looking for a
 * data point in a data set that lies inside a hotspot around that value.
 * /*from ww w  .  ja va2 s  .  c  o  m*/
 * @param point The Java 2D point
 * @return A string for the data at the point or null if no data is found.
 */
protected String getTooltipAtPoint(Point point) {
    String result = null;

    Point2D translatedPoint = this.translateScreenToJava2D(point);
    Plot plot = this.getChart().getPlot();
    PlotRenderingInfo info = this.getChartRenderingInfo().getPlotInfo();
    if (plot instanceof CombinedDomainXYPlot) {
        int index = info.getSubplotIndex(translatedPoint);
        if (index < 0) {
            index = 0;
        }
        plot = (Plot) ((CombinedDomainXYPlot) plot).getSubplots().get(index);
        info = this.getChartRenderingInfo().getPlotInfo().getSubplotInfo(index);
    }
    if (plot != null && plot instanceof XYPlot) {
        XYPlot xyPlot = (XYPlot) plot;
        ValueAxis domainAxis = xyPlot.getDomainAxis();
        ValueAxis rangeAxis = xyPlot.getRangeAxis();
        // had to switch to SWT's rectangle here.
        Rectangle screenArea = this.scale(info.getDataArea());

        double hotspotSizeX = hotspontsize * this.getScaleX();
        double hotspotSizeY = hotspontsize * this.getScaleY();
        double x0 = point.getX();
        double y0 = point.getY();
        double x1 = x0 - hotspotSizeX;
        double y1 = y0 + hotspotSizeY;
        double x2 = x0 + hotspotSizeX;
        double y2 = y0 - hotspotSizeY;
        RectangleEdge xEdge = RectangleEdge.BOTTOM;
        RectangleEdge yEdge = RectangleEdge.LEFT;
        // Switch everything for horizontal charts
        if (xyPlot.getOrientation() == PlotOrientation.HORIZONTAL) {
            hotspotSizeX = hotspontsize * this.getScaleY();
            hotspotSizeY = hotspontsize * this.getScaleX();
            x0 = point.getY();
            y0 = point.getX();
            x1 = x0 + hotspotSizeX;
            y1 = y0 - hotspotSizeY;
            x2 = x0 - hotspotSizeX;
            y2 = y0 + hotspotSizeY;
            xEdge = RectangleEdge.LEFT;
            yEdge = RectangleEdge.BOTTOM;
        }

        // OK, here we have to get ourselves back into AWT land...
        Rectangle2D r2d = new Rectangle2D.Double();
        r2d.setRect(screenArea.x, screenArea.y, screenArea.width, screenArea.height);

        double ty0 = rangeAxis.java2DToValue(y0, r2d, yEdge);
        double tx1 = domainAxis.java2DToValue(x1, r2d, xEdge);
        double ty1 = rangeAxis.java2DToValue(y1, r2d, yEdge);
        double tx2 = domainAxis.java2DToValue(x2, r2d, xEdge);
        double ty2 = rangeAxis.java2DToValue(y2, r2d, yEdge);

        int datasetCount = xyPlot.getDatasetCount();
        for (int datasetIndex = 0; datasetIndex < datasetCount; datasetIndex++) {
            XYDataset dataset = xyPlot.getDataset(datasetIndex);
            int seriesCount = dataset.getSeriesCount();
            for (int series = 0; series < seriesCount; series++) {
                int itemCount = dataset.getItemCount(series);
                if (dataset instanceof OHLCDataset) {
                    // This could be optimized to use a binary search for x first
                    for (int item = 0; item < itemCount; item++) {
                        double xValue = dataset.getXValue(series, item);
                        double yValueHi = ((OHLCDataset) dataset).getHighValue(series, item);
                        double yValueLo = ((OHLCDataset) dataset).getLowValue(series, item);
                        // Check hi lo and swap if needed
                        if (yValueHi < yValueLo) {
                            double temp = yValueHi;
                            yValueHi = yValueLo;
                            yValueLo = temp;
                        }
                        // Check if the dataset 'X' value lies between the hotspot (tx1 < xValue < tx2)
                        if (tx1 < xValue && xValue < tx2) {
                            // Check if the cursor 'y' value lies between the high and low (low < ty0 < high)
                            if (yValueLo < ty0 && ty0 < yValueHi) {
                                return hiLoTips.generateToolTip(dataset, series, item);
                            }
                        }
                    }
                } else {
                    // This could be optimized to use a binary search for x first
                    for (int item = 0; item < itemCount; item++) {
                        double xValue = dataset.getXValue(series, item);
                        double yValue = dataset.getYValue(series, item);
                        // Check if the dataset 'X' value lies between the hotspot (tx1< xValue < tx2)
                        if (tx1 < xValue && xValue < tx2) {
                            // Check if the dataset 'Y' value lies between the hotspot (ty1 < yValue < ty2)
                            if (ty1 < yValue && yValue < ty2) {
                                return xyTips.generateToolTip(dataset, series, item);
                            }
                        }
                    }
                }
            }
        }
    }

    return result;
}

From source file:de.hs.mannheim.modUro.controller.diagram.fx.ChartCanvas.java

/**
 * Returns the data area (the area inside the axes) for the plot or subplot.
 *
 * @param point  the selection point (for subplot selection).
 *
 * @return The data area./*from  w  w  w. j  av  a2s.c o m*/
 */
public Rectangle2D findDataArea(Point2D point) {
    PlotRenderingInfo plotInfo = this.info.getPlotInfo();
    Rectangle2D result;
    if (plotInfo.getSubplotCount() == 0) {
        result = plotInfo.getDataArea();
    } else {
        int subplotIndex = plotInfo.getSubplotIndex(point);
        if (subplotIndex == -1) {
            return null;
        }
        result = plotInfo.getSubplotInfo(subplotIndex).getDataArea();
    }
    return result;
}

From source file:org.gvsig.remotesensing.scatterplot.chart.ScatterPlotDiagram.java

/**
 * Returns the data area (the area inside the axes) for the plot or subplot,
 * with the current scaling applied.//from   w  w w. j  a  v  a 2s  .c  om
 *
 * @param x  the x-coordinate (for subplot selection).
 * @param y  the y-coordinate (for subplot selection).
 * 
 * @return The scaled data area.
 */
public Rectangle2D getScreenDataArea(int x, int y) {
    PlotRenderingInfo plotInfo = this.info.getPlotInfo();
    Rectangle2D result;
    if (plotInfo.getSubplotCount() == 0) {
        result = getScreenDataArea();
    } else {
        Point2D selectOrigin = translateScreenToJava2D(new Point(x, y));
        int subplotIndex = plotInfo.getSubplotIndex(selectOrigin);
        if (subplotIndex == -1) {
            return null;
        }
        result = scale(plotInfo.getSubplotInfo(subplotIndex).getDataArea());
    }
    return result;
}

From source file:de.xirp.ui.widgets.custom.XChartComposite.java

/**
 * Returns the data area (the area inside the axes) for the plot
 * or subplot, with the current scaling applied.
 * /*from   w ww  .  j av  a  2 s.  com*/
 * @param x
 *            the x-coordinate (for subplot selection).
 * @param y
 *            the y-coordinate (for subplot selection).
 * @return The scaled data area.
 */
public Rectangle getScreenDataArea(int x, int y) {
    PlotRenderingInfo plotInfo = this.info.getPlotInfo();
    Rectangle result;
    if (plotInfo.getSubplotCount() == 0) {
        result = getScreenDataArea();
    } else {
        // get the origin of the zoom selection in the Java2D
        // space used for
        // drawing the chart (that is, before any scaling to fit
        // the panel)
        Point2D selectOrigin = translateScreenToJava2D(new Point(x, y));
        int subplotIndex = plotInfo.getSubplotIndex(selectOrigin);
        if (subplotIndex == -1) {
            return null;
        }
        result = scale(plotInfo.getSubplotInfo(subplotIndex).getDataArea());
    }
    return result;
}

From source file:com.munch.exchange.ExchangeChartComposite.java

/**
 * Returns the data area (the area inside the axes) for the plot or subplot,
 * with the current scaling applied./*from ww  w .j  a  va  2 s .c o  m*/
 *
 * @param x  the x-coordinate (for subplot selection).
 * @param y  the y-coordinate (for subplot selection).
 *
 * @return The scaled data area.
 */
public Rectangle getScreenDataArea(int x, int y) {
    PlotRenderingInfo plotInfo = this.info.getPlotInfo();
    Rectangle result;
    if (plotInfo.getSubplotCount() == 0)
        result = getScreenDataArea();
    else {
        // get the origin of the zoom selection in the Java2D space used for
        // drawing the chart (that is, before any scaling to fit the panel)
        Point2D selectOrigin = translateScreenToJava2D(new Point(x, y));
        int subplotIndex = plotInfo.getSubplotIndex(selectOrigin);
        if (subplotIndex == -1) {
            return null;
        }
        result = scale(plotInfo.getSubplotInfo(subplotIndex).getDataArea());
    }
    return result;
}

From source file:org.mwc.cmap.grideditor.chart.FixedChartComposite.java

/**
 * Returns the data area (the area inside the axes) for the plot or subplot,
 * with the current scaling applied.//from   w  w w . jav  a  2 s . c  o m
 * 
 * @param x
 *          the x-coordinate (for subplot selection).
 * @param y
 *          the y-coordinate (for subplot selection).
 * 
 * @return The scaled data area.
 */
public Rectangle getScreenDataArea(final int x, final int y) {
    final PlotRenderingInfo plotInfo = this.info.getPlotInfo();
    Rectangle result;
    if (plotInfo.getSubplotCount() == 0)
        result = getScreenDataArea();
    else {
        // get the origin of the zoom selection in the Java2D space used for
        // drawing the chart (that is, before any scaling to fit the panel)
        final Point2D selectOrigin = translateScreenToJava2D(new Point(x, y));
        final int subplotIndex = plotInfo.getSubplotIndex(selectOrigin);
        if (subplotIndex == -1) {
            return null;
        }
        result = scale(plotInfo.getSubplotInfo(subplotIndex).getDataArea());
    }
    return result;
}

From source file:com.isti.traceview.common.TraceViewChartPanel.java

/**
 * Returns the data area (the area inside the axes) for the plot or subplot, with the current
 * scaling applied./*from  w  w w.  j  a  v  a  2s  .c  om*/
 * 
 * @param x
 *            the x-coordinate (for subplot selection).
 * @param y
 *            the y-coordinate (for subplot selection).
 * @return The scaled data area.
 */
public Rectangle2D getScreenDataArea(int x, int y) {
    PlotRenderingInfo plotInfo = this.info.getPlotInfo();
    Rectangle2D result;
    if (plotInfo.getSubplotCount() == 0) {
        result = getScreenDataArea();
    } else {
        // get the origin of the zoom selection in the Java2D space used for
        // drawing the chart (that is, before any scaling to fit the panel)
        Point2D selectOrigin = translateScreenToJava2D(new Point(x, y));
        int subplotIndex = plotInfo.getSubplotIndex(selectOrigin);
        if (subplotIndex == -1) {
            return null;
        }
        result = scale(plotInfo.getSubplotInfo(subplotIndex).getDataArea());
    }
    return result;
}

From source file:com.rapidminer.gui.plotter.charts.AbstractChartPanel.java

/**
 * Returns the data area (the area inside the axes) for the plot or subplot, with the current
 * scaling applied./*from   w ww  . j a  va 2 s .co  m*/
 * 
 * @param x
 *            the x-coordinate (for subplot selection).
 * @param y
 *            the y-coordinate (for subplot selection).
 * 
 * @return The scaled data area.
 */

@Override
public Rectangle2D getScreenDataArea(int x, int y) {
    PlotRenderingInfo plotInfo = this.info.getPlotInfo();
    Rectangle2D result;
    if (plotInfo.getSubplotCount() == 0) {
        result = getScreenDataArea();
    } else {
        // get the origin of the zoom selection in the Java2D space used for
        // drawing the chart (that is, before any scaling to fit the panel)
        Point2D selectOrigin = translateScreenToJava2D(new Point(x, y));
        int subplotIndex = plotInfo.getSubplotIndex(selectOrigin);
        if (subplotIndex == -1) {
            return null;
        }
        result = scale(plotInfo.getSubplotInfo(subplotIndex).getDataArea());
    }
    return result;
}