Example usage for org.jfree.data.xy XYSeriesCollection getSeriesCount

List of usage examples for org.jfree.data.xy XYSeriesCollection getSeriesCount

Introduction

In this page you can find the example usage for org.jfree.data.xy XYSeriesCollection getSeriesCount.

Prototype

@Override
public int getSeriesCount() 

Source Link

Document

Returns the number of series in the collection.

Usage

From source file:mzmatch.ipeak.align.CowCoda.java

@SuppressWarnings("unchecked")
public static void main(String args[]) {
    final String lbl_mcq = "mcq";

    try {/*from   w  ww  . ja  v  a2  s .  co  m*/
        Tool.init();

        // parse the commandline options
        final Options options = new Options();
        CmdLineParser cmdline = new CmdLineParser(options);

        // check whether we need to show the help
        cmdline.parse(args);
        if (options.help) {
            Tool.printHeader(System.out, application, version);
            cmdline.printUsage(System.out, "");
            return;
        }

        if (options.verbose) {
            Tool.printHeader(System.out, application, version);
            cmdline.printOptions();
        }

        // check the command-line parameters
        int filetype = JFreeChartTools.PDF;
        {
            if (options.ppm == -1) {
                System.err.println("[ERROR]: the ppm-value needs to be set.");
                System.exit(0);
            }
            if (options.order == -1) {
                System.err.println("[ERROR]: the order for the polynomial fit needs to be set.");
                System.exit(0);
            }
            if (options.maxrt == -1) {
                System.err.println("[ERROR]: the maximum retention time shift is not set.");
                System.exit(0);
            }

            if (options.image != null) {
                String extension = options.image.substring(options.image.lastIndexOf('.') + 1);
                if (extension.toLowerCase().equals("png"))
                    filetype = JFreeChartTools.PNG;
                else if (extension.toLowerCase().equals("pdf"))
                    filetype = JFreeChartTools.PDF;
                else {
                    System.err.println(
                            "[ERROR]: file extension of the image file needs to be either PDF or PNG.");
                    System.exit(0);
                }
            }

            // if the output directories do not exist, create them
            if (options.output != null)
                Tool.createFilePath(options.output, true);
            if (options.image != null)
                Tool.createFilePath(options.image, true);
            if (options.selection != null)
                Tool.createFilePath(options.selection, true);
        }

        // load the data
        if (options.verbose)
            System.out.println("Loading the data");
        double maxrt = 0;
        Vector<ParseResult> data = new Vector<ParseResult>();
        Vector<IPeakSet<IPeak>> matchdata = new Vector<IPeakSet<IPeak>>();
        for (String file : options.input) {
            System.out.println("- " + new File(file).getName());

            // load the mass chromatogram data
            ParseResult result = PeakMLParser.parse(new FileInputStream(file), true);
            data.add(result);

            // select the best mass chromatograms
            Vector<IPeak> selection = new Vector<IPeak>();
            for (IPeak peak : (IPeakSet<IPeak>) result.measurement) {
                maxrt = Math.max(maxrt, maxRT(peak));

                double mcq = codaDW(peak);
                peak.addAnnotation(lbl_mcq, Double.toString(mcq), Annotation.ValueType.DOUBLE);
                if (mcq >= options.codadw)
                    selection.add(peak);
            }

            // keep track of the selected mass chromatograms
            int id = options.input.indexOf(file);
            IPeakSet<IPeak> peakset = new IPeakSet<IPeak>(selection);
            peakset.setMeasurementID(id);
            for (IPeak mc : peakset)
                mc.setMeasurementID(id);
            matchdata.add(peakset);
        }

        // match the selection together
        if (options.verbose)
            System.out.println("Matching the data");
        Vector<IPeakSet<IPeak>> matches = IPeak.match((Vector) matchdata, options.ppm,
                new IPeak.MatchCompare<IPeak>() {
                    public double distance(IPeak peak1, IPeak peak2) {
                        double diff = Math.abs(peak1.getRetentionTime() - peak2.getRetentionTime());
                        if (diff > options.maxrt)
                            return -1;

                        Signal signal1 = new Signal(peak1.getSignal());
                        signal1.normalize();
                        Signal signal2 = new Signal(peak2.getSignal());
                        signal2.normalize();

                        double offset = bestOffSet(peak1, peak2, options.maxrt);
                        for (int i = 0; i < signal2.getSize(); ++i)
                            signal2.getX()[i] += offset;

                        double correlation = signal2
                                .pearsonsCorrelation(signal1)[Statistical.PEARSON_CORRELATION];
                        if (correlation < 0.5)
                            return -1;

                        // the match-function optimizes toward 0 (it's a distance)
                        return 1 - correlation;
                    }
                });

        // filter out all incomplete sets
        Vector<IPeakSet<IPeak>> valids = new Vector<IPeakSet<IPeak>>();
        for (IPeakSet<IPeak> set : matches) {
            if (set.size() < options.input.size())
                continue;
            valids.add((IPeakSet) set);
        }

        // calculate the alignment factors
        if (options.verbose)
            System.out.println("Calculating the alignment factors");
        double medians[] = new double[valids.size() + 2];
        DataFrame.Double dataframe = new DataFrame.Double(valids.size() + 2, options.input.size());

        medians[0] = 0;
        medians[medians.length - 1] = maxrt;
        for (int i = 0; i < options.input.size(); ++i) {
            dataframe.set(0, i, 0.1);
            dataframe.set(dataframe.getNrRows() - 1, i, 0);
        }

        for (int matchid = 0; matchid < valids.size(); ++matchid) {
            IPeakSet<IPeak> match = valids.get(matchid);

            // find the most central
            double offsets[][] = new double[match.size()][match.size()];
            for (int i = 0; i < match.size(); ++i)
                for (int j = i + 1; j < match.size(); ++j) {
                    offsets[i][j] = bestOffSet(match.get(i), match.get(j), options.maxrt);
                    offsets[j][i] = -offsets[i][j];
                }

            int besti = 0;
            double bestabssum = Double.MAX_VALUE;
            for (int i = 0; i < match.size(); ++i) {
                double abssum = 0;
                for (int j = 0; j < match.size(); ++j)
                    abssum += Math.abs(offsets[i][j]);
                if (abssum < bestabssum) {
                    besti = i;
                    bestabssum = abssum;
                }
            }

            for (int i = 0; i < match.size(); ++i)
                dataframe.set(matchid + 1, match.get(i).getMeasurementID(),
                        (i == besti ? 0 : offsets[i][besti]));

            medians[matchid + 1] = match.get(besti).getRetentionTime();
            dataframe.setRowName(matchid, Double.toString(match.get(besti).getRetentionTime()));
        }
        double minmedian = Statistical.min(medians);
        double maxmedian = Statistical.max(medians);

        // calculate for each profile the correction function
        PolynomialFunction functions[] = new PolynomialFunction[valids.size()];
        for (int i = 0; i < options.input.size(); ++i)
            functions[i] = PolynomialFunction.fit(options.order, medians, dataframe.getCol(i));

        // make a nice plot out of the whole thing
        if (options.verbose)
            System.out.println("Writing results");
        if (options.image != null) {
            org.jfree.data.xy.XYSeriesCollection dataset = new org.jfree.data.xy.XYSeriesCollection();
            JFreeChart linechart = ChartFactory.createXYLineChart(null, "Retention Time (seconds)", "offset",
                    dataset, PlotOrientation.VERTICAL, true, // legend
                    false, // tooltips
                    false // urls
            );

            // setup the colorkey
            Colormap colormap = new Colormap(Colormap.EXCEL);

            // get the structure behind the graph
            XYPlot plot = (XYPlot) linechart.getPlot();
            XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();

            // setup the plot area
            linechart.setBackgroundPaint(java.awt.Color.WHITE);
            linechart.setBorderVisible(false);
            linechart.setAntiAlias(true);

            plot.setBackgroundPaint(java.awt.Color.WHITE);
            plot.setDomainGridlinesVisible(true);
            plot.setRangeGridlinesVisible(true);

            // create the datasets
            for (int i = 0; i < options.input.size(); ++i) {
                org.jfree.data.xy.XYSeries series = new org.jfree.data.xy.XYSeries(dataframe.getColName(i));
                org.jfree.data.xy.XYSeries function = new org.jfree.data.xy.XYSeries(
                        dataframe.getColName(i) + "-function");
                dataset.addSeries(series);
                dataset.addSeries(function);

                renderer.setSeriesPaint(dataset.getSeriesCount() - 1, new java.awt.Color(colormap.getColor(i)));
                renderer.setSeriesPaint(dataset.getSeriesCount() - 2, new java.awt.Color(colormap.getColor(i)));

                renderer.setSeriesLinesVisible(dataset.getSeriesCount() - 2, false);
                renderer.setSeriesShapesVisible(dataset.getSeriesCount() - 2, true);

                // add the data-points
                for (int j = 0; j < valids.size(); ++j)
                    series.add(medians[j], dataframe.get(j, i));
                for (double x = minmedian; x < maxmedian; ++x)
                    function.add(x, functions[i].getY(x));
            }

            dataset.removeAllSeries();
            for (int i = 0; i < options.input.size(); ++i) {
                Function function = functions[i];

                org.jfree.data.xy.XYSeries series = new org.jfree.data.xy.XYSeries(dataframe.getColName(i));
                dataset.addSeries(series);

                renderer.setSeriesPaint(i, new java.awt.Color(colormap.getColor(i)));
                renderer.setSeriesLinesVisible(i, false);
                renderer.setSeriesShapesVisible(i, true);

                // add the data-points
                for (int j = 0; j < valids.size(); ++j)
                    series.add(medians[j], dataframe.get(j, i) - function.getY(medians[j]));
            }

            JFreeChartTools.writeAs(filetype, new FileOutputStream(options.image), linechart, 800, 500);
        }

        // save the selected
        if (options.selection != null) {
            Header header = new Header();

            // set the number of peaks to be stored
            header.setNrPeaks(valids.size());

            // create a set for the measurements
            SetInfo set = new SetInfo("", SetInfo.SET);
            header.addSetInfo(set);

            // create the measurement infos
            for (int i = 0; i < options.input.size(); ++i) {
                String file = options.input.get(i);

                // create the measurement info
                MeasurementInfo measurement = new MeasurementInfo(i, data.get(i).header.getMeasurementInfo(0));
                measurement.addFileInfo(new FileInfo(file, file));

                header.addMeasurementInfo(measurement);

                // add the file to the set
                set.addChild(new SetInfo(file, SetInfo.SET, i));
            }

            // write the data
            PeakMLWriter.write(header, (Vector) valids, null,
                    new GZIPOutputStream(new FileOutputStream(options.selection)), null);
        }

        // correct the values with the found function and save them
        for (int i = 0; i < options.input.size(); ++i) {
            Function function = functions[i];
            ParseResult result = data.get(i);

            IPeakSet<MassChromatogram<Peak>> peakset = (IPeakSet<MassChromatogram<Peak>>) result.measurement;
            for (IPeak peak : peakset)
                align(peak, function);

            File filename = new File(options.input.get(i));
            String name = filename.getName();

            PeakMLWriter.write(result.header, (Vector) peakset.getPeaks(), null,
                    new GZIPOutputStream(new FileOutputStream(options.output + "/" + name)), null);
        }
    } catch (Exception e) {
        Tool.unexpectedError(e, application);
    }
}

