JFreeChart是一組功能強大、靈活易用的Java繪圖API,使用它可以生成多種通用性的報表,包括柱狀圖、餅圖、曲線圖、甘特圖等。它能夠用在Swing和Web等中制作自定義的圖表或報表,并且得到廣泛的應用。本文将通過引領讀者學習在JFreeChart中餅圖、柱狀圖和曲線圖的進階應用,來達到熟練使用JFreeChart的目的。
一. 下載下傳與環境配置
1. 下載下傳JFreeChart
JFreeChart是開放源代碼的免費軟體,但是它的支援文檔需要付費才能得到。其下載下傳位址為:http://sourceforge.net/project/showfiles.php?group_id=15494 ,目前最新版本為1.0.13。筆者下載下傳的是1.0.13最新版。
說明:1)source目錄:為jfreechart的源碼目錄;
2)lib目錄:為包目錄,我們需要關注的包為jfreechart-1.0.13.jar和jcommon-1.0.16.jar這兩個包;
3)根目錄下的jfreechart-1.0.6-demo.jar是例子程式,大家輕按兩下後可看到其中有很多例子的運作結果。
2. 環境配置
筆者的開發環境如下:
JDK:1.5
MyEclipse:5.1.1 GA
Tomcat:5.5.28
為了後續的“用JFreeChart建立基于 Web的圖表”的一個例子的展示,我們在MyEclipse中建立一個名為jfreechart的Web工程。将下載下傳的JFreeChart1.0.13下 lib目錄下的jar包拷貝到WebRoot/WEB-INF/lib目錄下。另外還需要在web.xml檔案中增加如下配置 <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>
在WebRoot目錄下建立bar目錄,用來存放柱狀圖的各個例子。首先在該目錄下建立一個sample1.jsp檔案,讓我們來看一個簡單的柱狀圖的例子,修改後的sample1.jsp的内容如下所示:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page import="com.zwlc.fpoa.util.Pager,java.util.List,com.zwlc.fpoa.forest.forestbureau.statistics.model.AnimalStatistics" %>
<%@ page import="org.jfree.chart.ChartFactory,org.jfree.chart.JFreeChart,org.jfree.chart.plot.PlotOrientation,org.jfree.chart.servlet.ServletUtilities,org.jfree.data.category.CategoryDataset,org.jfree.data.general.DatasetUtilities,org.jfree.chart.plot.*,org.jfree.chart.labels.*,org.jfree.chart.renderer.category.BarRenderer3D,java.awt.*,org.jfree.ui.*,org.jfree.chart.axis.AxisLocation"%>
<%@ include file="/common/include/header.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd ">
<html>
<head>
<title><s:text name="動物種類統計" /></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<%
String[] rowKeys = {"動物種類數目"};
Pager pager = (Pager) request.getAttribute("pager");
List list = pager.getList();
if(list.size()==0){
out.println("<script language='javascript'> alert('沒有可統計資料!'); window.history.back(-1);</script>");
}else{
double[][] data = new double[1][list.size()];
String[]columnKeys = new String[list.size()];
String name = null;
double number;
AnimalStatistics animalStatistics;
for(int i=0;i<list.size();i++){
animalStatistics = (AnimalStatistics)list.get(i);
name = animalStatistics.getTreeFarm().getName();
for(int j=0;j<columnKeys.length;j++){
if(name.equals(columnKeys[j])){
name = name+"(B)";
}
}
columnKeys[i]=name;
}
for(int i=0;i<rowKeys.length;i++){ //外循環是根據rowKeys的大小進行
for(int j=0;j<columnKeys.length;j++){ //内循環是根據columnKeys的大小來進行
animalStatistics = (AnimalStatistics)list.get(j);
number = (double)animalStatistics.getNumber().getNumber();
data[i][j]=number;
}
}
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
JFreeChart chart = ChartFactory.createBarChart3D("動物種類統計圖",
"名額參數", "數量", dataset, PlotOrientation.VERTICAL, true, true, false);
Font font = new Font("黑體", Font.TRUETYPE_FONT, 12);
chart.getTitle().setFont(font);//設定圖表标題為中文
CategoryPlot plot = chart.getCategoryPlot(); //設定網格背景顔色 plot.setBackgroundPaint(Color.white);
// 設定網格豎線顔色 plot.setDomainGridlinePaint(Color.pink);
// 設定網格橫線顔色 plot.setRangeGridlinePaint(Color.pink);
// 顯示每個柱的數值,并修改該數值的字型屬性
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
//x軸标題設定
plot.getDomainAxis().setLabelFont(font);//設定軸标題
plot.getDomainAxis().setTickLabelFont(font);//設定軸數值
//y軸标題設定
plot.getRangeAxis().setLabelFont(font);
plot.getRangeAxis().setLabelFont(font);
renderer.setBaseLegendTextFont(font);//設定rooKeys顯示中文
// 預設的數字顯示在柱子中,通過如下兩句可調整數字的顯示
//注意:此句很關鍵,若無此句,那數字的顯示會被覆寫,給人數字沒有顯示出來的問題
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
renderer.setItemLabelAnchorOffset(10D);
// 設定每個地區所包含的平行柱的之間距離 //
renderer.setItemMargin(0.3);
plot.setRenderer(renderer);
// 設定地區、銷量的顯示位置 //将下方的“肉類”放到上方 plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
// 将預設放在左邊的“銷量”放到右方 plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);
String graphURL = request.getContextPath()+ "/DisplayChart?filename=" + filename;
%>
<img src="<%=graphURL%>" width=700 height=500 #<%=filename %>">
<% }%>