我创建了一个图表,如下所示,并将值添加到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);发布于 2011-04-12 06:45:25
我也遇到过同样的问题,JFreechart应用程序接口相当笨拙,只要添加一个数据点,就会重新绘制整个图表,从而导致很大的呈现开销。
我解决这个问题的方法是实现我自己的底层模型(例如XYDataset实现),该模型知道包含它的图表何时被显示,并且仅在该图表可见时传播事件-如果该图表不可见,则该模型应该将事件的触发推迟到以后;例如
public class MyXYDataset extends AbstractXYDataset {
private boolean shown;
private boolean pendingEvent;
/**
* Called when the chart containing this dataset is being displayed
* (e.g. hook this into a selection listener that listens to tab selection events).
*/
public void setShown(boolean shown) {
this.shown = shown;
if (this.shown && this.pendingEvent) {
this.pendingEvent = false;
fireDatasetChanged();
}
}
public void addDatapoint(double x, double y) {
// TODO: Add to underlying collection.
if (this.shown) {
// Chart is currently displayed so propagate event immediately.
fireDatasetChanged();
} else {
// Chart is hidden so delay firing of event but record that we need to fire one.
this.pendingEvent = true;
}
}
}发布于 2011-04-12 07:08:20
另一种可能是设置c.setNotify(false);,这将阻止图表侦听ChartChangeEvent
http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/JFreeChart.html#setNotify(boolean
https://stackoverflow.com/questions/5627976
复制相似问题