Example usage for org.jfree.chart.plot XYPlot getDataRange

List of usage examples for org.jfree.chart.plot XYPlot getDataRange

Introduction

In this page you can find the example usage for org.jfree.chart.plot XYPlot getDataRange.

Prototype

@Override
public Range getDataRange(ValueAxis axis) 

Source Link

Document

Returns the range for the specified axis.

Usage

From source file:de.mpg.mpi_inf.bioinf.netanalyzer.ui.charts.JFreeChartConn.java

/**
 * Updates the axis-related properties of a chart.
 * /*from   w ww . j a v a2  s .  c om*/
 * @param aControl
 *            Chart control to be updated.
 * @param aAxes
 *            Axis-related visual settings to be applied.
 * @param aGrid
 *            Grid-related visual settings to be applied.
 */
public static void updateAxes(JFreeChart aControl, AxesSettings aAxes, GridSettings aGrid) {
    XYPlot plot = aControl.getXYPlot();
    Range domainDataRange = aAxes.getLogarithmicDomainAxis()
            ? new Range(logLowerBound(plot.getDataset(), true),
                    plot.getDataRange(plot.getDomainAxis()).getUpperBound())
            : plot.getDataRange(plot.getDomainAxis());
    Range rangeDataRange = aAxes.getLogarithmicRangeAxis()
            ? new Range(logLowerBound(plot.getDataset(), false),
                    plot.getDataRange(plot.getRangeAxis()).getUpperBound())
            : plot.getDataRange(plot.getRangeAxis());
    updateAxes(plot, aAxes, aGrid, domainDataRange, rangeDataRange);
}

From source file:de.mpg.mpi_inf.bioinf.netanalyzer.ui.charts.JFreeChartConn.java

/**
 * Creates a scatter plot that visualizes the given data collection.
 * // ww w  . ja va2 s .c  om
 * @param aCollection
 *            Data to be visualized.
 * @param aGeneral
 *            General visual settings to be applied.
 * @param aAxes
 *            Axis-related visual settings to be applied.
 * @param aGrid
 *            Grid-related visual settings to be applied.
 * @param aScatter
 *            Point-related visual settings to be applied.
 * @return Newly created chart control.
 */
private static JFreeChart createScatter(XYSeriesCollection aCollection, GeneralVisSettings aGeneral,
        AxesSettings aAxes, GridSettings aGrid, ScatterSettings aScatter) {

    JFreeChart chart = ChartFactory.createScatterPlot(null, // title
            convertLabel(aAxes.getDomainAxisLabel()), // label of X axis
            convertLabel(aAxes.getRangeAxisLabel()), // label of Y axis
            aCollection, // dataset
            PlotOrientation.VERTICAL, // orientation
            false, // create legend
            true, // display tooltips
            false); // generate urls
    XYPlot plot = chart.getXYPlot();
    Range domainDataRange = aAxes.getLogarithmicDomainAxis()
            ? new Range(logLowerBound(plot.getDataset(), true),
                    plot.getDataRange(plot.getDomainAxis()).getUpperBound())
            : plot.getDataRange(plot.getDomainAxis());
    Range rangeDataRange = aAxes.getLogarithmicRangeAxis()
            ? new Range(logLowerBound(plot.getDataset(), false),
                    plot.getDataRange(plot.getRangeAxis()).getUpperBound())
            : plot.getDataRange(plot.getRangeAxis());
    updateGeneral(plot, aGeneral);
    updateAxes(plot, aAxes, aGrid, domainDataRange, rangeDataRange);
    updateScatter(plot, aScatter);
    chart.setBackgroundPaint(null);
    return chart;
}

From source file:ChartUsingJava.CombinedXYPlot.java

/**
 * Returns the bounds of the data values that will be plotted against
 * the specified axis.//from  w  w  w  . j  a  v  a 2 s .c om
 *
 * @param axis  the axis.
 *
 * @return The bounds.
 */
public Range getDataRange(ValueAxis axis) {
    Range l_result = null;
    Iterator l_itr = getSubplots().iterator();
    while (l_itr.hasNext()) {
        XYPlot l_subplot = (XYPlot) l_itr.next();

        l_result = Range.combine(l_result, l_subplot.getDataRange(axis));
    }
    return l_result;
}