From source file:audio.cords.old.RegressionDemo.java

private static XYDataset regress(XYSeriesCollection data) {
    // Determine bounds
    double xMin = Double.MAX_VALUE, xMax = 0;
    for (int i = 0; i < data.getSeriesCount(); i++) {
        XYSeries ser = data.getSeries(i);
        for (int j = 0; j < ser.getItemCount(); j++) {
            double x = ser.getX(j).doubleValue();
            if (x < xMin) {
                xMin = x;//from  w w  w . ja v a  2  s .  c o m
            }
            if (x > xMax) {
                xMax = x;
            }
        }
    }
    // Create 2-point series for each of the original series
    XYSeriesCollection coll = new XYSeriesCollection();
    for (int i = 0; i < data.getSeriesCount(); i++) {
        XYSeries ser = data.getSeries(i);
        int n = ser.getItemCount();
        double sx = 0, sy = 0, sxx = 0, sxy = 0, syy = 0;
        for (int j = 0; j < n; j++) {
            double x = ser.getX(j).doubleValue();
            double y = ser.getY(j).doubleValue();
            sx += x;
            sy += y;
            sxx += x * x;
            sxy += x * y;
            syy += y * y;
        }
        double b = (n * sxy - sx * sy) / (n * sxx - sx * sx);
        double a = sy / n - b * sx / n;
        XYSeries regr = new XYSeries(ser.getKey());
        regr.add(xMin, a + b * xMin);
        regr.add(xMax, a + b * xMax);
        coll.addSeries(regr);
    }
    return coll;
}

