modify dataset of an already built chart with Java

2019-02-22 22:52发布

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?

1条回答
狗以群分
2楼-- · 2019-02-22 23:52

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.

查看更多
登录 后发表回答