Example usage for org.jfree.chart.axis ValueAxis java2DToValue

List of usage examples for org.jfree.chart.axis ValueAxis java2DToValue

Introduction

In this page you can find the example usage for org.jfree.chart.axis ValueAxis java2DToValue.

Prototype

public abstract double java2DToValue(double java2DValue, Rectangle2D area, RectangleEdge edge);

Source Link

Document

Converts a coordinate in Java2D space to the corresponding data value, assuming that the axis runs along one edge of the specified dataArea.

Usage

From source file:net.sf.maltcms.chromaui.charts.tools.ChartTools.java

/**
 *
 * @param plot/*w  w  w.j a v a  2s  .  c o  m*/
 * @param screenPoint
 * @param screenDataArea
 * @return
 */
public static Point translatePointToImageCoord(XYPlot plot, final Point screenPoint,
        final Rectangle2D screenDataArea) {
    final ValueAxis da = plot.getDomainAxis();
    final ValueAxis ra = plot.getRangeAxis();

    final double x = da.java2DToValue(screenPoint.getX(), screenDataArea, plot.getDomainAxisEdge());
    final double y = ra.java2DToValue(screenPoint.getY(), screenDataArea, plot.getRangeAxisEdge());

    Logger.getLogger(ChartTools.class.getName()).log(Level.INFO, "{0} - {1}", new Object[] { x, y });

    if (x > 0 && y > 0) {
        return new Point((int) x, (int) y);
    }

    //        final double axisXperc = (x - plot.getDomainAxis().getLowerBound())
    //                / (plot.getDomainAxis().getUpperBound() - plot.getDomainAxis().getLowerBound());
    //        final double axisYperc = (y - plot.getRangeAxis().getLowerBound())
    //                / (plot.getRangeAxis().getUpperBound() - plot.getRangeAxis().getLowerBound());
    //
    //        System.out.println(axisXperc + " - " + axisYperc);
    //        final int newX = (int) (axisXperc * this.imageWidth);
    //        final int newY = (int) (axisYperc * this.imageHeight);
    return null;
}

From source file:org.gumtree.vis.mask.ChartMaskingUtilities.java

public static double translateScreenX(double screenX, Rectangle2D imageArea, JFreeChart chart) {
    //       XYPlot plot = chart.getXYPlot();
    //       boolean isDomainInverted = plot.getDomainAxis().isInverted();
    //       Range domainSection = plot.getDomainAxis().getRange();
    ValueAxis axis = chart.getXYPlot().getDomainAxis();

    return axis.java2DToValue(screenX, imageArea, RectangleEdge.BOTTOM);
    //       if (isDomainInverted) {
    //          return domainSection.getUpperBound() - (screenX - imageArea.getMinX()) 
    //                / imageArea.getWidth() * domainSection.getLength();
    //       } else {
    //          return (screenX - imageArea.getMinX()) / imageArea.getWidth() 
    //                * domainSection.getLength() + domainSection.getLowerBound();
    //       }//from ww w .  ja v a2  s. c  o m
}

From source file:org.gumtree.vis.mask.ChartMaskingUtilities.java

