Example usage for org.jfree.data.xy XYDataset getXValue

List of usage examples for org.jfree.data.xy XYDataset getXValue

Introduction

In this page you can find the example usage for org.jfree.data.xy XYDataset getXValue.

Prototype

public double getXValue(int series, int item);

Source Link

Document

Returns the x-value for an item within a series.

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// w w w . j ava2s  . 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:net.sf.maltcms.common.charts.api.overlay.AbstractChartOverlay.java

/**
 *
 * @param entity/*from   ww  w  . jav  a 2 s  .  c  o m*/
 * @param chartPanel
 * @param dataset
 * @param seriesIndex
 * @param itemIndex
 * @return
 */
public static Shape toView(Shape entity, ChartPanel chartPanel, Dataset dataset, int seriesIndex,
        int itemIndex) {
    if (dataset instanceof XYDataset) {
        XYDataset xyds = (XYDataset) dataset;
        double x1 = xyds.getXValue(seriesIndex, itemIndex);
        double y1 = xyds.getYValue(seriesIndex, itemIndex);
        AffineTransform toPosition = getModelToViewTransformXY(chartPanel, x1, y1);
        toPosition.concatenate(
                getTranslateInstance(-entity.getBounds2D().getCenterX(), -entity.getBounds2D().getCenterY()));
        return toPosition.createTransformedShape(entity);
    } else if (dataset instanceof CategoryDataset) {
        CategoryDataset cds = (CategoryDataset) dataset;
        double y1 = cds.getValue(seriesIndex, itemIndex).doubleValue();
        AffineTransform toPosition = getModelToViewTransformCategory(chartPanel, itemIndex, y1);
        toPosition.concatenate(
                getTranslateInstance(-entity.getBounds2D().getCenterX(), -entity.getBounds2D().getCenterY()));
        return toPosition.createTransformedShape(entity);
    }
    throw new IllegalArgumentException("Unsupported dataset type: " + dataset.getClass());
}

From source file:org.jfree.data.statistics.Regression.java

/**
 * Returns the parameters 'a' and 'b' for an equation y = a + bx, fitted to
 * the data using ordinary least squares regression. The result is returned
 * as a double[], where result[0] --&gt; a, and result[1] --&gt; b.
 *
 * @param data  the data./*w  w w .  j  ava 2s  .  co m*/
 * @param series  the series (zero-based index).
 *
 * @return The parameters.
 */
public static double[] getOLSRegression(XYDataset data, int series) {

    int n = data.getItemCount(series);
    if (n < 2) {
        throw new IllegalArgumentException("Not enough data.");
    }

    double sumX = 0;
    double sumY = 0;
    double sumXX = 0;
    double sumXY = 0;
    for (int i = 0; i < n; i++) {
        double x = data.getXValue(series, i);
        double y = data.getYValue(series, i);
        sumX += x;
        sumY += y;
        double xx = x * x;
        sumXX += xx;
        double xy = x * y;
        sumXY += xy;
    }
    double sxx = sumXX - (sumX * sumX) / n;
    double sxy = sumXY - (sumX * sumY) / n;
    double xbar = sumX / n;
    double ybar = sumY / n;

    double[] result = new double[2];
    result[1] = sxy / sxx;
    result[0] = ybar - result[1] * xbar;

    return result;

}

From source file:org.jfree.data.statistics.Regression.java

/**
 * Returns the parameters 'a' and 'b' for an equation y = ax^b, fitted to
 * the data using a power regression equation.  The result is returned as
 * an array, where double[0] --&gt; a, and double[1] --&gt; b.
 *
 * @param data  the data./*from   w  w w.  j  a v a 2  s. c o m*/
 * @param series  the series to fit the regression line against.
 *
 * @return The parameters.
 */
public static double[] getPowerRegression(XYDataset data, int series) {

    int n = data.getItemCount(series);
    if (n < 2) {
        throw new IllegalArgumentException("Not enough data.");
    }

    double sumX = 0;
    double sumY = 0;
    double sumXX = 0;
    double sumXY = 0;
    for (int i = 0; i < n; i++) {
        double x = Math.log(data.getXValue(series, i));
        double y = Math.log(data.getYValue(series, i));
        sumX += x;
        sumY += y;
        double xx = x * x;
        sumXX += xx;
        double xy = x * y;
        sumXY += xy;
    }
    double sxx = sumXX - (sumX * sumX) / n;
    double sxy = sumXY - (sumX * sumY) / n;
    double xbar = sumX / n;
    double ybar = sumY / n;

    double[] result = new double[2];
    result[1] = sxy / sxx;
    result[0] = Math.pow(Math.exp(1.0), ybar - result[1] * xbar);

    return result;

}

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