From source file:fr.amap.lidar.amapvox.chart.VoxelsToChart.java

public JFreeChart[] getVegetationProfileCharts(LayerReference reference, float maxPAD) {

    boolean inverseRangeAxis;

    inverseRangeAxis = !(reference == LayerReference.FROM_ABOVE_GROUND);

    int quadratNumber = getQuadratNumber(split, length);

    JFreeChart[] charts = new JFreeChart[quadratNumber];

    for (int i = 0; i < quadratNumber; i++) {

        XYSeriesCollection dataset = new XYSeriesCollection();

        for (VoxelFileChart voxelFile : voxelFiles) {

            int[] indices = getIndiceRange(voxelFile, i);

            XYSeries serie = createVegetationProfileSerie(voxelFile.reader, voxelFile.label, indices[0],
                    indices[1], reference, maxPAD);
            dataset.addSeries(serie);//from w w w.  j  av  a2  s  .  com
        }

        List<XYSeries> series = dataset.getSeries();

        double correlationValue = Double.NaN;

        if (series.size() == 2) {

            XYSeries firstSerie = series.get(0);
            XYSeries secondSerie = series.get(1);

            Map<Double, Double[]> valuesMap = new HashMap<>();

            for (int j = 0; j < firstSerie.getItemCount(); j++) {

                Double[] value = new Double[] { firstSerie.getDataItem(j).getXValue(), Double.NaN };
                valuesMap.put(firstSerie.getDataItem(j).getYValue(), value);
            }

            for (int j = 0; j < secondSerie.getItemCount(); j++) {

                Double[] value = valuesMap.get(Double.valueOf(secondSerie.getDataItem(j).getYValue()));
                if (value == null) {
                    valuesMap.put(secondSerie.getDataItem(j).getYValue(),
                            new Double[] { Double.NaN, secondSerie.getDataItem(j).getXValue() });
                } else if (Double.isNaN(value[1])) {
                    value[1] = secondSerie.getDataItem(j).getXValue();
                    valuesMap.put(secondSerie.getDataItem(j).getYValue(), value);
                }
            }

            List<Double> firstList = new ArrayList<>();
            List<Double> secondList = new ArrayList<>();

            Iterator<Map.Entry<Double, Double[]>> iterator = valuesMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Double, Double[]> next = iterator.next();
                Double[] value = next.getValue();

                if (!Double.isNaN(value[0]) && !Double.isNaN(value[1])) {
                    firstList.add(value[0]);
                    secondList.add(value[1]);
                }
            }

            double[] firstArray = new double[firstList.size()];
            double[] secondArray = new double[secondList.size()];

            for (int j = 0; j < firstList.size(); j++) {
                firstArray[j] = firstList.get(j);
                secondArray[j] = secondList.get(j);
            }

            PearsonsCorrelation correlation = new PearsonsCorrelation();
            correlationValue = correlation.correlation(firstArray, secondArray);
        }

        charts[i] = createChart("Vegetation profile" + " - quadrat " + (i + 1), dataset, "PAD",
                reference.getLabel());
        if (!Double.isNaN(correlationValue)) {
            charts[i].addSubtitle(
                    new TextTitle("R2 = " + (Math.round(Math.pow(correlationValue, 2) * 100)) / 100.0));
        }

        ((XYPlot) charts[i].getPlot()).getRangeAxis().setInverted(inverseRangeAxis);
    }

    //set quadrats ranges

    double minX = 0;
    double maxX = 0;
    double minY = 0;
    double maxY = 0;

    int id = 0;
    for (JFreeChart chart : charts) {

        XYPlot plot = (XYPlot) chart.getPlot();
        Range rangeOfRangeAxis = plot.getDataRange(plot.getRangeAxis());
        Range rangeOfDomainAxis = plot.getDataRange(plot.getDomainAxis());

        double currentMinY = rangeOfRangeAxis.getLowerBound();
        double currentMaxY = rangeOfRangeAxis.getUpperBound();
        double currentMinX = rangeOfDomainAxis.getLowerBound();
        double currentMaxX = rangeOfDomainAxis.getUpperBound();

        if (id == 0) {
            minX = currentMinX;
            maxX = currentMaxX;
            minY = currentMinY;
            maxY = currentMaxY;
        } else {

            if (currentMinX < minX) {
                minX = currentMinX;
            }
            if (currentMaxX > maxX) {
                maxX = currentMaxX;
            }
            if (currentMinY < minY) {
                minY = currentMinY;
            }
            if (currentMaxY > maxY) {
                maxY = currentMaxY;
            }
        }

        id++;
    }

    for (JFreeChart chart : charts) {

        XYPlot plot = (XYPlot) chart.getPlot();

        plot.getDomainAxis().setRange(minX, maxX);
        plot.getRangeAxis().setRange(minY, maxY);
    }

    return charts;
}

