JFreeChart初学者入门实例详细解析(一)
JFreeChart,开放源码的Java图表生成组件(基于LGPL Licence),可以创建饼图、曲线图、柱状图、甘特图,生成PNG,JPG等图片格式文件。当前最新版本为0.9.11,下面就是一张JFreeChart生成的统计图。本文讲解如何在Web利用JFreeChart生成饼状统计图。



一、安装配置

当前最新版本为0.9.11

下载jfreechart-0 .9.11.zip

下载jfreechar t-0.9.11.tar.gz

解压缩文件,jfreechart-0.9.11目录下的jfreechart-0.9.11.jar和 lib/jcommon-0.8.6.jar,lib/gnujaxp.jar都是开发运行需要的文件,把这三个文件放置到应用的lib目录下。

然后在web.xml文件中增加以下内容:

<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.
servlet.DisplayChart</servlet-class>
</servlet>


安装配置完成,现在可以写程序生成我们需要的统计图了。

二、开始,在JSP中生成饼型统计图

<%@ page contentType=
"text/html;charset=GBK"%>
<%@ page import=
"org.jfree.data.DefaultPieDataset"%>
<%@ page import=
"org.jfree.chart.JFreeChart"%>
<%@ page import=
"org.jfree.chart.plot.PiePlot"%>
<%@ page import=
"org.jfree.chart.
ChartRenderingInfo"%>
<%@ page import=
"org.jfree.chart.servlet.
ServletUtilities"%>
<%@ page import=
"org.jfree.chart.urls.
StandardPieURLGenerator"%>
<%@ page import=
"org.jfree.chart.entity.
StandardEntityCollection"%>
<%

DefaultPieDataset data 
= new DefaultPieDataset();
data.setValue("六月",500);
data.setValue("七月",580);
data.setValue("八月",828); 

PiePlot plot = new PiePlot(data);

JFreeChart chart = new JFreeChart
("",JFreeChart.DEFAULT_TITLE_FONT,
plot, true);
chart.setBackgroundPaint
(java.awt.Color.white);
//可选,设置图片背景色
chart.setTitle
("www.SenTom.net 网站访问统计表");
//可选,设置图片标题
ChartRenderingInfo info 
= new ChartRenderingInfo
(new StandardEntityCollection());
//500是图片长度,300是图片高度
String filename =
ServletUtilities.saveChartAsPNG
(chart, 500, 300, info, session);
String graphURL =
request.getContextPath() +
"/servlet/DisplayChart?filename="
+ filename; 
%>
<HTML>
<HEAD>
       <TITLE>www.sentom.net</TITLE>
</HEAD>
<BODY>
<P ALIGN="CENTER">
<ccid_file values="<%=" graphURL % />"
width=500 height=300 border=0 
usemap="#<%= filename 
%>">
</P>
</BODY>
</HTML>


就这么简单,生成的饼状图如下



三、JSP+JAVABEAN生成饼状统计图

WebChart.java 
 
/*
 * Created on 2003-9-9
 * http://www.sentom.net
 * To change the template 
 for this generated file go to
 * Window>Preferences>Java>Code
 Generation>Code and Comments
 */
package net.sentom.chart;

import java.awt.Insets;
import java.awt.Font;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;
import org.jfree.data.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.entity.*;
import org.jfree.chart.urls.*;
import org.jfree.chart.servlet.*;
import org.jfree.chart.labels.
StandardPieToolTipGenerator;

/**
 * @author sentom
 *
 * To change the template for
 this generated type comment go to
 * Window>Preferences>Java>Code
 Generation>Code and Comments
 */
public class WebChart
{
	private DefaultPieDataset data =
	new DefaultPieDataset();
	
	public void setValue
	(String key,double value)
	{
		data.setValue(key,value);
	}
	
	public String generatePieChart
	(String title, HttpSession session, 
	PrintWriter pw)
	{
		String filename = null;
		try
		{
//创建chart对象
PiePlot plot =
new PiePlot(data);
plot.setInsets
(new Insets(0, 5, 5, 5));
//在统计图片上建连结
plot.setURLGenerator
(new StandardPieURLGenerator
("link.jsp","section"));
plot.setToolTipGenerator
(new StandardPieToolTipGenerator());
JFreeChart chart = new JFreeChart
("", JFreeChart.DEFAULT_TITLE_FONT, 
plot, true);
chart.setBackgroundPaint
(java.awt.Color.white);
//设置图片的背景色
Font font = new Font
("黑体",Font.CENTER_BASELINE,20);
//设置图片标题的字体和大小
TextTitle _title =
new TextTitle(title);
_title.setFont(font);
chart.setTitle(_title);
//把生成的图片放到临时目录
ChartRenderingInfo info =
new ChartRenderingInfo(new 
StandardEntityCollection());
//500是图片长度,
300是图片高度
filename = ServletUtilities.
saveChartAsPNG
(chart, 500, 300, info, 
session);
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;
	}
}


JSP文件

<%@ page contentType=
"text/html;charset=GBK"%>
<%@ page import =
"java.io.PrintWriter" %>
<%@ page import=
"net.sentom.chart.WebChart"%>
<%

WebChart chart = new WebChart();
chart.setValue("六月",500);
chart.setValue("七月",580);
chart.setValue("八月",828);

String filename = 
chart.generatePieChart
("www.SenTom.net 网站访问统计表",
session, new 

PrintWriter(out));
String graphURL = 
request.getContextPath() +
"/servlet/DisplayChart?filename=" 
+ filename;

%>
<HTML>
<HEAD>
	<TITLE>www.sentom.net</TITLE>
</HEAD>
<BODY>
<P ALIGN="CENTER">
	<ccid_file values="<%=" graphURL % />
	" width=500 height=300 
	border=0 usemap="#<%= 

filename %>">
</P>
</BODY>
</HTML>


生成的饼状图如下



四 参考资源

http://sourceforge.net/projects/jfreechart

http://www.jfree.org/jfreechart/index.html

http://www.jfree.org/phpBB2/viewforum.php?f=3
阅读更多
个人分类: java图形
想对作者说点什么? 我来说一句

jfreechart要用的包

2008年12月01日 72KB 下载

JFreeChart Demo源代码

2012年06月26日 398KB 下载

没有更多推荐了,返回首页

不良信息举报

JFreeChart初学者入门实例详细解析(一)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