在工作中,我们常常碰到需要在网页上绘制统计图的例子。其实实现的办法有很多,我们可以亲自编写全部图形生成代码,比如用flash,或者用svg,这样做代码量很大,还容易出错,很烦的。也可以利用现成的java统计图表库。例如用jclass(http://java.quest.com/jclass/jclass.shtml),可是这玩艺却是要收费的。对于我们这种崇尚open source的程序员来说,有没有一个开源的统计图形库呢。有,它就是www.jfree.org推出的jfreechart包,它从柱形图,饼形图,到雷达图,蜡烛图等等无所不包,可以在c/s,b/s,甚至是实时环境都能一显身手。最关键的是它是open source!我在这里给出一个在网页上绘制饼形图的简单例子。
一、到www.jfree.org下载一个最新的java库,置jfreechart.jar和jcommon.jar.jar于classpath中
二、编写一个java bean,用来生成饼形图。(GSPieChart.java)
package gov.gwssi.tax.nspg.datadisposal;
import java.awt.Insets;
import java.awt.Image;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import java.text.NumberFormat;
import javax.servlet.http.HttpSession;
import org.jfree.data.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.renderer.VerticalBarRenderer;
import org.jfree.chart.renderer.StandardXYItemRenderer;
import org.jfree.chart.plot.*;
import org.jfree.chart.entity.*;
import org.jfree.chart.tooltips.*;
import org.jfree.chart.urls.*;
import org.jfree.chart.servlet.*;
import java.awt.Font;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author jagie
* @version 1.0
*/
public class GSPieChart {
public static void main(String[] args) {
Long[] testData = {
new Long(10), new Long(20), new Long(30), new Long(40)};
generatePieChart(testData, "", null, new PrintWriter(System.out), 580, 250);
}
/**
* 生成图像文件
* @param datas Long[]数组
* @param targetUrl 点饼形图上的扇形,定向到的url
* @param session httpsession
* @param pw PrintWriter
* @param w 生成的图的宽度
* @param h 生成的图的高度
* @return 生成的图像文件的路径
*/
public static String generatePieChart(Long[] datas, String targetUrl,
HttpSession session, PrintWriter pw,
int w, int h) {
String filename = null;
try {
DefaultPieDataset data = new DefaultPieDataset();
data.setValue("正常纳税人", datas[0]);
data.setValue("异常纳税人", datas[1]);
data.setValue("数据不全纳税人", datas[2]);
data.setValue("未处理纳税人", datas[3]);
// Create the chart object
Pie3DPlot plot = new Pie3DPlot(data);
plot.setInsets(new Insets(0, 5, 5, 5));
plot.setForegroundAlpha(0.6f);
plot.setSectionLabelType(plot.NO_LABELS);
plot.setURLGenerator(new StandardPieURLGenerator(targetUrl, "type"));
plot.setToolTipGenerator(new StandardPieToolTipGenerator());
JFreeChart chart = new JFreeChart(
"数据处理结果统计图", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
chart.setTitle(new TextTitle(
"数据处理结果统计图", new Font("黑体", Font.BOLD, 15)));
StandardLegend sl = (StandardLegend) chart.getLegend();
sl.setItemFont(new Font("黑体", Font.TRUETYPE_FONT, 12));
chart.setBackgroundPaint(new java.awt.Color(254, 254, 141));
//因为jfreechart把生成的png文件保存在系统的临时文件夹中,你需要在适当的
//时候调用session.removeAttribute("JFreeChart_Deleter"),这样可以保证png文
//件被删除掉
ChartRenderingInfo info = new ChartRenderingInfo(new
StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, w, h, info, session);
// Write the image map to the PrintWriter
ChartUtilities.writeImageMap(pw, filename, info);
pw.flush();
}
catch (Exception e) {
System.out.println("Exception - " + e.toString());
e.printStackTrace(System.out);
filename = "public_error_500x300.png";
}
return filename;
}
}
.........java代码.................
<%
//以下部分为图象输出
String targetUrl="../servlet/gov.gwssi.tax.nspg.
controlservlet.GSMainControlServlet?id="+sid;
Long[] datas={normals,new Long(totalAbnormal),
dataLacks,notProcesses};
String filename=GSPieChart.generatePieChart
(datas,targetUrl,session,new PrintWriter(out),580,250);
String graphURL = request.getContextPath() +
"/servlet/DisplayChart?filename=" + filename;
%> |
三、html代码
<!-- pie chart -->
<table width="580" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<ccid_file values="<%=" graphURL % />" width=580 height=250 border=0
usemap="#<%=filename %>">
</td>
</tr>
</table> |
四、在web.xml中配置jfreechart的图像生成的servlet
[pre]<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>/servlet/DisplayChart</url-pattern>
</servlet-mapping>[/pre] |
以上就是在b/s中使用jfreechart的大体步骤,在你下载的jfreechart的sample文件夹中找到jfreechart-sample.war文件中也有类似的例子,只不过复杂一些。( |