Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

modify dataset of an already built chart with Java and JFreeChart

Let's say I've generated 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 modify a value previously inserted in an XYSeries without build another chart (no other call to createXYLineChart), and make the chart automatically updated.

How do this?

like image 327
Heisenbug Avatar asked Nov 14 '22 18:11

Heisenbug


1 Answers

Just look at the api.

There are many different methods, depending if you know the index of the element in the series, or if you know the x-value, but want to change the y-value. I assume the chart will be redrawn since all the methods throw a SeriesChangeEvent.

like image 91
toto2 Avatar answered Mar 29 '23 23:03

toto2