private static double logLowerBound(XYDataset aDataset, boolean isDomainAxis) {
    double lowerBound = 0.0;
    for (int i = 0; i < aDataset.getItemCount(0); i++) {
        double tmp = isDomainAxis ? aDataset.getXValue(0, i) : aDataset.getYValue(0, i);
        if (lowerBound == 0.0 || tmp < lowerBound)
            lowerBound = tmp;//from   ww  w  .  j  a v a2  s .  c om
    }

    return lowerBound;
}

From source file:org.jfree.data.statistics.Regression.java

/**
 * Returns the parameters 'a0', 'a1', 'a2', ..., 'an' for a polynomial 
 * function of order n, y = a0 + a1 * x + a2 * x^2 + ... + an * x^n,
 * fitted to the data using a polynomial regression equation.
 * The result is returned as an array with a length of n + 2,
 * where double[0] --&gt; a0, double[1] --&gt; a1, .., double[n] --&gt; an.
 * and double[n + 1] is the correlation coefficient R2
 * Reference: J. D. Faires, R. L. Burden, Numerische Methoden (german
 * edition), pp. 243ff and 327ff./*w w  w  .j  av  a  2s  .c o  m*/
 *
 * @param dataset  the dataset (<code>null</code> not permitted).
 * @param series  the series to fit the regression line against (the series
 *         must have at least order + 1 non-NaN items).
 * @param order  the order of the function (&gt; 0).
 *
 * @return The parameters.
 *
 * @since 1.0.14
 */
public static double[] getPolynomialRegression(XYDataset dataset, int series, int order) {
    ParamChecks.nullNotPermitted(dataset, "dataset");
    int itemCount = dataset.getItemCount(series);
    if (itemCount < order + 1) {
        throw new IllegalArgumentException("Not enough data.");
    }
    int validItems = 0;
    double[][] data = new double[2][itemCount];
    for (int item = 0; item < itemCount; item++) {
        double x = dataset.getXValue(series, item);
        double y = dataset.getYValue(series, item);
        if (!Double.isNaN(x) && !Double.isNaN(y)) {
            data[0][validItems] = x;
            data[1][validItems] = y;
            validItems++;
        }
    }
    if (validItems < order + 1) {
        throw new IllegalArgumentException("Not enough data.");
    }
    int equations = order + 1;
    int coefficients = order + 2;
    double[] result = new double[equations + 1];
    double[][] matrix = new double[equations][coefficients];
    double sumX = 0.0;
    double sumY = 0.0;

    for (int item = 0; item < validItems; item++) {
        sumX += data[0][item];
        sumY += data[1][item];
        for (int eq = 0; eq < equations; eq++) {
            for (int coe = 0; coe < coefficients - 1; coe++) {
                matrix[eq][coe] += Math.pow(data[0][item], eq + coe);
            }
            matrix[eq][coefficients - 1] += data[1][item] * Math.pow(data[0][item], eq);
        }
    }
    double[][] subMatrix = calculateSubMatrix(matrix);
    for (int eq = 1; eq < equations; eq++) {
        matrix[eq][0] = 0;
        for (int coe = 1; coe < coefficients; coe++) {
            matrix[eq][coe] = subMatrix[eq - 1][coe - 1];
        }
    }
    for (int eq = equations - 1; eq > -1; eq--) {
        double value = matrix[eq][coefficients - 1];
        for (int coe = eq; coe < coefficients - 1; coe++) {
            value -= matrix[eq][coe] * result[coe];
        }
        result[eq] = value / matrix[eq][eq];
    }
    double meanY = sumY / validItems;
    double yObsSquare = 0.0;
    double yRegSquare = 0.0;
    for (int item = 0; item < validItems; item++) {
        double yCalc = 0;
        for (int eq = 0; eq < equations; eq++) {
            yCalc += result[eq] * Math.pow(data[0][item], eq);
        }
        yRegSquare += Math.pow(yCalc - meanY, 2);
        yObsSquare += Math.pow(data[1][item] - meanY, 2);
    }
    double rSquare = yRegSquare / yObsSquare;
    result[equations] = rSquare;
    return result;
}

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

/**
 * Performs Gaussian fit on XYSeries/*from  ww  w .  ja v  a  2  s  .  c o m*/
 * 
 * @param data the data
 * @param series the series index
 * @param gMin lower bound of Gaussian fit
 * @param gMax upper bound of Gaussian fit
 * @param sigDigits number of significant digits
 * @return double[] {normFactor, mean, sigma} as a result of
 *         GaussianCurveFitter.create().fit(obs.toList())
 */
