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

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

Introduction

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

Prototype

public int getItemCount(int series);

Source Link

Document

Returns the number of items in a series.

Usage

From source file:com.newatlanta.bluedragon.XYItemLabelGenerator.java

private static double calculateYTotal(XYDataset dataset, int series) {
    double total = 0;
    int numItems = dataset.getItemCount(series);
    for (int i = 0; i < numItems; i++)
        total += dataset.getYValue(series, i);
    return total;
}

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.//from  w  ww.ja v a2 s  .c o  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.// ww  w.j  ava 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: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  a  v  a 2  s.  c om
 *
 * @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:playground.benjamin.scenarios.zurich.analysis.charts.BkChartWriter.java

public static void writeChartDataToFile(String filename, JFreeChart chart) {
    filename += ".txt";
    try {/*from  w  w  w . ja v  a  2  s  .c o m*/
        BufferedWriter writer = IOUtils.getBufferedWriter(filename);
        try { /*read "try" as if (plot instanceof XYPlot)*/
            XYPlot xy = chart.getXYPlot();
            String yAxisLabel = xy.getRangeAxis().getLabel();

            String xAxisLabel = "";
            if (xy.getDomainAxis() != null) {
                xAxisLabel = xy.getDomainAxis().getLabel();
            }
            String header = xAxisLabel + "\t " + yAxisLabel;
            writer.write(header);
            writer.newLine();
            for (int i = 0; i < xy.getDatasetCount(); i++) {
                XYDataset xyds = xy.getDataset(i);
                for (int seriesIndex = 0; seriesIndex < xyds.getSeriesCount(); seriesIndex++) {
                    writer.newLine();
                    writer.write("Series " + "'" + xyds.getSeriesKey(seriesIndex).toString());
                    writer.write("'");
                    writer.newLine();
                    int items = xyds.getItemCount(seriesIndex);
                    for (int itemsIndex = 0; itemsIndex < items; itemsIndex++) {
                        Number xValue = xyds.getX(seriesIndex, itemsIndex);
                        Number yValue = xyds.getY(seriesIndex, itemsIndex);
                        writer.write(xValue.toString());
                        writer.write("\t");
                        writer.write(yValue.toString());
                        writer.newLine();
                    }
                }
            }
            System.out.println("Table written to : " + filename + "\n"
                    + "==================================================");

        } catch (ClassCastException e) { //else instanceof CategoryPlot
            log.info("caught class cast exception, trying to write CategoryPlot");
            CategoryPlot cp = chart.getCategoryPlot();
            String header = "CategoryRowKey \t CategoryColumnKey \t CategoryRowIndex \t CategoryColumnIndex \t Value";
            writer.write(header);
            writer.newLine();
            for (int i = 0; i < cp.getDatasetCount(); i++) {
                CategoryDataset cpds = cp.getDataset(i);
                for (int rowIndex = 0; rowIndex < cpds.getRowCount(); rowIndex++) {
                    for (int columnIndex = 0; columnIndex < cpds.getColumnCount(); columnIndex++) {
                        Number value = cpds.getValue(rowIndex, columnIndex);
                        writer.write(cpds.getRowKey(rowIndex).toString());
                        writer.write("\t");
                        writer.write(cpds.getColumnKey(columnIndex).toString());
                        writer.write("\t");
                        writer.write(Integer.toString(rowIndex));
                        writer.write("\t");
                        writer.write(Integer.toString(columnIndex));
                        writer.write("\t");
                        writer.write(value.toString());
                        writer.newLine();
                    }
                }
            }

        }
        writer.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:playground.dgrether.analysis.charts.utils.DgChartWriter.java

public static void writeChartDataToFile(String filename, JFreeChart chart) {
    filename += ".txt";
    try {/*  ww  w.  ja v a  2 s . c  om*/
        BufferedWriter writer = IOUtils.getBufferedWriter(filename);
        try { /*read "try" as if (plot instanceof XYPlot)*/
            XYPlot xy = chart.getXYPlot();
            String yAxisLabel = xy.getRangeAxis().getLabel();

            String xAxisLabel = "";
            if (xy.getDomainAxis() != null) {
                xAxisLabel = xy.getDomainAxis().getLabel();
            }
            String header = "#" + xAxisLabel + "\t " + yAxisLabel;
            writer.write(header);
            writer.newLine();
            //write the header
            writer.write("#");
            for (int i = 0; i < xy.getDatasetCount(); i++) {
                XYDataset xyds = xy.getDataset(i);
                int seriesIndex = 0;
                int maxItems = 0;
                int seriesCount = xyds.getSeriesCount();
                while (seriesIndex < seriesCount) {
                    writer.write("Series " + xyds.getSeriesKey(seriesIndex).toString());
                    if (seriesIndex < seriesCount - 1) {
                        writer.write("\t \t");
                    }
                    if (xyds.getItemCount(seriesIndex) > maxItems) {
                        maxItems = xyds.getItemCount(seriesIndex);
                    }
                    seriesIndex++;
                }
                writer.newLine();

                //write the data
                Number xValue, yValue = null;
                for (int itemsIndex = 0; itemsIndex < maxItems; itemsIndex++) {
                    for (int seriesIdx = 0; seriesIdx < seriesCount; seriesIdx++) {
                        if (seriesIdx < xyds.getSeriesCount() && itemsIndex < xyds.getItemCount(seriesIdx)) {
                            xValue = xyds.getX(seriesIdx, itemsIndex);
                            yValue = xyds.getY(seriesIdx, itemsIndex);
                            if (xValue != null && yValue != null) {
                                writer.write(xValue.toString());
                                writer.write("\t");
                                writer.write(yValue.toString());
                                if (seriesIdx < seriesCount - 1) {
                                    writer.write("\t");
                                }
                            }
                        }
                    }
                    writer.newLine();
                }
            }
        } catch (ClassCastException e) { //else instanceof CategoryPlot
            log.info("Due to a caught class cast exception, it should be a CategoryPlot");
            CategoryPlot cp = chart.getCategoryPlot();
            String header = "# CategoryRowKey \t CategoryColumnKey \t CategoryRowIndex \t CategoryColumnIndex \t Value";
            writer.write(header);
            writer.newLine();
            for (int i = 0; i < cp.getDatasetCount(); i++) {
                CategoryDataset cpds = cp.getDataset(i);
                for (int rowIndex = 0; rowIndex < cpds.getRowCount(); rowIndex++) {
                    for (int columnIndex = 0; columnIndex < cpds.getColumnCount(); columnIndex++) {
                        Number value = cpds.getValue(rowIndex, columnIndex);
                        writer.write(cpds.getRowKey(rowIndex).toString());
                        writer.write("\t");
                        writer.write(cpds.getColumnKey(columnIndex).toString());
                        writer.write("\t");
                        writer.write(Integer.toString(rowIndex));
                        writer.write("\t");
                        writer.write(Integer.toString(columnIndex));
                        writer.write("\t");
                        writer.write(value.toString());
                        writer.newLine();
                    }
                }
            }

        }
        writer.close();
        log.info("Chart data written to: " + filename);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

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 . j  av a2 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: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   w  ww.j  a  v  a 2  s.  c o  m
    }

    return lowerBound;
}

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

/**
 * Performs Gaussian fit on XYSeries//from   ww w.ja va 2  s .  co  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: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./* w w w  . j  a  va2 s.  c  om*/
 * @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;

}