public static double translateScreenY(double screenY, Rectangle2D imageArea, JFreeChart chart,
        int rangeAxisIndex) {
    ValueAxis rangeAxis;
    if (rangeAxisIndex < 0 || rangeAxisIndex >= chart.getXYPlot().getRangeAxisCount()) {
        rangeAxis = chart.getXYPlot().getRangeAxis();
    } else {//from  www .  ja v  a  2  s.  co  m
        rangeAxis = chart.getXYPlot().getRangeAxis(rangeAxisIndex);
    }
    return rangeAxis.java2DToValue(screenY, imageArea, RectangleEdge.LEFT);
}

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;
    {//w  w  w  . j  av a 2s .  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:dbseer.gui.events.InformationChartMouseListener.java

@Override
public void chartMouseMoved(ChartMouseEvent event) {
    Rectangle2D dataArea = chartPanel.getScreenDataArea();
    JFreeChart chart = event.getChart();
    XYPlot plot = (XYPlot) chart.getPlot();
    ValueAxis xAxis = plot.getDomainAxis();
    double x = xAxis.java2DToValue(event.getTrigger().getX(), dataArea, RectangleEdge.BOTTOM);
    chartPanel.getVerticalCrossHair().setValue(x);
}

From source file:dbseer.gui.events.InformationChartMouseListener.java

@Override
public void chartMouseClicked(ChartMouseEvent event) {
    Rectangle2D dataArea = chartPanel.getScreenDataArea();
    JFreeChart chart = event.getChart();
    XYPlot plot = (XYPlot) chart.getPlot();
    ValueAxis xAxis = plot.getDomainAxis();
    int x = (int) Math.round(xAxis.java2DToValue(event.getTrigger().getX(), dataArea, RectangleEdge.BOTTOM));

    XYDataset dataset = plot.getDataset();
    int maxX = DatasetUtilities.findMaximumDomainValue(dataset).intValue();
    if (x >= 1 && x <= maxX) {
        int seriesCount = dataset.getSeriesCount();
        int[] mixtures = new int[seriesCount - 1];
        int total = 0;
        for (int i = 0; i < seriesCount - 1; ++i) {
            mixtures[i] = (int) dataset.getYValue(i, x - 1);
            total += mixtures[i];//ww  w . j  a  v  a 2s  . c o  m
        }
        for (int i = 0; i < seriesCount - 1; ++i) {
            mixtures[i] = (int) Math.round((double) mixtures[i] / (double) total * 100.0);
            tpsMixturePanel.setMixture(i, mixtures[i]);
        }
    }
}

From source file:edu.cmu.sv.modelinference.eventtool.charting.DataChart.java

private void createChartPanel(JFreeChart chart) {
    chartPanel = new ChartPanel(chart);

    chartPanel.addChartMouseListener(new ChartMouseListener() {

        @Override/*from   w w w  . ja  v a2s. c o  m*/
        public void chartMouseClicked(ChartMouseEvent arg0) {
            //ignore
        }

        @Override
        public void chartMouseMoved(ChartMouseEvent event) {
            Rectangle2D dataArea = chartPanel.getScreenDataArea();
            JFreeChart chart = event.getChart();
            XYPlot plot = (XYPlot) chart.getPlot();
            ValueAxis xAxis = plot.getDomainAxis();
            double x = xAxis.java2DToValue(event.getTrigger().getX(), dataArea, RectangleEdge.BOTTOM);
            ValueAxis yAxis = plot.getRangeAxis();
            double y = yAxis.java2DToValue(event.getTrigger().getY(), dataArea, RectangleEdge.LEFT);

            //Alternatively, obtain y for one of the subplots, which would be very neat.
            //We should find the "nearest" subplot to the cursor -- this is easy
            //double y = DatasetUtilities.findYValue(plot.getDataset(), 0, x);
            xCrosshair.setValue(x);
            yCrosshair.setValue(y);
        }
    });

    CrosshairOverlay crosshairOverlay = new CrosshairOverlay();
    xCrosshair = new Crosshair(Double.NaN, Color.GRAY, new BasicStroke(0f));
    xCrosshair.setLabelVisible(true);
    yCrosshair = new Crosshair(Double.NaN, Color.GRAY, new BasicStroke(0f));
    yCrosshair.setLabelVisible(true);
    crosshairOverlay.addDomainCrosshair(xCrosshair);
    crosshairOverlay.addRangeCrosshair(yCrosshair);
    chartPanel.addOverlay(crosshairOverlay);

    chartPanel.setPreferredSize(new java.awt.Dimension(800, 600));
    setContentPane(chartPanel);
}

From source file:slash.navigation.converter.gui.profileview.LazyToolTipChartPanel.java

protected String getTooltipAtPoint(Point point) {
    XYPlot plot = (XYPlot) getChart().getPlot();
    PlotRenderingInfo info = getChartRenderingInfo().getPlotInfo();
    double x0 = point.getX();
    double x1 = x0 - 2 * getScaleX();
    double x2 = x0 + 4 * getScaleX();

    ValueAxis domainAxis = plot.getDomainAxis();
    Rectangle2D screenArea = scale(info.getDataArea());
    double tx1 = domainAxis.java2DToValue(x1, screenArea, BOTTOM);
    double tx2 = domainAxis.java2DToValue(x2, screenArea, BOTTOM);

    for (int datasetIndex = 0; datasetIndex < plot.getDatasetCount(); datasetIndex++) {
        XYDataset dataset = plot.getDataset(datasetIndex);
        for (int seriesIndex = 0; seriesIndex < dataset.getSeriesCount(); seriesIndex++) {
            int itemCount = dataset.getItemCount(seriesIndex);
            for (int itemIndex = 0; itemIndex < itemCount; itemIndex++) {
                double xValue = dataset.getXValue(seriesIndex, itemIndex);
                if (tx1 < xValue && xValue < tx2)
                    return toolTipGenerator.generateToolTip(dataset, seriesIndex, itemIndex);
            }/*from www  .j  a va 2s  .c  o  m*/
        }
    }
    return null;
}

From source file:org.jfree.chart.demo.MouseListenerDemo4.java

public void chartMouseClicked(ChartMouseEvent chartmouseevent) {
    int i = chartmouseevent.getTrigger().getX();
    int j = chartmouseevent.getTrigger().getY();
    System.out.println("x = " + i + ", y = " + j);
    Point2D point2d = chartPanel.translateScreenToJava2D(new Point(i, j));
    XYPlot xyplot = (XYPlot) chart.getPlot();
    ChartRenderingInfo chartrenderinginfo = chartPanel.getChartRenderingInfo();
    java.awt.geom.Rectangle2D rectangle2d = chartrenderinginfo.getPlotInfo().getDataArea();
    ValueAxis valueaxis = xyplot.getDomainAxis();
    org.jfree.ui.RectangleEdge rectangleedge = xyplot.getDomainAxisEdge();
    ValueAxis valueaxis1 = xyplot.getRangeAxis();
    org.jfree.ui.RectangleEdge rectangleedge1 = xyplot.getRangeAxisEdge();
    double d = valueaxis.java2DToValue(point2d.getX(), rectangle2d, rectangleedge);
    double d1 = valueaxis1.java2DToValue(point2d.getY(), rectangle2d, rectangleedge1);
    System.out.println("Chart: x = " + d + ", y = " + d1);
}

From source file:com.haskins.cloudtrailviewer.feature.MetricsFeature.java

@Override
public void chartMouseMoved(ChartMouseEvent event) {

    Rectangle2D dataArea = this.chartPanel.getScreenDataArea();
    JFreeChart chart = event.getChart();
    XYPlot plot = (XYPlot) chart.getPlot();
    ValueAxis xAxis = plot.getDomainAxis();

    double x = xAxis.java2DToValue(event.getTrigger().getX(), dataArea, RectangleEdge.BOTTOM);

    this.xCrosshair.setValue(x);
}