public static double[] gaussianFit(XYDataset data, int series, double gMin, double gMax) {
    // gaussian fit
    WeightedObservedPoints obs = new WeightedObservedPoints();

    for (int i = 0; i < data.getItemCount(series); i++) {
        double x = data.getXValue(series, i);
        if (x >= gMin && x <= gMax)
            obs.add(x, data.getYValue(series, i));
    }
    return fitter.fit(obs.toList());
}

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

/**
 * Extracts the visualized data from a given chart instance.
 * <p>//from   w w  w  . jav  a  2 s.co m
 * This methods extracts the default data series from the default dataset of the given chart.
 * </p>
 * 
 * @param aChart
 *            Chart to extract the data from.
 * @return Visualized data in the form of an array of points.
 */
public static Point2D.Double[] extractData(JFreeChart aChart) {
    XYDataset dataColl = aChart.getXYPlot().getDataset();
    final int n = dataColl.getItemCount(0);
    Point2D.Double[] dataPoints = new Point2D.Double[n];
    for (int i = 0; i < n; ++i) {
        dataPoints[i] = new Point2D.Double(dataColl.getXValue(0, i), dataColl.getYValue(0, i));
    }
    return dataPoints;
}

From source file:ec.util.chart.swing.Charts.java

/**
 * Finds the index of the nearest dataitem of a series to the left of the
 * point we clicked on the chart by using dichotomy.
 *
 * @param chartX Position of the click on the domain axis
 * @param begin Lower bound of current interval
 * @param end Upper bound of current interval
 * @param series Index of series in dataset
 * @param dataset Data used by the chart
 * @return Index of dataitem in the series
 *///from  w  w  w  .j a  v  a 2  s.c  o  m
public static int getNearestLeftPoint(double chartX, int begin, int end, int series,
        @Nonnull XYDataset dataset) {
    // Index of center point
    int mid = begin + (end - begin) / 2;

    // Click is totally on the left side of chart panel
    if (mid == 0) {
        return 0;
    }

    if (mid < dataset.getItemCount(series) - 1) {
        // Get positions on the domain axis
        double left = dataset.getXValue(series, mid);
        double right = dataset.getXValue(series, mid + 1);

        if (left <= chartX && right >= chartX) // We've found our target
        {
            return mid;
        } else if (left <= chartX && right <= chartX) // Our target is on the
        // right side from mid
        {
            return getNearestLeftPoint(chartX, mid + 1, end, series, dataset);
        } else // Our target is on the left side from mid
        {
            return getNearestLeftPoint(chartX, begin, mid, series, dataset);
        }
    } else // Click is totally on the right side of chart panel
    {
        return dataset.getItemCount(series) - 1;
    }
}

From source file:org.jfree.data.time.MovingAverage.java

/**
 * Creates a new {@link XYSeries} containing the moving averages of one
 * series in the <code>source</code> dataset.
 *
 * @param source  the source dataset.//from   ww w .  j a v  a  2 s .  co  m
 * @param series  the series index (zero based).
 * @param name  the name for the new series.
 * @param period  the averaging period.
 * @param skip  the length of the initial skip period.
 *
 * @return The dataset.
 */
public static XYSeries createMovingAverage(XYDataset source, int series, String name, double period,
        double skip) {

    ParamChecks.nullNotPermitted(source, "source");
    if (period < Double.MIN_VALUE) {
        throw new IllegalArgumentException("period must be positive.");
    }
    if (skip < 0.0) {
        throw new IllegalArgumentException("skip must be >= 0.0.");
    }

    XYSeries result = new XYSeries(name);

    if (source.getItemCount(series) > 0) {

        // if the initial averaging period is to be excluded, then
        // calculate the lowest x-value to have an average calculated...
        double first = source.getXValue(series, 0) + skip;

        for (int i = source.getItemCount(series) - 1; i >= 0; i--) {

            // get the current data item...
            double x = source.getXValue(series, i);

            if (x >= first) {
                // work out the average for the earlier values...
                int n = 0;
                double sum = 0.0;
                double limit = x - period;
                int offset = 0;
                boolean finished = false;

                while (!finished) {
                    if ((i - offset) >= 0) {
                        double xx = source.getXValue(series, i - offset);
                        Number yy = source.getY(series, i - offset);
                        if (xx > limit) {
                            if (yy != null) {
                                sum = sum + yy.doubleValue();
                                n = n + 1;
                            }
                        } else {
                            finished = true;
                        }
                    } else {
                        finished = true;
                    }
                    offset = offset + 1;
                }
                if (n > 0) {
                    result.add(x, sum / n);
                } else {
                    result.add(x, null);
                }
            }

        }
    }

    return result;

}