JFreeChart is a free chart library for Java that can generate a wide variety of charts for use in applications, applets and servlets.
JFreeChart API文檔分兩部分,org.jfree.chart包和org.jfree.data包
我們要畫一張圖首先做的就是:
以餅圖為例:
·準(zhǔn)備數(shù)據(jù)集->生成數(shù)據(jù)集對象org.jfree.data.general 下Interface
Dataset的實現(xiàn)類
DefaultPieDataset dpd = new DefaultPieDataset(); |
·set數(shù)據(jù)進去
不同的圖set數(shù)據(jù)的參數(shù)不同,一般參數(shù)都比較多,但都很好理解
dpd.setValue("Chinese", 108); dpd.setValue("Math", 110); dpd.setValue("English", 74); dpd.setValue("Science Department", 226); |
·使用org.jfree.chart.ChartFactory產(chǎn)生一個JFreeChart對象
createPieChart方法四個參數(shù)分別為餅圖標(biāo)題,數(shù)據(jù)集,是否產(chǎn)生圖注,鼠標(biāo)移上去是否產(chǎn)生相應(yīng)的提示信息、locale -
the locale (null not permitted),可以改變參數(shù)看效果,真的很不錯
JFreeChart jfreechart = ChartFactory.createPieChart("bulktree high-tech achievement", dpd, true, true, false); |
·利用org.jfree.chart.ChartFrame生成顯示圖的窗體
ChartFrame繼承自javax.swing.JFrame,自然有窗體標(biāo)題和JFreeChart對象
ChartFrame frame = new ChartFrame("BULKTREE HIGH-TECH ACHIEVEMENT", jfreechart); |
順便調(diào)用setVisible方法顯示
frame.pack(); frame.setVisible(true); |
完整的代碼如下:
package com.bulktree.jfreechart; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.data.general.DefaultPieDataset; public class CreatePieChartTest { public static void main(String[] args) { // 準(zhǔn)備餅圖數(shù)據(jù)集 DefaultPieDataset dpd = new DefaultPieDataset(); dpd.setValue("Chinese", 108); dpd.setValue("Math", 110); dpd.setValue("English", 74); dpd.setValue("Science Department", 226); /** * 利用chart工廠產(chǎn)生JFreeChart對象 * createPieChart四個參數(shù)餅圖標(biāo)題,數(shù)據(jù)集,是否產(chǎn)生圖注,鼠標(biāo)移上去是否產(chǎn)生相應(yīng)的提示信息、locale - the locale (null not permitted). */ JFreeChart jfreechart = ChartFactory.createPieChart("bulktree high-tech achievement", dpd, true, true, false);
// 產(chǎn)生3d餅圖 // JFreeChart jfreechart = ChartFactory.createPieChart3D("bulktree high-tech achievement", dpd, // true, true, false);
ChartFrame frame = new ChartFrame("BULKTREE HIGH-TECH ACHIEVEMENT", jfreechart); frame.pack(); frame.setVisible(true); } } |