From source file:com.ivli.roim.controls.ChartControl.java

private ValueMarker findMarker(MouseEvent e) {
    final XYPlot plot = getChart().getXYPlot();

    Collection mark = plot.getDomainMarkers(Layer.FOREGROUND);

    if (null == mark || mark.isEmpty())
        return null;

    final double domainX = plot.getDomainAxis().java2DToValue(e.getX(), getScreenDataArea(),
            plot.getDomainAxisEdge());/*w  w  w.ja v  a2s. c o  m*/

    final double Epsilon = plot.getDataRange(plot.getDomainAxis()).getLength() * .01d;

    for (Object o : mark) {
        if (o instanceof DomainMarker) {
            //DomainMarker m = (DomainMarker)o;
            double val = ((DomainMarker) o).getValue();
            if (val >= domainX - Epsilon && val <= domainX + Epsilon) {
                //getContentPane().setCursor(new Cursor(Cursor.HAND_CURSOR)); 
                return (ValueMarker) o;
            }
        }
    }
    return null;
}

From source file:de.unibayreuth.bayeos.goat.panels.timeseries.JPanelChart.java

/**
 * Decreases the range on the horizontal axis, centered about a Java2D x coordinate.
 * <P>/*from w  w  w  .j av a2  s . c om*/
 * The range on the x axis is multiplied by zoomFactor
 * 
 * @param x  the x coordinate in Java2D space.
 * @param zoomFactor  the zoomFactor < 1 == zoom in; else out.
 */
private void zoomHorizontal(double x, double zoomFactor) {

    JFreeChart chart = this.chartPanel.getChart();
    ChartRenderingInfo info = this.chartPanel.getChartRenderingInfo();
    if (chart.getPlot() instanceof XYPlot) {
        XYPlot hvp = (XYPlot) chart.getPlot();
        ValueAxis axis = hvp.getDomainAxis();
        if (axis != null) {
            double anchorValue = axis.java2DToValue((float) x, info.getPlotInfo().getDataArea(),
                    hvp.getDomainAxisEdge());
            if (zoomFactor < 1.0) {
                axis.resizeRange(zoomFactor, anchorValue);
            } else if (zoomFactor > 1.0) {
                Range range = hvp.getDataRange(axis);
                adjustRange(axis, range, zoomFactor, anchorValue);
            }
        }
    }
}

From source file:apidemo.PanScrollZoomDemo.java

/**
 * Decreases the range on the horizontal axis, centered about a Java2D x coordinate.
 * <P>/*from  www.ja  v a  2  s. c  om*/
 * The range on the x axis is multiplied by zoomFactor
 * 
 * @param x  the x coordinate in Java2D space.
 * @param zoomFactor  the zoomFactor < 1 == zoom in; else out.
 */
private void zoomHorizontal(final double x, final double zoomFactor) {

    final JFreeChart chart = this.chartPanel.getChart();
    final ChartRenderingInfo info = this.chartPanel.getChartRenderingInfo();
    if (chart.getPlot() instanceof XYPlot) {
        final XYPlot hvp = (XYPlot) chart.getPlot();
        final ValueAxis axis = hvp.getDomainAxis();
        if (axis != null) {
            final double anchorValue = axis.java2DToValue((float) x, info.getPlotInfo().getDataArea(),
                    hvp.getDomainAxisEdge());
            if (zoomFactor < 1.0) {
                axis.resizeRange(zoomFactor, anchorValue);
            } else if (zoomFactor > 1.0) {
                final Range range = hvp.getDataRange(axis);
                adjustRange(axis, range, zoomFactor, anchorValue);
            }
        }
    }
}

