Example usage for org.jfree.data Range getLength

List of usage examples for org.jfree.data Range getLength

Introduction

In this page you can find the example usage for org.jfree.data Range getLength.

Prototype

public double getLength() 

Source Link

Document

Returns the length of the range.

Usage

From source file:org.fhcrc.cpl.viewer.gui.SpectrumChartFactory.java

protected static XYPlot createXYPlot(XYDataset dataset, Color[] colors) {
    // break up into two datasets, one for bars one for lines
    //LinkedList lines =new LinkedList();
    //LinkedList bars = new LinkedList();
    XYDataset seriesLines = new XYSeriesCollection();
    XYDataset seriesBars = new XYSeriesCollection();
    ((XYSeriesCollection) seriesBars).setIntervalWidth(0.0);

    if (dataset instanceof XYSeriesCollection) {
        while (0 < dataset.getSeriesCount()) {
            XYSeries s = ((XYSeriesCollection) dataset).getSeries(0);
            ((XYSeriesCollection) dataset).removeSeries(0);
            Comparable key = s.getKey();
            boolean lines = false;
            if (key instanceof String)
                lines = ((String) key).startsWith("-");
            if (lines)
                ((XYSeriesCollection) seriesLines).addSeries(s);
            else//  ww  w.j a va  2 s . c o  m
                ((XYSeriesCollection) seriesBars).addSeries(s);
        }
    } else {
        seriesBars = dataset;
    }

    NumberAxis axisDomain = new NumberAxis();
    axisDomain.setAutoRange(true);
    axisDomain.setAutoRangeIncludesZero(false);
    //      axisDomain.setRange(400.0, 1600.0);
    // NOTE: zooming in too far kills the chart, prevent this
    axisDomain.addChangeListener(new AxisChangeListener() {
        public void axisChanged(AxisChangeEvent event) {
            NumberAxis axis = (NumberAxis) event.getSource();
            Range range = axis.getRange();
            if (range.getLength() < 2.0) {
                //_log.info("AxisChangeListener " + range.getLength() + " " + range.toString());
                double middle = range.getLowerBound() + range.getLength() / 2.0;
                axis.setRange(new Range(middle - 1.1, middle + 1.1));
            }
        }
    });

    NumberAxis axisRange = new NumberAxis();
    axisRange.setAutoRange(true);
    axisRange.setAutoRangeIncludesZero(true);

    XYToolTipGenerator toolTipGenerator = new XYToolTipGenerator() {
        public String generateToolTip(XYDataset xyDataset, int s, int i) {
            double X = Math.round(xyDataset.getXValue(s, i) * 1000.0) / 1000.0;
            double Y = Math.round(xyDataset.getYValue(s, i) * 1000.0) / 1000.0;
            return "(" + X + ", " + Y + ")";
        }
    };

    XYBarRenderer barRenderer = new XYBarRenderer();
    //dhmay adding 2009/09/14.  As of jfree 1.0.13, shadows on by default        
    barRenderer.setShadowVisible(false);

    //dhmay adding for jfreechart 1.0.6 upgrade.  If this isn't here, we get a
    //nullPointerException in XYBarRenderer.drawItemLabel
    barRenderer.setBaseItemLabelGenerator(new NullLabelGenerator());

    barRenderer.setSeriesItemLabelsVisible(0, true);
    barRenderer.setBaseToolTipGenerator(toolTipGenerator);

    XYLineAndShapeRenderer lineRenderer = new XYLineAndShapeRenderer();
    lineRenderer.setBaseToolTipGenerator(toolTipGenerator);

    XYPlot xy = new XYPlot(null, axisDomain, axisRange, null);

    int ds = 0;
    if (seriesLines.getSeriesCount() > 0) {
        xy.setDataset(ds, seriesLines);
        xy.setRenderer(ds, lineRenderer);
        xy.mapDatasetToRangeAxis(ds, 0);
        ds++;
        for (int i = 0; i < seriesLines.getSeriesCount(); i++) {
            Comparable key = ((XYSeriesCollection) seriesLines).getSeriesKey(i);
            boolean lines = false;
            if (key instanceof String)
                lines = ((String) key).startsWith("-");
            lineRenderer.setSeriesLinesVisible(i, lines);
            lineRenderer.setSeriesShapesVisible(i, !lines);
        }
    }
    if (seriesBars.getSeriesCount() > 0) {
        xy.setDataset(ds, seriesBars);
        xy.setRenderer(ds, barRenderer);
        xy.mapDatasetToRangeAxis(ds, 0);
        ds++;
    }

    setColors(xy, colors);

    return xy;
}

From source file:org.jfree.data.Range.java

/**
 * Creates a new range by adding margins to an existing range.
 *
 * @param range  the range (<code>null</code> not permitted).
 * @param lowerMargin  the lower margin (expressed as a percentage of the
 *                     range length).//w ww .j  av a  2 s. c om
 * @param upperMargin  the upper margin (expressed as a percentage of the
 *                     range length).
 *
 * @return The expanded range.
 */
