您的位置:首页 > 其它

JFreeChart-饼状图-初级

tianxudong2004 2007-04-19 14:47 27 查看
一 安装配置

当前最新版本为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">
<img src="<%= 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">
<img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%=

filename %>">
</P>
</BODY>
</HTML>
标签: 
章节导航