启用/禁用 JFreeChart 的绘制
我创建了一个图表,如下所示,并且我正在向 TimeSeries 添加值(在程序中的不同位置)。 ChartPanel 实际上包含在 JTabbedPane 中,我不想重绘图表,除非它的选项卡正在显示。有什么方法可以让我表明,当新数据进入 TimeSeries 时,不应进行渲染,除非该选项卡是当前显示的选项卡?我猜测有一些调用表明数据已更新并且需要新的渲染,所以基本上我想拦截该调用,并且如果未显示选项卡则不执行任何操作,如果选项卡正在显示则让调用通过显示,并在用户切换到该选项卡时手动调用一次。这对于后台的一个 ChartPanel 来说并不是一个主要问题,但我在不同的选项卡上有几个,它开始消耗 CPU,不断更新 4-5 个图表。
sAccuracy = new TimeSeries("a");
TimeSeriesCollection dataset = new TimeSeriesCollection(sAccuracy);
JFreeChart c = ChartFactory.createTimeSeriesChart("Accuracy",
"", "Percent", dataset, false, false, false);
ChartPanel cp = new ChartPanel(c);
I have a chart created as shown below, and I am adding values to the TimeSeries (in a different location in my program). The ChartPanel is actually contained within a JTabbedPane, and I would like to not redraw the chart unless it's tab is being displayed. Is there any way for me to signal that rendering should not occur when new data comes into the TimeSeries unless that tab is the one currently being shown? I'm guessing there is some call that signals the data has been updated and a new rendering is needed, so basically I want to intercept that call and do nothing if the tab is not being shown, let the call through if the tab is being shown, and call it once manually when the user switches to that tab. This isn't a major issue with one ChartPanel in the background, but I have a few on different tabs and it's starting to eat CPU like nasty to update 4-5 charts constantly.
sAccuracy = new TimeSeries("a");
TimeSeriesCollection dataset = new TimeSeriesCollection(sAccuracy);
JFreeChart c = ChartFactory.createTimeSeriesChart("Accuracy",
"", "Percent", dataset, false, false, false);
ChartPanel cp = new ChartPanel(c);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也遇到过同样的问题,即 JFreechart API 相当笨重,每当添加单个数据点时都会简单地重新绘制整个图表,从而导致大量的渲染开销。
我解决这个问题的方法是实现我自己的底层模型(例如 XYDataset 实现),该模型知道何时显示包含它的图表,并且仅在该图表时传播事件可见 - 如果图表不可见,则模型应将事件的触发推迟到稍后;例如
I've faced the same problem whereby the JFreechart API is fairly clunky and simply repaints the entire chart whenever a single datapoint is added resulting in a large rendering overhead.
The way I've solved this is to implement my own underlying model (e.g.
XYDataset
implementation) that is aware of when the chart containing it is being displayed, and to only propagate events when that chart is visible - If the chart is not visible then the model should defer the firing of events until later; e.g.另一种可能性是设置
c.setNotify(false);
,这将阻止图表监听ChartChangeEvent
:http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart /JFreeChart.html#setNotify(布尔值)
Another possibility is to set
c.setNotify(false);
, which will prevent the chart to listen to theChartChangeEvent
:http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/JFreeChart.html#setNotify(boolean)