public static Range expand(Range range, double lowerMargin, double upperMargin) {
    ParamChecks.nullNotPermitted(range, "range");
    double length = range.getLength();
    double lower = range.getLowerBound() - length * lowerMargin;
    double upper = range.getUpperBound() + length * upperMargin;
    if (lower > upper) {
        lower = lower / 2.0 + upper / 2.0;
        upper = lower;
    }
    return new Range(lower, upper);
}

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

public static JFreeChart createHistogram(double[] data) {
    double bin = Math.sqrt(data.length);
    Range r = getBounds(data);
    return createHistogram(data, r.getLength() / bin);
}

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

public static JFreeChart createHistogram(double[] data, String yAxisLabel, double min, double max,
        DoubleFunction<Double> function) {
    int bin = (int) Math.sqrt(data.length);
    Range r = getBounds(data);
    return createHistogram(data, r.getLength() / bin, yAxisLabel, min, max, function);
}

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

public static double translateChartY(double chartY, Rectangle2D imageArea, JFreeChart chart) {
    XYPlot plot = chart.getXYPlot();//from  w w w.ja v a2  s  . co m
    boolean isRangeInverted = plot.getRangeAxis().isInverted();
    Range rangeSection = plot.getRangeAxis().getRange();
    if (!isRangeInverted) {
        return imageArea.getMinY()
                + (rangeSection.getUpperBound() - chartY) / rangeSection.getLength() * imageArea.getHeight();
    } else {
        return imageArea.getMinY()
                + (chartY - rangeSection.getLowerBound()) / rangeSection.getLength() * imageArea.getHeight();
    }
}

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

/**
 * /*from ww w.  j  a v a 2s .com*/
 * Domain and Range axes need to share the same unit (e.g. mm)
 * 
 * @param myChart
 * @return
 */
public static double calcWidthToHeight(ChartViewer myChart, double chartHeight) {
    makeChartResizable(myChart);

    myChart.getCanvas().draw();

    XYPlot plot = (XYPlot) myChart.getChart().getPlot();
    ChartRenderingInfo info = myChart.getRenderingInfo();
    Rectangle2D dataArea = info.getPlotInfo().getDataArea();
    Rectangle2D chartArea = info.getChartArea();

    // calc title space: will be added later to the right plot size
    double titleWidth = chartArea.getWidth() - dataArea.getWidth();
    double titleHeight = chartArea.getHeight() - dataArea.getHeight();

    // calc right plot size with axis dim.
    // real plot width is given by factor;
    double realPH = chartHeight - titleHeight;

    // ranges
    ValueAxis domainAxis = plot.getDomainAxis();
    org.jfree.data.Range x = domainAxis.getRange();
    ValueAxis rangeAxis = plot.getRangeAxis();
    org.jfree.data.Range y = rangeAxis.getRange();

    // real plot height can be calculated by
    double realPW = realPH / y.getLength() * x.getLength();

    double width = realPW + titleWidth;

    return width;
}

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

/**
 * /*from  www.  j ava 2  s . c  o  m*/
 * Domain and Range axes need to share the same unit (e.g. mm)
 * 
 * @param myChart
 * @return
 */
public static double calcWidthToHeight(ChartPanel myChart, double chartHeight) {
    makeChartResizable(myChart);
    // paint on a ghost panel
    JPanel parent = (JPanel) myChart.getParent();
    JPanel p = new JPanel();
    p.removeAll();
    p.add(myChart, BorderLayout.CENTER);
    p.setBounds(myChart.getBounds());
    myChart.paintImmediately(myChart.getBounds());
    p.removeAll();
    parent.add(myChart);

    XYPlot plot = (XYPlot) myChart.getChart().getPlot();
    ChartRenderingInfo info = myChart.getChartRenderingInfo();
    Rectangle2D dataArea = info.getPlotInfo().getDataArea();
    Rectangle2D chartArea = info.getChartArea();

    // calc title space: will be added later to the right plot size
    double titleWidth = chartArea.getWidth() - dataArea.getWidth();
    double titleHeight = chartArea.getHeight() - dataArea.getHeight();

    // calc right plot size with axis dim.
    // real plot width is given by factor;
    double realPH = chartHeight - titleHeight;

    // ranges
    ValueAxis domainAxis = plot.getDomainAxis();
    org.jfree.data.Range x = domainAxis.getRange();
    ValueAxis rangeAxis = plot.getRangeAxis();
    org.jfree.data.Range y = rangeAxis.getRange();

    // real plot height can be calculated by
    double realPW = realPH / y.getLength() * x.getLength();

    double width = realPW + titleWidth;

    return width;
}

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

