Example usage for org.jfree.chart ChartPanel translateScreenToJava2D

List of usage examples for org.jfree.chart ChartPanel translateScreenToJava2D

Introduction

In this page you can find the example usage for org.jfree.chart ChartPanel translateScreenToJava2D.

Prototype

public Point2D translateScreenToJava2D(Point screenPoint) 

Source Link

Document

Translates a panel (component) location to a Java2D point.

Usage

From source file:net.sf.mzmine.chartbasics.ChartLogics.java

/**
 * Translates mouse coordinates to chart coordinates (xy-axis)
 * //from   w w w.j av a2 s  .  c om
 * @param myChart
 * @param mouseX
 * @param mouseY
 * @return Range as chart coordinates
 * @throws Exception
 */
public static Point2D mouseXYToPlotXY(ChartPanel myChart, int mouseX, int mouseY) throws Exception {
    Point2D p = myChart.translateScreenToJava2D(new Point(mouseX, mouseY));

    XYPlot plot = null;
    // find plot as parent of axis
    ChartEntity entity = findChartEntity(myChart, mouseX, mouseY);
    if (entity instanceof AxisEntity) {
        Axis a = ((AxisEntity) entity).getAxis();
        if (a.getPlot() instanceof XYPlot)
            plot = (XYPlot) a.getPlot();
    }

    ChartRenderingInfo info = myChart.getChartRenderingInfo();
    int subplot = info.getPlotInfo().getSubplotIndex(p);
    Rectangle2D dataArea = info.getPlotInfo().getDataArea();
    if (subplot != -1)
        dataArea = info.getPlotInfo().getSubplotInfo(subplot).getDataArea();

    if (plot == null)
        plot = findXYSubplot(myChart.getChart(), info, p.getX(), p.getY());

    // coordinates
    double cx = 0;
    double cy = 0;
    if (plot != null) {
        // find axis
        ValueAxis domainAxis = plot.getDomainAxis();
        ValueAxis rangeAxis = plot.getRangeAxis();
        RectangleEdge domainAxisEdge = plot.getDomainAxisEdge();
        RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();
        // parent?
        if (domainAxis == null && plot.getParent() != null && plot.getParent() instanceof XYPlot) {
            XYPlot pp = ((XYPlot) plot.getParent());
            domainAxis = pp.getDomainAxis();
            domainAxisEdge = pp.getDomainAxisEdge();
        }
        if (rangeAxis == null && plot.getParent() != null && plot.getParent() instanceof XYPlot) {
            XYPlot pp = ((XYPlot) plot.getParent());
            rangeAxis = pp.getRangeAxis();
            rangeAxisEdge = pp.getRangeAxisEdge();
        }

        if (domainAxis != null)
            cx = domainAxis.java2DToValue(p.getX(), dataArea, domainAxisEdge);
        if (rangeAxis != null)
            cy = rangeAxis.java2DToValue(p.getY(), dataArea, rangeAxisEdge);
    } else {
        throw new Exception("no xyplot found");
    }
    return new Point2D.Double(cx, cy);

}

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;
    {/*from ww w .j  av a 2s . c  om*/
        // 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:net.bioclipse.model.ScatterPlotMouseHandler.java

public void mouseClicked(MouseEvent me) {
    Point2D p = null;//w  w  w .  j a  va  2s .  co  m
    ChartDescriptor cd = null;
    int[] indices = null;
    JFreeChart selectedChart = null;

    ChartPanel chartPanel = getChartPanel(me);
    p = chartPanel.translateScreenToJava2D(new Point(me.getX(), me.getY()));
    selectedChart = chartPanel.getChart();

    cd = ChartUtils.getChartDescriptor(selectedChart);
    indices = cd.getSourceIndices();

    XYPlot plot = (XYPlot) chartPanel.getChart().getPlot();

    XYItemRenderer plotRenderer = plot.getRenderer();

    if (!(plotRenderer instanceof ScatterPlotRenderer)) {
        throw new IllegalStateException(
                "Charts using ScatterPlotMouseHandler must use ScatterPlotRenderer as their renderer");
    }
    renderer = (ScatterPlotRenderer) plot.getRenderer();

    // now convert the Java2D coordinate to axis coordinates...
    Number xx = getDomainX(chartPanel, plot, p);
    Number yy = getRangeY(chartPanel, plot, p);

    //Find the selected point in the dataset
    //If shift is down, save old selections
    if (!me.isShiftDown() || currentSelection == null) {
        currentSelection = new ChartSelection();
    }

    for (int j = 0; j < plot.getDataset().getItemCount(plot.getDataset().getSeriesCount() - 1); j++) {
        for (int i = 0; i < plot.getDataset().getSeriesCount(); i++) {
            Number xK = plot.getDataset().getX(i, j);
            Number yK = plot.getDataset().getY(i, j);
            Number xKCheck = xK.doubleValue() - xx.doubleValue();
            Number yKCheck = yK.doubleValue() - yy.doubleValue();
            Number xxCheck = xKCheck.doubleValue() * xKCheck.doubleValue();
            Number yyCheck = yKCheck.doubleValue() * yKCheck.doubleValue();
            //Check distance from click and point, don't want to mark points that are too far from the click
            if (Math.sqrt(xxCheck.doubleValue()) <= 0.1 && Math.sqrt(yyCheck.doubleValue()) <= 0.1) {
                //Create a new selection
                PlotPointData cp = new PlotPointData(indices[j], cd.getXLabel(), cd.getYLabel());
                cp.setDataPoint(j, i);
                currentSelection.addPoint(cp);
                if (!me.isShiftDown())
                    renderer.clearMarkedPoints();
                renderer.addMarkedPoint(j, i);
                selectedChart.plotChanged(new PlotChangeEvent(plot));

            }
        }
    }
    currentSelection.setDescriptor(cd);
    ChartUtils.updateSelection(currentSelection);
}