From source file:statistic.graph.gui.Charts.java

private static void initXAxis(XYPlot plot, XYDataset dataset) {
    plot.setDomainAxis(new NumberAxis(plot.getDomainAxis().getLabel()));
    XYSeriesCollection collection = (XYSeriesCollection) dataset;
    double max = Double.NEGATIVE_INFINITY;
    double min = Double.POSITIVE_INFINITY;
    if (collection != null) {
        for (int s = 0; s < collection.getSeriesCount(); s++) {
            for (int d = 0; d < collection.getItemCount(s); d++) {
                XYDataItem data = collection.getSeries(s).getDataItem(d);
                if (data.getX().longValue() == Integer.MAX_VALUE
                        || data.getX().longValue() == Integer.MIN_VALUE) {
                    continue;
                }/*from w ww. j  a v  a  2s .  co m*/
                if (data.getX().doubleValue() > max) {
                    max = data.getX().doubleValue();
                }
                if (data.getX().doubleValue() < min) {
                    min = data.getX().doubleValue();
                }
            }
        }
        if (min < max) {
            plot.getDomainAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());
            plot.getDomainAxis().setRange(min - 0.5, max + 0.5);
            for (int s = 0; s < collection.getSeriesCount(); s++) {
                XYSeries series = collection.getSeries(s);
                if (series.indexOf(Integer.MIN_VALUE) >= 0) {
                    XYDataItem item = series.remove((Number) Integer.MIN_VALUE);
                    if (series.indexOf(min) < 0) {
                        series.add(min, item.getY());
                    }
                }
                if (series.indexOf(Integer.MAX_VALUE) >= 0) {
                    XYDataItem item = series.remove((Number) Integer.MAX_VALUE);
                    if (series.indexOf(max) < 0) {
                        series.add(max, item.getY());
                    }
                }
            }
        } else {
            plot.getDomainAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());
            plot.getDomainAxis().setRange(0 - 0.5, 1 + 0.5);
        }
    } else {
        plot.getDomainAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        plot.getDomainAxis().setRange(0 - 0.5, 1 + 0.5);
    }
}

