使用 Java 和 JFreeChart 修改已构建图表的数据集
假设我已经使用以下代码生成了一个图表:
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;
}
现在我想修改先前插入 XYSeries 中的值,而不构建另一个图表(没有对 createXYLineChart 的其他调用),并使图表自动更新。
这怎么办?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需查看 api 即可。
有许多不同的方法,具体取决于您是否知道系列中元素的索引,或者您是否知道 x 值,但想要更改 y 值。
我假设图表将被重新绘制,因为所有方法都会抛出 SeriesChangeEvent。
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.