你可以用 JFreeChart 做出像 Windows 工作管理員那樣不斷變動的 CPU Usage History 曲線.
怎麼做呢?
很簡單, 其實只要把資料不斷地加進去就可以了.
以 XYSeries 來說,
1. 建立 資料元件
XYSeries series=new XYSeries("Frame");
2. 加入資料
series.add(1.0, 500); // X Y
3. 把資料串封裝成 collection
final XYSeriesCollection data = new XYSeriesCollection(series);
4. 建立水平 xy 圖形
final JFreeChart chart = ChartFactory.createXYLineChart(
"XY Series Demo", // 標題
"X", // x 軸標題
"Y", // y 軸標題
data, // 資料 collection
PlotOrientation.VERTICAL,
true,
true,
false
);
然後, 你就可以用一個 thread 不斷的加入新的資料
ex:
-----------------------------------------------------------------------
public void updateData() {
int i=0;
Random randomGenerator = new Random();
while (true) {
int randomData = randomGenerator.nextInt(100);
series.add(i++,randomData,true);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(-1);
}
series.remove(0);
}
}
------------------------------------------
動態曲線範例展示
| |
操作範例
Step 1: 把 XYSeriesDemo.java 放到適當的位置
copy XYSeriesDemo.java jfreechart-1.0.11\jfreechart-1.0.11\source\org\jfree\chart\demo
Step 2: 放置 make.bat 與 run.bat
copy XYSeriesDemo_make.bat jfreechart-1.0.11\
copy XYSeriesDemo_run.bat jfreechart-1.0.11\
Step 3: 編譯
滑鼠雙擊 XYSeriesDemo_make.bat
Step 4: 執行範例
滑鼠雙擊 XYSeriesDemo_run.bat
Enjoy
大哥不好意思~
回覆刪除如果我不要建立資料串
我需要從一個vector陣列(裡面會存資料)
裡面讀取出來跑圖~
該如何修改呢!?