示例一,餅圖,簡單示例:
導入jar,代碼文件:

運行結果:

代碼:
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
public class JFreeChartTest
{
public static void main(String[] args)
{
DefaultPieDataset dpd=new DefaultPieDataset(); //建立一個默認的餅圖
dpd.setValue("管理人員", 25); //輸入數據
dpd.setValue("市場人員", 25);
dpd.setValue("開發人員", 45);
dpd.setValue("其他人員", 10);
JFreeChart chart=ChartFactory.createPieChart("某公司人員組織數據圖",dpd,true,true,false);
//可以查具體的API文檔,第一個參數是標題,第二個參數是一個數據集,第三個參數表示是否顯示Legend,第四個參數表示是否顯示提示,第五個參數表示圖中是否存在URL
ChartFrame chartFrame=new ChartFrame("某公司人員組織數據圖",chart);
//chart要放在Java容器組件中,ChartFrame繼承自java的Jframe類。該第一個參數的數據是放在窗口左上角的,不是正中間的標題。
chartFrame.pack(); //以合適的大小展現圖形
chartFrame.setVisible(true);//圖形是否可見
}
}
示例二,柱狀圖/直方圖
運行結果:

代碼:
package com.test.jfreechart;
import java.awt.Font;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
public class JFreeChartTest2 extends ApplicationFrame
{
public JFreeChartTest2(String title)
{
super(title);
this.setContentPane(createPanel()); //構造函數中自動創建Java的panel面板
}
public static CategoryDataset createDataset() //創建柱狀圖數據集
{
DefaultCategoryDataset dataset=new DefaultCategoryDataset();
dataset.setValue(10,"a","管理人員");
dataset.setValue(20,"b","市場人員");
dataset.setValue(40,"c","開發人員");
dataset.setValue(15,"d","其他人員");
return dataset;
}
public static JFreeChart createChart(CategoryDataset dataset) //用數據集創建一個圖表
{
JFreeChart chart=ChartFactory.createBarChart("hi", "人員分布",
"人員數量", dataset, PlotOrientation.VERTICAL, true, true, false); //創建一個JFreeChart
chart.setTitle(new TextTitle("某公司組織結構圖",new Font("宋體",Font.BOLD+Font.ITALIC,20)));//可以重新設置標題,替換“hi”標題
CategoryPlot plot=(CategoryPlot)chart.getPlot();//獲得圖標中間部分,即plot
CategoryAxis categoryAxis=plot.getDomainAxis();//獲得橫坐標
categoryAxis.setLabelFont(new Font("微軟雅黑",Font.BOLD,12));//設置橫坐標字體
return chart;
}
public static JPanel createPanel()
{
JFreeChart chart =createChart(createDataset());
return new ChartPanel(chart); //將chart對象放入Panel面板中去,ChartPanel類已繼承Jpanel
}
public static void main(String[] args)
{
JFreeChartTest2 chart=new JFreeChartTest2("某公司組織結構圖");
chart.pack();//以合適的大小顯示
chart.setVisible(true);
}
}
示例三,柱狀圖/直方圖,保存圖片文件
代碼:
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
public class JFreeChartTest3
{
public static void main(String[] args) throws Exception
{
JFreeChart chart=ChartFactory.createPieChart("某公司人員組織數據圖",getDataset(),true,true,false);
chart.setTitle(new TextTitle("某公司組織結構圖",new Font("宋體",Font.BOLD+Font.ITALIC,20)));
LegendTitle legend=chart.getLegend(0);//設置Legend
legend.setItemFont(new Font("宋體",Font.BOLD,14));
PiePlot plot=(PiePlot) chart.getPlot();//設置Plot
plot.setLabelFont(new Font("隸書",Font.BOLD,16));
OutputStream os = new FileOutputStream("company.jpeg");//圖片是文件格式的,故要用到FileOutputStream用來輸出。
ChartUtilities.writeChartAsJPEG(os, chart, 1000, 800);
//使用一個面向application的工具類,將chart轉換成JPEG格式的圖片。第3個參數是寬度,第4個參數是高度。
os.close();//關閉輸出流
}
private static DefaultPieDataset getDataset()
{
DefaultPieDataset dpd=new DefaultPieDataset(); //建立一個默認的餅圖
dpd.setValue("管理人員", 25); //輸入數據
dpd.setValue("市場人員", 25);
dpd.setValue("開發人員", 45);
dpd.setValue("其他人員", 10);
return dpd;
}
}
運行之后,刷新文件:

示例四,JSP頁面中顯示JFreeChart生成的圖
文件結構圖:

配置servlet:
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/DisplayChart</url-pattern>
</servlet-mapping>
index.jsp 代碼:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="org.jfree.data.general.DefaultPieDataset,org.jfree.chart.ChartFactory
,org.jfree.chart.JFreeChart,org.jfree.chart.servlet.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<%
DefaultPieDataset dpd = new DefaultPieDataset();
dpd.setValue("管理人員", 25);
dpd.setValue("市場人員", 25);
dpd.setValue("開發人員", 45);
dpd.setValue("其他人員", 10);
JFreeChart chart = ChartFactory.createPieChart("某公司組織結構圖",dpd, true, false, false);
String fileName = ServletUtilities.saveChartAsPNG(chart,800,600,session);
//ServletUtilities是面向web開發的工具類,返回一個字符串文件名,文件名自動生成,生成好的圖片會自動放在服務器(tomcat)的臨時文件下(temp)
String url = request.getContextPath() + "/DisplayChart?filename=" + fileName;
//根據文件名去臨時目錄下尋找該圖片,這里的/DisplayChart路徑要與配置文件里用戶自定義的<url-pattern>一致
%>
<img src="<%= url %>" width="800" height="600">
</body>
</html>
運行結果:

Ref:
http://www.open-open.com/lib/view/open1365997415828.html (入門示例)
http://www.cnblogs.com/xingyun/archive/2012/02/05/2339237.html (入門示例)
http://www.jfree.org/jfreechart/ (下載代碼和jar)
http://sourceforge.net/projects/jfreechart/files/ (下載代碼和jar)
http://langhua9527.iteye.com/blog/395244 (設置字體,顏色,等等)
