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

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

Introduction

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

Prototype

public XYSeries getSeries(Comparable key) 

Source Link

Document

Returns a series from the collection.

Usage

From source file:visualize.Visualize.java

public static void main(String[] args) throws NotEnoughDataPointsException, IllDefinedDataPointsException {
    XYSeries seriesQ = new XYSeries("quadratic");
    XYSeries seriesL = new XYSeries("linear");
    XYSeries seriesI = new XYSeries("intepolated");

    final ArrayList<Point> pointsQ = new ArrayList<Point>();

    for (double x = -5.0; x <= 5.0; x = x + 0.5)
        pointsQ.add(new Point(new double[] { x, 2.0 * x * x * x - 10 * x * x }));

    final LinearFunction fl = new LinearFunction();
    final HigherOrderPolynomialFunction fq = new HigherOrderPolynomialFunction(3);
    final InterpolatedPolynomial<LinearFunction, HigherOrderPolynomialFunction> fi = new InterpolatedPolynomial<LinearFunction, HigherOrderPolynomialFunction>(
            new LinearFunction(), fq.copy(), 0.5);

    fl.fitFunction(pointsQ);/* w ww.j av  a  2 s  .  c om*/
    fq.fitFunction(pointsQ);
    fi.fitFunction(pointsQ);

    System.out.println(fl);
    System.out.println(fq);
    System.out.println(fi.interpolatedFunction);

    for (double x = -5.0; x <= 5.0; x = x + 0.5) {
        seriesQ.add(x, fq.predict(x));
        seriesL.add(x, fl.predict(x));
        seriesI.add(x, fi.predict(x));
    }

    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(seriesQ);
    dataset.addSeries(seriesL);
    dataset.addSeries(seriesI);

    JFreeChart chart = ChartFactory.createXYLineChart("XY Chart", "x-axis", "y-axis", dataset,
            PlotOrientation.VERTICAL, true, true, false);

    final XYPlot plot = chart.getXYPlot();
    final XYItemRenderer renderer = plot.getRenderer();
    renderer.setSeriesPaint(0, new Color(0, 0, 255));
    renderer.setSeriesStroke(0, new BasicStroke(0.5f));
    renderer.setSeriesPaint(1, new Color(255, 0, 0));
    renderer.setSeriesStroke(1, new BasicStroke(0.5f));
    renderer.setSeriesPaint(2, new Color(0, 200, 40));
    renderer.setSeriesStroke(2, new BasicStroke(1.5f));

    //chart.getXYPlot().setRenderer(new XYSplineRenderer(100));

    JPanel panel = new JPanel();
    ChartPanel chartPanel = new ChartPanel(chart);
    panel.add(chartPanel);

    JFrame frame = new JFrame();
    frame.setContentPane(panel);
    frame.validate();
    Dimension d = new Dimension(800, 500);
    frame.setSize(d);

    frame.setVisible(true);

    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("starting");

    for (int lambda = 0; lambda <= 100; ++lambda) {
        fi.setLambda(lambda / 100.0);
        fi.fitFunction(pointsQ);
        System.out.println(fi.interpolatedFunction);

        dataset.getSeries(2).clear();
        for (double x = -5.0; x <= 5.0; x = x + 0.5)
            seriesI.add(x, fi.predict(x));

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //   makeScreenshot( lambda );
    }

}

From source file:org.hxzon.demo.jfreechart.PolarChartDemo.java

private static void addValue() {
    XYSeriesCollection datasetUpdate = (XYSeriesCollection) dataset1;
    datasetUpdate.getSeries(series1Name).add(lastRadius, lastRadius / 5);
    lastRadius += 40;/*from  w w  w  .j ava  2 s  . co m*/
}

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  ww . j  a va  2s . c om*/
            }
            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:br.unicamp.cst.util.ChartViewerUtil.java

public static synchronized void updateValuesInXYLineChart(XYSeriesCollection dataset,
        List<? extends Codelet> codelets, long instant) {
    ArrayList<Codelet> tempCodeletsList = new ArrayList<Codelet>();
    tempCodeletsList.addAll(codelets);/*from  w  w  w. ja  v  a  2s . co  m*/

    synchronized (tempCodeletsList) {
        for (Codelet co : tempCodeletsList) {
            dataset.getSeries(co.getName()).add(instant, co.getActivation());
        }
    }
}

From source file:at.granul.mason.collector.ChartFileScalarDataWriter.java