From source file:de.unibayreuth.bayeos.goat.panels.timeseries.JPanelChart.java

public void chartChanged(ChartChangeEvent event) {
    try {// w w w  .j av  a2  s .co  m
        if (event.getChart() == null) {
            return;
        }

        BoundedRangeModel scrollBarModel = this.chartScrollBar.getModel();
        if (scrollBarModel == null) {
            return;
        }

        boolean chartIsZoomed = false;

        Plot plot = event.getChart().getPlot();
        if (plot instanceof XYPlot) {
            XYPlot hvp = (XYPlot) plot;
            ValueAxis xAxis = hvp.getDomainAxis();
            Range xAxisRange = xAxis.getRange();

            // avoid recursion
            scrollBarModel.removeChangeListener(this);

            int low = (int) (xAxisRange.getLowerBound() * scrollFactor);
            scrollBarModel.setValue(low);
            int ext = (int) (xAxisRange.getUpperBound() * scrollFactor - low);
            scrollBarModel.setExtent(ext);

            // restore
            scrollBarModel.addChangeListener(this);

            // check if zoomed horizontally
            //Range hdr = hvp.getHorizontalDataRange(xAxis);
            Range hdr = hvp.getDataRange(xAxis);

            double len = hdr == null ? 0 : hdr.getLength();
            chartIsZoomed |= xAxisRange.getLength() < len;
        }

        if (!chartIsZoomed && plot instanceof XYPlot) {
            // check if zoomed vertically
            XYPlot vvp = (XYPlot) plot;
            ValueAxis yAxis = vvp.getRangeAxis();
            if (yAxis != null) {
                chartIsZoomed = yAxis.getLowerBound() > yMin || yAxis.getUpperBound() < yMax;
            }
        }

        // enable "zoom-out-buttons" if chart is zoomed
        // otherwise disable them
        chartPanButton.setEnabled(chartIsZoomed);
        chartZoomOutButton.setEnabled(chartIsZoomed);
        chartFitButton.setEnabled(chartIsZoomed);
        chartScrollBar.setEnabled(chartIsZoomed);
        if (!chartIsZoomed) {
            setPanMode(false);
            chartZoomButton.setSelected(true);
        }
    } catch (Exception e) {
        MsgBox.error(e.getMessage());
    }
}

From source file:de.unibayreuth.bayeos.goat.panels.timeseries.JPanelChart.java

public void mouseDragged(MouseEvent event) {
    try {//from w w  w. jav a 2  s .  c  om
        if (this.panStartPoint != null) {
            Rectangle2D scaledDataArea = this.chartPanel.getScaledDataArea();

            this.panStartPoint = RefineryUtilities.getPointInRectangle(this.panStartPoint.getX(),
                    this.panStartPoint.getY(), scaledDataArea);
            Point2D panEndPoint = RefineryUtilities.getPointInRectangle(event.getX(), event.getY(),
                    scaledDataArea);

            // horizontal pan

            Plot plot = this.chartPanel.getChart().getPlot();
            if (plot instanceof XYPlot) {
                XYPlot hvp = (XYPlot) plot;
                ValueAxis xAxis = hvp.getDomainAxis();

                if (xAxis != null) {
                    double translatedStartPoint = xAxis.java2DToValue((float) panStartPoint.getX(),
                            scaledDataArea, hvp.getDomainAxisEdge());
                    double translatedEndPoint = xAxis.java2DToValue((float) panEndPoint.getX(), scaledDataArea,
                            hvp.getDomainAxisEdge());
                    double dX = translatedStartPoint - translatedEndPoint;

                    double oldMin = xAxis.getLowerBound();
                    double newMin = oldMin + dX;

                    double oldMax = xAxis.getUpperBound();
                    double newMax = oldMax + dX;

                    // do not pan out of range
                    if (newMin >= hvp.getDataRange(xAxis).getLowerBound()
                            && newMax <= hvp.getDataRange(xAxis).getUpperBound()) {
                        xAxis.setLowerBound(newMin);
                        xAxis.setUpperBound(newMax);
                    }
                }
            }

            // vertical pan (1. Y-Axis)

            if (plot instanceof XYPlot) {
                XYPlot vvp = (XYPlot) plot;
                ValueAxis yAxis = vvp.getRangeAxis();

                if (yAxis != null) {
                    double translatedStartPoint = yAxis.java2DToValue((float) panStartPoint.getY(),
                            scaledDataArea, vvp.getRangeAxisEdge());
                    double translatedEndPoint = yAxis.java2DToValue((float) panEndPoint.getY(), scaledDataArea,
                            vvp.getRangeAxisEdge());
                    double dY = translatedStartPoint - translatedEndPoint;

                    double oldMin = yAxis.getLowerBound();
                    double newMin = oldMin + dY;

                    double oldMax = yAxis.getUpperBound();
                    double newMax = oldMax + dY;

                    // do not pan out of range
                    if (newMin >= yMin && newMax <= yMax) {
                        yAxis.setLowerBound(newMin);
                        yAxis.setUpperBound(newMax);
                    }
                }
            }

            // for the next time
            this.panStartPoint = panEndPoint;
        }
    } catch (Exception e) {
        MsgBox.error(e.getMessage());
    }
}