From source file:com.griddynamics.jagger.reporting.chart.ChartHelper.java

public static Pair<String, XYSeriesCollection> adjustTime(XYSeriesCollection chartsCollection,
        Collection<IntervalMarker> markers) {
    int maxTime = 0;
    for (int i = 0; i < chartsCollection.getSeriesCount(); i++) {
        XYSeries series = chartsCollection.getSeries(i);
        for (int j = 0; j < series.getItemCount(); j++) {
            int x = series.getX(j).intValue();
            if (x > maxTime) {
                maxTime = x;/*from  w  w  w.  j a v a2  s  .c  om*/
            }
        }
    }

    String type = "ms";
    int div = 1;

    if (maxTime > 10 * 60 * 1000) {
        div = 60 * 1000;
        type = "min";
    }

    if (maxTime > 30 * 1000) {
        div = 1000;
        type = "sec";
    }

    XYSeriesCollection result = new XYSeriesCollection();

    for (int i = 0; i < chartsCollection.getSeriesCount(); i++) {

        XYSeries old = chartsCollection.getSeries(i);
        XYSeries series = new XYSeries(old.getKey(), old.getAutoSort(), old.getAllowDuplicateXValues());
        for (int j = 0; j < old.getItemCount(); j++) {
            Number x = old.getX(j).doubleValue() / div;
            Number y = old.getY(j);
            series.add(x, y);
        }

        result.addSeries(series);
    }

    if (markers != null) {
        for (IntervalMarker marker : markers) {
            marker.setStartValue(marker.getStartValue() / div);
            marker.setEndValue(marker.getEndValue() / div);
        }
    }

    return Pair.of(type, result);
}

