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
<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.text.SimpleDateFormat;
import java.util.Date;
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.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import com.dao.DbApplicationDAO;
/** *//**
* 处理柱状图
*
* @version1.0 2007-11-3
* @author王世清
*/
public class CategoryAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String year = "";
/** *//** ******JfreeChart中的制作柱状图类******* */
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
/** *//** 查找当年的请假每月的假期总数* */
DbApplicationDAO dbApplicationDAO = new DbApplicationDAO();
String flag = (String) request.getSession().getAttribute("flag");
if ("manager".equals(flag)) {
year = request.getParameter("year");
// System.out.println("经理输入的年份是 "+ year);
} else {
SimpleDateFormat sd = new SimpleDateFormat("yyyy");
Date date = new Date();
year = sd.format(date);
}
List list = dbApplicationDAO.findAplicationByYear(year);
Iterator i = list.iterator();
while (i.hasNext()) {
Object[] row = (Object[]) i.next();
Integer days = (Integer) row[0];
String month = (String) row[1];
dataset.addValue(days, month + "月", month + "月");
}
JFreeChart chart = ChartFactory.createBarChart3D(year + " 年假期统计", year
+ " 年假期统计", "天数",
dataset, PlotOrientation.VERTICAL, // 选择水平或者垂直放
true,// 是否显示颜色类别(地区)提示
false,
false);
request.setAttribute("chart", chart);
return mapping.findForward("go_category");
}
}
显示柱状图的JSP
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page
import="org.jfree.chart.JFreeChart,org.jfree.chart.servlet.ServletUtilities"%>
<HTML>
<HEAD>
<TITLE>柱状图</TITLE>
</HEAD>
<BODY bgcolor="#E0F0F8">
<%
JFreeChart chart=(JFreeChart)request.getAttribute("chart");
String filename = ServletUtilities.saveChartAsPNG (chart, 600, 300, null, session);
String graphURL = request.getContextPath () + "/servlet/DisplayChart?filename=" + filename;
%>
<center>
<table>
<tr valign="top" align="center">
<td>
<%@ include file="header.jsp"%>
</td>
</tr>
<tr>
<td valign="middle" align="center">
<img src="<%= graphURL %>" width="500" height="300" border="0"
usemap="#<%= filename %>" alt="">
</td>
</tr>
</table>
</center>
</BODY>
</HTML>
posted on 2007-12-06 14:40
Crying 阅读(261)
评论(1) 编辑 收藏 所属分类:
JreeChart使用