Jfreechart – 根据更改的数据刷新图表
如果我们想要改变“实时”某些数据,我想知道如何刷新图表。 我的意思是,例如,我有一个带有TaskSeries的图表,该图表出现3年,我想将3年改为5年。 我希望图表立即改变。
是否有某种更新图表或类似的东西?
我知道您可以说“获取您的TaskSeries,进行更改并自动刷新”,但我的TaskSeries已生成,我无法轻易更改这些。 这就是为什么我想找到一种重新计算和重建整个图表的方法。
我希望我足够清楚,解释起来并不容易。
谢谢你的帮助,
约翰
TaskSeries
的add()
方法自动将SeriesChangeEvent
发送给所有已注册的侦听器,例如CategoryPlot
。 在此示例中 , DynamicTimeSeriesCollection
实现了SeriesChangeEvent
。 在这种情况下,图表的XYPlot
是一个注册的监听器。
我用XYPlot也遇到了这个问题。 我通过重置数据集找到了一种解决方法:
chart.getXYPlot().setDataset(chart.getXYPlot().getDataset());
这很疯狂,但它有效……
注意: chart.setNotify(true)
似乎什么都不做。
我有这个问题; 我是这样做的:
private void refreshChart() { jPanel_GraphicsTop.removeAll(); jPanel_GraphicsTop.revalidate(); // This removes the old chart aChart = createChart(); aChart.removeLegend(); ChartPanel chartPanel = new ChartPanel(aChart); jPanel_GraphicsTop.setLayout(new BorderLayout()); jPanel_GraphicsTop.add(chartPanel); jPanel_GraphicsTop.repaint(); // This method makes the new chart appear }
我还没有找到一种简单的方法来更新JFreeChart“live”,因为jfreechart的数据结构与我的数据结构非常不兼容。 所以我构建了一个redraw() – 我自己的方法,它从我的dataModel收集数据,构建一个JFreeChart dataModel并将图表设置为new。
这给人一种“实时”更新的感觉,也很难看。
// create a chart ChartFrame mychartframe = new ChartFrame("my charts", chart); // some other stuff ... // somewhere else in a code far far away mychartframe.getChartPanel().getChart().fireChartChanged();
与我合作的是以下内容:
//reset with new dataset chart().setDataset(dataset); repaint the ChartPanel that contains the JFreeChart chartPanel.repaint();