Posted on 2008-09-01 00:39
橡皮人 阅读(140)
评论(0) 编辑 收藏
这里就JFreeChart写一个小小的饼图DEMO,其中的数据是从数据库中取出来的,所有代码有些比较。。。大家需要的改改就可以了。首先写饼图之前要先引入二个Jar文件:jfreechart-1.0.10.jar和jcommon-1.0.13.jar
Code如下:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ page import="org.jfree.data.general.DefaultPieDataset"%>
<%@ page import="org.jfree.chart.*"%>
<%@ page import="org.jfree.chart.plot.*"%>
<%@ page import="org.jfree.chart.servlet.ServletUtilities"%>
<%@ page import="org.jfree.chart.labels.StandardPieToolTipGenerator"%>
<%@ page import="org.jfree.chart.urls.StandardPieURLGenerator"%>
<%@ page import="org.jfree.chart.entity.StandardEntityCollection"%>
<%@ page import="java.io.*"%>
<%@ page import="com.csthit.store.dao.*" %>
<%@ page import="com.csthit.store.vo.*" %>
<html>
<head>
<%
TypeDAO typedao=new TypeDAO();
SalesDAO sales=null;
FatherType father=null;
List list=null;
list=typedao.getFatherType();
%>
<title>销售图</title>
<%
DefaultPieDataset data = new DefaultPieDataset();
//数据动态初始化
for(int i=0;i<list.size();i++) {
sales=new SalesDAO();
father=(FatherType)list.get(i);
data.setValue(father.getType(),sales.queryQuantityByType(father.getType())); //设值
}
PiePlot3D plot = new PiePlot3D(data);//生成一个3D饼图
JFreeChart chart = new JFreeChart("",JFreeChart.DEFAULT_TITLE_FONT, plot, true);
chart.setBackgroundPaint(java.awt.Color.lightGray);//可选,设置图片背景色
chart.setTitle("图书销量统计图");//可选,设置图片标题
plot.setToolTipGenerator(new StandardPieToolTipGenerator());
StandardEntityCollection sec = new StandardEntityCollection();
ChartRenderingInfo info = new ChartRenderingInfo(sec);
PrintWriter w = new PrintWriter(out);//输出MAP信息
//500是图片长度,300是图片高度
String filename = ServletUtilities.saveChartAsJPEG(chart,500,300,info,session);
ChartUtilities.writeImageMap(w,"map0",info,false);
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
%>
</head>
<body>
<center>
<img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#map0">
</center>
</body>
</html>
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> /servlet/DisplayChart </url-pattern>
</servlet-mapping>
效果显示如下:
各位晚安~