Example usage for org.jfree.chart.axis Axis getPlot

List of usage examples for org.jfree.chart.axis Axis getPlot

Introduction

In this page you can find the example usage for org.jfree.chart.axis Axis getPlot.

Prototype

public Plot getPlot() 

Source Link

Document

Returns the plot that the axis is assigned to.

Usage

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

/**
 * Translates mouse coordinates to chart coordinates (xy-axis)
 * /*from w ww. j  a  v  a2  s.c om*/
 * @param myChart
 * @param mouseX
 * @param mouseY
 * @return Range as chart coordinates (never null)
 */
public static Point2D mouseXYToPlotXY(ChartViewer myChart, int mouseX, int mouseY) {
    XYPlot plot = null;
    // find plot as parent of axis
    ChartEntity entity = findChartEntity(myChart.getCanvas(), mouseX, mouseY);
    if (entity instanceof AxisEntity) {
        Axis a = ((AxisEntity) entity).getAxis();
        if (a.getPlot() instanceof XYPlot)
            plot = (XYPlot) a.getPlot();
    }

    ChartRenderingInfo info = myChart.getRenderingInfo();
    int subplot = info.getPlotInfo().getSubplotIndex(new Point2D.Double(mouseX, mouseY));
    Rectangle2D dataArea = info.getPlotInfo().getDataArea();
    if (subplot != -1)
        dataArea = info.getPlotInfo().getSubplotInfo(subplot).getDataArea();

    // find subplot or plot
    if (plot == null)
        plot = findXYSubplot(myChart.getChart(), info, mouseX, mouseY);

    // 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(mouseX, dataArea, domainAxisEdge);
        if (rangeAxis != null)
            cy = rangeAxis.java2DToValue(mouseY, dataArea, rangeAxisEdge);
    }
    return new Point2D.Double(cx, cy);
}

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

/**
 * Translates mouse coordinates to chart coordinates (xy-axis)
 * //from ww w  .  j a  va2s. 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);

}