public static void exportGraph(XYChartGenerator chart, String prefix, int width, int height) {
    try {/* www  . j  ava 2 s  .  c  o  m*/
        Document document = new Document(new com.lowagie.text.Rectangle(width, height));

        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream(new File(prefix + "_" + DataWriter.DF.format(new Date()) + ".pdf")));

        document.addAuthor("MASON");
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        //PdfTemplate tp = cb.createTemplate(width, height);

        //Write the chart with all datasets
        chart.addLegend();
        /*LegendTitle title = new LegendTitle(chart.getChart().getPlot());
        title.setLegendItemGraphicPadding(new org.jfree.ui.RectangleInsets(0,8,0,4));
        chart.addLegend(title);*/
        LegendTitle legendTitle = chart.getChart().getLegend();
        legendTitle.setPosition(RectangleEdge.BOTTOM);

        Graphics2D g2 = cb.createGraphics(width, height, new DefaultFontMapper());
        Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, width, height);
        chart.getChart().draw(g2, rectangle2D);
        g2.dispose();

        //PNG Output
        ChartUtilities.saveChartAsJPEG(new File(prefix + "_" + DataWriter.DF.format(new Date()) + ".png"),
                chart.getChart(), width, height);

        chart.getChart().removeLegend();
        //tp = cb.createTemplate(width, height);

        //All invisible
        final XYItemRenderer renderer = chart.getChartPanel().getChart().getXYPlot().getRenderer();
        for (int a = 0; a < chart.getSeriesCount(); a++) {
            renderer.setSeriesVisible(a, false);
        }

        final Dataset seriesDataset = chart.getSeriesDataset();
        XYSeriesCollection series = ((XYSeriesCollection) seriesDataset);
        for (int a = 0; a < chart.getSeriesCount(); a++) {
            renderer.setSeriesVisible(a, true);
            final String seriesName = series.getSeries(a).getKey() + "";
            chart.setYAxisLabel(seriesName);
            document.newPage();
            g2 = cb.createGraphics(width, height * (a + 2), new DefaultFontMapper());
            g2.translate(0, height * (a + 1));
            chart.getChart().draw(g2, rectangle2D);
            g2.dispose();

            //PNG Output
            ChartUtilities.saveChartAsJPEG(
                    new File(prefix + "_" + seriesName + DataWriter.DF.format(new Date()) + ".png"),
                    chart.getChart(), width, height);

            renderer.setSeriesVisible(a, false);
        }
        //cb.addTemplate(tp, 0, 0);

        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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  av  a  2s . com
                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: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  ww  .  j ava  2 s .  co  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: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  va 2  s . c o  m*/
            }
        }
    }

    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:playground.dgrether.analysis.scripts.Trb09Analysis.java

public static void writeMixedDeltaUtilsModeGroupChart(DgDeltaUtilsModeGroupChart deltaUtilsModeGroupChart,
        DgAvgDeltaUtilsModeQuantilesChart avgDScoreModeIncomeChartData, String mixedDeltaScoreIncomeChartFile,
        String mixedMsoDeltaScoreIncomeChartFile, String runid1, String runid2) {
    DgMixedDeltaUtilsModeGroupChart mixedDsIncomeChart = new DgMixedDeltaUtilsModeGroupChart();
    XYSeriesCollection modeChoiceDataset = deltaUtilsModeGroupChart
            .createDeltaScoreIncomeModeChoiceDataset(runid1, runid2);
    mixedDsIncomeChart.addIncomeModeChoiceDataSet(modeChoiceDataset);
    XYSeriesCollection avgScoreDataset = avgDScoreModeIncomeChartData.getDataset();
    mixedDsIncomeChart.addAvgDeltaScoreIncomeDs(avgScoreDataset);
    //      DgChartFrame frame = new DgChartFrame("test", mixedDsIncomeChart.createChart());
    DgChartWriter.writeChart(mixedDeltaScoreIncomeChartFile, mixedDsIncomeChart.createChart());

    XYSeriesCollection ds2 = new XYSeriesCollection();
    ds2.addSeries(modeChoiceDataset.getSeries(2));
    ds2.addSeries(modeChoiceDataset.getSeries(3));
    XYSeriesCollection ds3 = new XYSeriesCollection();
    ds3.addSeries(avgScoreDataset.getSeries(2));
    ds3.addSeries(avgScoreDataset.getSeries(3));
    DgMixedModeSwitcherOnlyDeltaScoreIncomeModeChoiceChart mixedSwichterOnlyDsIncomeChart = new DgMixedModeSwitcherOnlyDeltaScoreIncomeModeChoiceChart();
    mixedSwichterOnlyDsIncomeChart.addIncomeModeChoiceDataSet(ds2);
    mixedSwichterOnlyDsIncomeChart.addAvgDeltaScoreIncomeDs(ds3);
    //      DgChartFrame frame = new DgChartFrame("test", mixedSwichterOnlyDsIncomeChart.createChart());
    DgChartWriter.writeChart(mixedMsoDeltaScoreIncomeChartFile, mixedSwichterOnlyDsIncomeChart.createChart());
}

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 www  .java  2s  .  c o m*/
                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;
}