From source file:org.gephi.ui.utils.ChartsUtils.java

/**
 * Calculates linear regression points from a XYSeriesCollection data set
 * Code obtained from http://pwnt.be/2009/08/17/simple-linear-regression-with-jfreechart
 *//*from   w w w .  j  av  a2  s .  c  o  m*/
private static XYDataset regress(XYSeriesCollection data) {
    // Determine bounds
    double xMin = Double.MAX_VALUE, xMax = 0;
    for (int i = 0; i < data.getSeriesCount(); i++) {
        XYSeries ser = data.getSeries(i);
        for (int j = 0; j < ser.getItemCount(); j++) {
            double x = ser.getX(j).doubleValue();
            if (x < xMin) {
                xMin = x;
            }
            if (x > xMax) {
                xMax = x;
            }
        }
    }
    // Create 2-point series for each of the original series
    XYSeriesCollection coll = new XYSeriesCollection();
    for (int i = 0; i < data.getSeriesCount(); i++) {
        XYSeries ser = data.getSeries(i);
        int n = ser.getItemCount();
        double sx = 0, sy = 0, sxx = 0, sxy = 0, syy = 0;
        for (int j = 0; j < n; j++) {
            double x = ser.getX(j).doubleValue();
            double y = ser.getY(j).doubleValue();
            sx += x;
            sy += y;
            sxx += x * x;
            sxy += x * y;
            syy += y * y;
        }
        double b = (n * sxy - sx * sy) / (n * sxx - sx * sx);
        double a = sy / n - b * sx / n;
        XYSeries regr = new XYSeries(ser.getKey());
        regr.add(xMin, a + b * xMin);
        regr.add(xMax, a + b * xMax);
        coll.addSeries(regr);
    }
    return coll;
}

From source file:audio.cords.old.RegressionDemo.java

private static JFreeChart createChart(XYSeriesCollection data) {
    JFreeChart chart = ChartFactory.createScatterPlot(null, "X", "Y", data, PlotOrientation.VERTICAL, true,
            false, false);/*from w  w w  .ja  v  a2 s. c  o  m*/
    XYPlot plot = (XYPlot) chart.getPlot();
    XYItemRenderer scatterRenderer = plot.getRenderer();
    StandardXYItemRenderer regressionRenderer = new StandardXYItemRenderer();
    regressionRenderer.setBaseSeriesVisibleInLegend(false);
    plot.setDataset(1, regress(data));
    plot.setRenderer(1, regressionRenderer);
    DrawingSupplier ds = plot.getDrawingSupplier();
    for (int i = 0; i < data.getSeriesCount(); i++) {
        Paint paint = ds.getNextPaint();
        scatterRenderer.setSeriesPaint(i, paint);
        regressionRenderer.setSeriesPaint(i, paint);
    }
    return chart;
}

From source file:edu.fullerton.viewerplugin.PluginSupport.java

public static int scaleRange(XYSeriesCollection mtds, Double miny, Double maxy) {
    int exp = PluginSupport.getExp(miny, maxy);
    if (exp > 0 && exp < 100) {
        int nseries = mtds.getSeriesCount();
        XYSeries[] newSeries = new XYSeries[nseries];

        double scale = Math.pow(10, exp);
        for (int s = 0; s < nseries; s++) {
            XYSeries ds = (XYSeries) mtds.getSeries(s);
            Comparable skey = mtds.getSeriesKey(s);
            XYSeries nds = new XYSeries(skey, true);
            for (int item = 0; item < ds.getItemCount(); item++) {
                double x = ds.getX(item).doubleValue();
                double y = ds.getY(item).doubleValue();

                y *= scale;//from   w  w w .  ja  v a  2s . c  om
                nds.add(x, y);
            }
            newSeries[s] = nds;
        }
        mtds.removeAllSeries();
        for (int s = 0; s < nseries; s++) {
            mtds.addSeries(newSeries[s]);
        }
    } else {
        exp = 0;
    }
    return exp;
}

