2011-04-11 42 views
0

我有一個圖表,如下所示,我將值添加到TimeSeries(在我的程序中的不同位置)。 ChartPanel實際上包含在一個JTabbedPane中,我不想重繪圖表,除非它的選項卡正在顯示。除非該選項卡是當前顯示的選項卡,否則當有新數據進入TimeSeries時,是否有任何方式可以表示不應該發生渲染?我猜是有一些調用信號表示數據已經更新,需要一個新的渲染,所以基本上我想攔截該調用,如果該選項卡沒有顯示,則不執行任何操作,如果該選項卡正在調用顯示,並在用戶切換到該選項卡時手動調用一次。這對於後臺的一個ChartPanel來說並不是什麼大問題,但是我有一些不同的選項卡,它開始吃像討厭的CPU來不斷更新4-5個圖表。啓用/禁用繪製JFreeChart

sAccuracy = new TimeSeries("a"); 
    TimeSeriesCollection dataset = new TimeSeriesCollection(sAccuracy); 
    JFreeChart c = ChartFactory.createTimeSeriesChart("Accuracy", 
      "", "Percent", dataset, false, false, false); 

    ChartPanel cp = new ChartPanel(c); 

回答

1

我已經面臨同樣的問題,由此JFreeChart的API是相當笨重,簡單地重畫每當加入一個單一的數據點產生大的渲染開銷整個圖表。

我已經解決了這個問題的方法是實現自己的底層模型(如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; 
    } 
    } 
}