Change dataset of already plotted chart with Java and JFreeChart

Say I created a chart with the following code:

private ChartPanel createChart(){
        XYSeries series1 = new XYSeries("First");
        XYSeries series2 = new XYSeries("Second");
        XYSeries series3 = new XYSeries("Third");

        series1.add(0.0, 5.5);
        series1.add(5, 10);
        series1.add(10, 5.5);

        series2.add(0.0, 2);
        series2.add(5, 2);
        series2.add(10, 7);

        series3.add(0.0, 10);
        series3.add(5, 5);
        series3.add(10, 6);

        XYSeriesCollection dataset = new XYSeriesCollection();

        dataset.addSeries(series1);
        dataset.addSeries(series2);
        dataset.addSeries(series3);

        JFreeChart chart = ChartFactory.createXYLineChart("line chart example",
                "X", "Y", dataset, PlotOrientation.VERTICAL, true, true, false);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        return chartPanel;
    }

Now I would like to change the value previously inserted into the XYSeries without assembling another diagram (no other call to createXYLineChart) and make the diagram automatically updated.

How to do it?

+5
source share
1 answer

Just look at the api .

There are many different methods, depending on whether you know the index of the element in the series or know the value of x, but want to change the value of y. I assume that the chart will be redrawn as all methods call SeriesChangeEvent.

+1
source

All Articles