/**
 * Returns dimensions for limiting factor width or height
 * //ww w. j ava  2 s .co m
 * @param myChart
 * @return
 */
public static Dimension calcMaxSize(ChartViewer myChart, double chartWidth, double chartHeight) {
    makeChartResizable(myChart);
    // paint on a ghost panel
    myChart.getCanvas().draw();

    XYPlot plot = (XYPlot) myChart.getChart().getPlot();
    ChartRenderingInfo info = myChart.getRenderingInfo();
    Rectangle2D dataArea = info.getPlotInfo().getDataArea();
    Rectangle2D chartArea = info.getChartArea();

    // calc title space: will be added later to the right plot size
    double titleWidth = chartArea.getWidth() - dataArea.getWidth();
    double titleHeight = chartArea.getHeight() - dataArea.getHeight();

    // calculatig width for max height

    // calc right plot size with axis dim.
    // real plot width is given by factor;
    double realPH = chartHeight - titleHeight;

    // ranges
    ValueAxis domainAxis = plot.getDomainAxis();
    org.jfree.data.Range x = domainAxis.getRange();
    ValueAxis rangeAxis = plot.getRangeAxis();
    org.jfree.data.Range y = rangeAxis.getRange();

    // real plot height can be calculated by
    double realPW = realPH / y.getLength() * x.getLength();

    double width = realPW + titleWidth;
    // if width is higher than given chartWidth then calc height for chartWidth
    if (width > chartWidth) {
        // calc right plot size with axis dim.
        // real plot width is given by factor;
        realPW = chartWidth - titleWidth;

        // real plot height can be calculated by
        realPH = realPW / x.getLength() * y.getLength();

        double height = realPH + titleHeight;
        // Return size
        return new Dimension((int) chartWidth, (int) height);
    } else {
        // Return size
        return new Dimension((int) width, (int) chartHeight);
    }
}

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

/**
 * Returns dimensions for limiting factor width or height
 * /*from ww w  . j av  a  2s.  c om*/
 * @param myChart
 * @return
 */
public static Dimension calcMaxSize(ChartPanel myChart, double chartWidth, double chartHeight) {
    makeChartResizable(myChart);
    // paint on a ghost panel
    JPanel parent = (JPanel) myChart.getParent();
    JPanel p = new JPanel();
    p.removeAll();
    p.add(myChart, BorderLayout.CENTER);
    p.setBounds(myChart.getBounds());
    myChart.paintImmediately(myChart.getBounds());
    p.removeAll();
    parent.add(myChart);

    XYPlot plot = (XYPlot) myChart.getChart().getPlot();
    ChartRenderingInfo info = myChart.getChartRenderingInfo();
    Rectangle2D dataArea = info.getPlotInfo().getDataArea();
    Rectangle2D chartArea = info.getChartArea();

    // calc title space: will be added later to the right plot size
    double titleWidth = chartArea.getWidth() - dataArea.getWidth();
    double titleHeight = chartArea.getHeight() - dataArea.getHeight();

    // calculatig width for max height

    // calc right plot size with axis dim.
    // real plot width is given by factor;
    double realPH = chartHeight - titleHeight;

    // ranges
    ValueAxis domainAxis = plot.getDomainAxis();
    org.jfree.data.Range x = domainAxis.getRange();
    ValueAxis rangeAxis = plot.getRangeAxis();
    org.jfree.data.Range y = rangeAxis.getRange();

    // real plot height can be calculated by
    double realPW = realPH / y.getLength() * x.getLength();

    double width = realPW + titleWidth;
    // if width is higher than given chartWidth then calc height for chartWidth
    if (width > chartWidth) {
        // calc right plot size with axis dim.
        // real plot width is given by factor;
        realPW = chartWidth - titleWidth;

        // real plot height can be calculated by
        realPH = realPW / x.getLength() * y.getLength();

        double height = realPH + titleHeight;
        // Return size
        return new Dimension((int) chartWidth, (int) height);
    } else {
        // Return size
        return new Dimension((int) width, (int) chartHeight);
    }
}

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

/**
 * Calculates the size of a chart for a given fixed plot width Domain and Range axes need to share
 * the same unit (e.g. mm)//from  w  w  w.  ja va2s.co m
 * 
 * @param chart
 * @param plotWidth
 * @return
 */
public static Dimension calcSizeForPlotWidth(ChartViewer myChart, double plotWidth, int iterations) {
    // ranges
    XYPlot plot = (XYPlot) myChart.getChart().getPlot();
    ValueAxis domainAxis = plot.getDomainAxis();
    Range x = domainAxis.getRange();
    ValueAxis rangeAxis = plot.getRangeAxis();
    Range y = rangeAxis.getRange();

    // plot height is fixed
    double plotHeight = plotWidth / x.getLength() * y.getLength();
    return calcSizeForPlotSize(myChart, plotWidth, plotHeight, iterations);
}