1.JFreeReport :报表解决工具
2.JFreeChart:Java 图形解决方案(Application/Applet/Servlet/Jsp)
3.JCommon :JFreeReport和JFreeChart的公共类库
4.JFreeDesigner :JFreeReport的报表设计工具
下载 地址
http://www.jfree.org/jfreechart/index.html 解压将LIB下的jfreechart-1.0.6.jar 、jcommon-1.0.10.jar、gnujaxp.jar 放到项目中即可,如果加上第三个jar(gnujaxp.jar)包有时web.xml会报错,把它去掉就好了。
我的环境 tomcat 6.0 ,eclipse 3.2. mssql
在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>/servlet/DisplayChart</url-pattern>
</servlet-mapping>
package com.report;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.jfree.data.general.DefaultPieDataset;
import com.dao.DbApplicationDAO;
/** *//**
* 处理饼状图
*
* @version1.0 2007-11-3
* @author王世清
*/
public class PieAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String year = request.getParameter("year").trim();
/** *//** ******JfreeChart中的制作柱饼图类******* */
DefaultPieDataset dataset = new DefaultPieDataset();
//DAO
DbApplicationDAO dbApplicationDAO = new DbApplicationDAO();
/** *//** 查找当年的请假每月的假期总数* */
//list 里面装的数组
List list = dbApplicationDAO.findAplicationByYear(year);
/** *//** findAplicationByYear(year)方法如下
public List findAplicationByYear(String year) {
log.debug("finding by year instances");
try {
Byte st = new Byte("1");
String queryString = "select sum(days),holidayMonth from DbApplication d where d.state="
+ st
+ " and d.holidayYear="
+ year
+ " group by d.holidayMonth order by d.holidayMonth ";
Query queryObject = getSession().createQuery(queryString);
List list = queryObject.list();
// Iterator i=list.iterator();
// while(i.hasNext()){
// Object[] row = (Object[]) i.next();
// Integer days = (Integer) row[0];
// String month= (String) row[1];
// System.out.println(days+" 个半天 "+month+" 月");
//
//
// }
return list;
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
**/
Iterator i = list.iterator();
while (i.hasNext()) {
Object[] row = (Object[]) i.next();
Integer days = (Integer) row[0];
String month = (String) row[1];
dataset.setValue(month + "月", days);
}
request.setAttribute("year", year);
request.setAttribute("dataset", dataset);
return mapping.findForward("go_pie");//转发到pie.jsp就是下面显示的JSP
}
}
饼状图辅助类
package com.report;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.servlet.ChartDeleter;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.general.DefaultPieDataset;
/** *//**
* 饼状图辅助类
*
* @version1.0 2007-11-3
* @author王世清
*/
public class ChartUtil {
public static String generatePieChart(DefaultPieDataset dataset,
String title, int width, int height, HttpSession session,
PrintWriter pw) {
String filename = null;
try {
if (session != null) {
ChartDeleter deleter = (ChartDeleter) session
.getAttribute("JFreeChart_Deleter");
session.removeAttribute("JFreeChart_Deleter");
session.setAttribute("JFreeChart_Deleter", deleter);
}
JFreeChart chart = ChartFactory.createPieChart3D(title, // chart
// title
dataset, // data
true, // include legend
true, false);
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, width, height,
info, session);
ChartUtilities.writeImageMap(pw, filename, info, true);
pw.flush();
} catch (Exception e) {
System.out.println("Exception - " + e.toString());
e.printStackTrace(System.out);
filename = "picture_error.png";
}
return filename;
}
}
显示的页面pie.jsp
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.io.PrintWriter"%>
<%@ page import="org.jfree.data.general.DefaultPieDataset"%>
<%@ page import="com.report.ChartUtil"%>
<HTML>
<HEAD>
<TITLE>员工看见的饼状图</TITLE>
</HEAD>
<BODY bgcolor="#E0F0F8">
<% String year= (String) request.getAttribute("year");
DefaultPieDataset piedataset=(DefaultPieDataset)request.getAttribute("dataset");
String p = ChartUtil.generatePieChart(piedataset,year+" 假期统计",600,300,null, new PrintWriter(out));
String p1 = request.getContextPath() + "/servlet/DisplayChart?filename=" + p;
%>
<center>
<table>
<tr valign="top" align="center">
<td>
<%@ include file="header.jsp"%>
</td>
</tr>
<tr valign="middle" align="center">
<td>
<img src="<%=p1%>" width=600 height=300 border=0 usemap="#<%=p%>">
</td>
</tr>
</table>
</center>
</BODY>
</HTML>
posted on 2007-12-06 13:29
Crying 阅读(585)
评论(0) 编辑 收藏 所属分类:
JreeChart使用