From source file:audio.cords.SimplestChart.java

private static JFreeChart createChart(XYSeriesCollection data) {
    JFreeChart chart = ChartFactory.createScatterPlot(null, "X", "Y", data, PlotOrientation.VERTICAL, true,
            false, false);//from   w  w w .  ja v a  2  s. c  om
    XYPlot plot = (XYPlot) chart.getPlot();
    XYItemRenderer scatterRenderer = plot.getRenderer();

    //        plot.getDomainAxis().resizeRange(2);
    //        plot.getRangeAxis().resizeRange(2);
    plot.zoom(2);

    //StandardXYItemRenderer regressionRenderer = new StandardXYItemRenderer();
    //regressionRenderer.setBaseSeriesVisibleInLegend(false);
    //plot.setDataset(1, regress(data));
    //plot.setRenderer(1, regressionRenderer);
    DrawingSupplier ds = plot.getDrawingSupplier();
    for (int i = 0; i < data.getSeriesCount(); i++) {
        Paint paint = ds.getNextPaint();
        scatterRenderer.setSeriesPaint(i, paint);
        //regressionRenderer.setSeriesPaint(i, paint);
    }
    return chart;
}

From source file:com.griddynamics.jagger.reporting.chart.ChartHelper.java

public static JFreeChart createStackedAreaChart(String title, XYSeriesCollection areaData,
        XYSeriesCollection lineData, String xLabel, String yLabel, ColorTheme theme) {

    final ValueAxis xAxis = new NumberAxis(xLabel);
    final ValueAxis yAxis = new NumberAxis(yLabel);
    XYPlot mainPlot = new XYPlot();
    mainPlot.setDomainAxis(xAxis);/*from w  w  w .j ava2 s  .  co m*/
    mainPlot.setRangeAxis(yAxis);

    mainPlot.setForegroundAlpha(0.9f);

    //[ stacked area
    DefaultTableXYDataset areaDs = new DefaultTableXYDataset();
    for (int i = 0; i < areaData.getSeriesCount(); i++) {
        areaDs.addSeries(areaData.getSeries(i));
    }
    XYItemRenderer stackedRenderer = new StackedXYAreaRenderer2();
    mainPlot.setDataset(areaDs);
    mainPlot.setRenderer(stackedRenderer);

    Color[] colors = generateJetSpectrum(areaData.getSeriesCount());
    for (int i = 0; i < areaData.getSeriesCount(); i++) {
        stackedRenderer.setSeriesPaint(i, colors[i]);
    }
    //]

    //[ lines
    if (lineData != null) {
        XYItemRenderer lineRenderer = new StandardXYItemRenderer();
        DefaultTableXYDataset lineDs = new DefaultTableXYDataset();
        for (int i = 0; i < lineData.getSeriesCount(); i++) {
            lineDs.addSeries(lineData.getSeries(i));
        }
        mainPlot.setDataset(1, lineDs);
        mainPlot.setRenderer(1, lineRenderer);

        colors = new Color[] { Color.black, Color.red, Color.darkGray };
        for (int i = 0; i < lineData.getSeriesCount(); i++) {
            lineRenderer.setSeriesPaint(i, colors[i % colors.length]);
            lineRenderer.setSeriesStroke(i, new BasicStroke(2f));
        }
    }
    //]

    mainPlot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, mainPlot, true);

    formatColorTheme(chart, theme);

    return chart;
}

From source file:com.itemanalysis.jmetrik.graph.density.DensityPanel.java

public void updateDataset(XYSeriesCollection dataset) {
    if (showLegend && dataset.getSeriesCount() == 1) {
        chart.removeLegend();/*from  w w w .j a  v  a 2 s. c  om*/
    }
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setDataset(dataset);
    setXYPlotRenderer(plot);
}