From source file:apidemo.PanScrollZoomDemo.java

/**
 * Handles a {@link ChartChangeEvent}./*  w ww.  ja v  a 2  s .c  o  m*/
 * 
 * @param event  the event.
 */
public void chartChanged(final ChartChangeEvent event) {
    try {
        if (event.getChart() == null) {
            return;
        }

        final BoundedRangeModel scrollBarModel = this.scrollBar.getModel();
        if (scrollBarModel == null) {
            return;
        }

        boolean chartIsZoomed = false;

        final Plot plot = event.getChart().getPlot();
        if (plot instanceof XYPlot) {
            final XYPlot hvp = (XYPlot) plot;
            final ValueAxis xAxis = hvp.getDomainAxis();
            final Range xAxisRange = xAxis.getRange();

            // avoid recursion
            scrollBarModel.removeChangeListener(this);

            final int low = (int) (xAxisRange.getLowerBound() * this.scrollFactor);
            scrollBarModel.setValue(low);
            final int ext = (int) (xAxisRange.getUpperBound() * this.scrollFactor - low);
            scrollBarModel.setExtent(ext);

            // restore
            scrollBarModel.addChangeListener(this);

            // check if zoomed horizontally
            //Range hdr = hvp.getHorizontalDataRange(xAxis);
            final Range hdr = hvp.getDataRange(xAxis);

            final double len = hdr == null ? 0 : hdr.getLength();
            chartIsZoomed |= xAxisRange.getLength() < len;
        }

        if (!chartIsZoomed && plot instanceof XYPlot) {
            // check if zoomed vertically
            final XYPlot vvp = (XYPlot) plot;
            ValueAxis yAxis = vvp.getRangeAxis();
            if (yAxis != null) {
                chartIsZoomed = yAxis.getLowerBound() > this.primYMinMax[0]
                        || yAxis.getUpperBound() < this.primYMinMax[1];

                // right y-axis
                if (!chartIsZoomed && plot instanceof XYPlot) {
                    final XYPlot xyPlot = (XYPlot) plot;
                    yAxis = xyPlot.getRangeAxis(1);
                    if (yAxis != null) {
                        chartIsZoomed = yAxis.getLowerBound() > this.secondYMinMax[0]
                                || yAxis.getUpperBound() < this.secondYMinMax[1];
                    }
                }
            }
        }

        // enable "zoom-out-buttons" if chart is zoomed
        // otherwise disable them
        this.panButton.setEnabled(chartIsZoomed);
        this.zoomOutButton.setEnabled(chartIsZoomed);
        this.fitButton.setEnabled(chartIsZoomed);
        this.scrollBar.setEnabled(chartIsZoomed);
        if (!chartIsZoomed) {
            setPanMode(false);
            this.zoomButton.setSelected(true);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}