JFreeChart1.0.6的一个通用Web Demo
这是我自己做的一个关于统计的WebDemo;
首先建立一个web项目;在src中加入以下java文件:
GenerateWebBarChart3D.java(用于Bar3D图的显示的) GenerateWebPieChart3D.java(用于Pie3D图显示的) IOpLoggerChart.java(调用方法接口文件)OpLoggerChartImpl.java(调用实现)
java 代码
- /**
- * Project name: RCP
- *
- * Package name: com.sclh.rsp.registercenter.hibernate.dao.chat
- * Filename : LoggerChat.java
- * @Author : fhway
- * DateTime : 2007-10-18 上午10:26:02
- * Visoin : 1.0
- * Company : sclh
- * Copyright (c) 2007
- *
- * 说明:日志类型分类,图的长度和宽度采取Ioc的方式注入
- * 可以统计的类别有
- * 1. 日志数据库操作类型
- * 2. 时间段访问量(以月为单位,当前一年)
- * sample HQL="select t.dbtype, count(t.dbtype) from oplogger t group by(t.dbtype) order by to_number(t.dbtype)"
- */
- package com.sclh.rsp.registercenter.service.chart;
- import java.io.PrintWriter;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import javax.servlet.http.HttpSession;
- import org.hibernate.HibernateException;
- import org.hibernate.criterion.Projections;
- import org.jfree.data.category.CategoryDataset;
- import org.jfree.data.category.DefaultCategoryDataset;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.sclh.rsp.registercenter.hibernate.dao.IOpLogger;
- import com.sclh.rsp.registercenter.hibernate.vo.OpLogger;
- import com.sclh.rsp.registercenter.hibernate.vo.OpLoggerDbType;
- /**
- * @author fhway
- *
- */
- public class OpLoggerChartImpl implements IOpLoggerChart{
- /**
- * 定义输出图片的长度和宽度
- */
- private int width;
- private int height;
- /*DAO方法 */
- private IOpLogger opLoggerDAO;
- /* 图标数据 */
- private Map map;
- public OpLoggerChartImpl(){
- map = new HashMap();
- if(width == 0 ){ width = 500;}
- if(height == 0){ height = 400;}
- }
- /**
- * 获取图标数据
- * @return
- * @throws Exception
- * fhway 2007-10-18 上午10:49:55
- */
- private Map getLoggerDBTypeDataMap() throws Exception{
- Map map = new HashMap();
- List ll = opLoggerDAO.getCriteria(OpLogger.class)
- .setProjection(Projections.projectionList()
- .add(Projections.property("dbtype"))
- .add(Projections.count("dbtype"))
- .add(Projections.groupProperty("dbtype")))
- .list();
- Iterator it = ll.iterator();
- while(it.hasNext()){
- Object[] object = (Object[]) it.next();
- OpLoggerDbType odb = (OpLoggerDbType)object[0];
- map.put(odb.getName(), object[1].toString());
- }
- return map;
- }
- /**
- * 获取一个演示用的简单数据集对象
- * @return
- * @throws Exception
- * @throws HibernateException
- */
- private CategoryDataset getLoggerDBTypeDataDataSet() throws Exception {
- DefaultCategoryDataset dataset = new DefaultCategoryDataset();
- List ll = opLoggerDAO.getCriteria(OpLogger.class)
- .setProjection(Projections.projectionList()
- .add(Projections.property("dbtype"))
- .add(Projections.count("dbtype"))
- .add(Projections.groupProperty("dbtype")))
- .list();
- Iterator it = ll.iterator();
- while(it.hasNext()){
- Object[] object = (Object[]) it.next();
- OpLoggerDbType odb = (OpLoggerDbType)object[0];
- dataset.addValue(new Integer(object[1].toString()).intValue(), "", odb.getName());
- }
- return dataset;
- }
- /**
- * 图形生成
- * @param map
- * @param title
- * @param session
- * @param pw
- * @return
- * fhway 2007-10-18 上午10:50:15
- * @throws Exception
- */
- public String getPieChart3D(String title, HttpSession session,PrintWriter pw) throws Exception{
- Map map = getLoggerDBTypeDataMap();
- return GenerateWebPieChart3D.getPieChart3D(map,title,width,height, session, pw);
- }
- public String getBarChart3D(String title, String xName ,String yName, HttpSession session,PrintWriter pw) throws Exception{
- CategoryDataset dataset = getLoggerDBTypeDataDataSet();
- return GenerateWebBarChart3D.getBarChart3D(dataset,title,xName , yName, width, height, session,pw);
- }
- /**
- * @return the height
- */
- public int getHeight() {
- return height;
- }
- /**
- * @param height the height to set
- */
- public void setHeight(int height) {
- this.height = height;
- }
- /**
- * @return the width
- */
- public int getWidth() {
- return width;
- }
- /**
- * @param width the width to set
- */
- public void setWidth(int width) {
- this.width = width;
- }
- /**
- * @return the map
- */
- public Map getMap() {
- return map;
- }
- /**
- * @param map the map to set
- */
- public void setMap(Map map) {
- this.map = map;
- }
- /**
- * @return the opLoggerDAO
- */
- public IOpLogger getOpLoggerDAO() {
- return opLoggerDAO;
- }
- /**
- * @param opLoggerDAO the opLoggerDAO to set
- */
- public void setOpLoggerDAO(IOpLogger opLoggerDAO) {
- this.opLoggerDAO = opLoggerDAO;
- }
- public static void main(String[] args) throws Exception{
- ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
- IOpLoggerChart lc = (IOpLoggerChart) context.getBean("loggerChartManger");
- }
- }
java 代码
- <%@ page contentType="text/html;charset=GBK"%>
- <%@ page import = "java.io.PrintWriter" %>
- <%@ page import="org.springframework.web.context.WebApplicationContext"%>
- <%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
- <%@ page import="com.sclh.rsp.registercenter.service.chart.IOpLoggerChart"%>
- <%
- ServletContext servletContext = (ServletContext) request.getSession().getServletContext();
- WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
- IOpLoggerChart lc = (IOpLoggerChart) ctx.getBean("opLoggerChartManger");
- String filename = lc.getPieChart3D("日志数据操作统计图", session, new PrintWriter(out));
- String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
- %>
- <HTML>
- <HEAD>
- <TITLE>日志数据操作统计图</TITLE>
- </HEAD>
- <BODY>
- <P ALIGN="CENTER">
- <img src="<%=graphURL%>" width=560 height=500 border=0 usemap="#<%=filename%>">
- </P>
- </BODY>
- </HTML>
代码基本就是这些,下面是项目的配置问题:
1.由于项目采用了Spring和Hibernate的框架,所以必须要进行一些设定.
2.数据库表的sql如下:
java 代码
- create table OPLOGGER
- (
- ID VARCHAR2(12) not null,
- MESSAGE VARCHAR2(1000) not null,
- USERID VARCHAR2(12) not null,
- TYPE VARCHAR2(2) not null,
- DBTYPE VARCHAR2(2) not null,
- ISLOOK CHAR(1),
- REMOTEADDR VARCHAR2(15),
- SYSTEMTIME DATE not null,
- DELFLAG CHAR(1) not null
- )
- tablespace RSC
- pctfree 10
- initrans 1
- maxtrans 255
- storage
- (
- initial 64K
- minextents 1
- maxextents unlimited
- );
- comment on table OPLOGGER
- is '日志表';
- alter table OPLOGGER
- add constraint PK_LOG primary key (ID)
- using index
- tablespace RSC
- pctfree 10
- initrans 2
- maxtrans 255
- storage
- (
- initial 64K
- minextents 1
- maxextents unlimited
- );
- create table OPLOGGER_DBTYPE
- (
- ID VARCHAR2(2) not null,
- NAME VARCHAR2(30) not null,
- ALIAS VARCHAR2(50),
- COMM VARCHAR2(200),
- SYSTEMTIME DATE not null,
- DELFLAG CHAR(1) not null
- )
- tablespace RSC
- pctfree 10
- initrans 1
- maxtrans 255
- storage
- (
- initial 16K
- minextents 1
- maxextents unlimited
- );
- comment on table OPLOGGER_DBTYPE
- is '日志DB操作类别';
- alter table OPLOGGER_DBTYPE
- add constraint PK_OPLOGGER_DBTYPE primary key (ID)
- using index
- tablespace RSC
- pctfree 10
- initrans 2
- maxtrans 255
- storage
- (
- initial 64K
- minextents 1
- maxextents unlimited
- );
- create table OPLOGGER_TYPE
- (
- ID VARCHAR2(2) not null,
- NAME VARCHAR2(30) not null,
- ALIAS VARCHAR2(50),
- COMM VARCHAR2(200),
- SYSTEMTIME DATE not null,
- DELFLAG CHAR(1) not null
- )
- tablespace RSC
- pctfree 10
- initrans 1
- maxtrans 255
- storage
- (
- initial 16K
- minextents 1
- maxextents unlimited
- );
- comment on table OPLOGGER_TYPE
- is '日志类别';
- alter table OPLOGGER_TYPE
- add constraint PK_OPLOGGER_TYPE primary key (ID)
- using index
- tablespace RSC
- pctfree 10
- initrans 2
- maxtrans 255
- storage
- (
- initial 64K
- minextents 1
- maxextents unlimited
- );
3.主要Hibernate的配置文件如下
AbstractOpLogger.java
OpLogger.java
OpLogger.hbm.xml
java 代码
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse - Hibernate Tools
- -->
- <hibernate-mapping>
- <class name="com.sclh.rsp.registercenter.hibernate.vo.OpLogger" table="OPLOGGER" schema="RSC">
- <id name="id" type="java.lang.String">
- <column name="ID" length="12" />
- <generator class="sequence">
- <param name="sequence">SEQ_OPLOGGER</param>
- </generator>
- </id>
- <property name="message" type="java.lang.String">
- <column name="MESSAGE" length="1000" not-null="true" />
- </property>
- <many-to-one name="userid"
- column="USERID"
- class="com.sclh.rsp.registercenter.hibernate.vo.UserInfo"
- outer-join="true"
- not-found="ignore"
- lazy="false"/>
- <many-to-one name="type"
- column="TYPE"
- class="com.sclh.rsp.registercenter.hibernate.vo.OpLoggerType"
- outer-join="true"
- not-found="ignore"
- lazy="false"/>
- <many-to-one name="dbtype"
- column="DBTYPE"
- class="com.sclh.rsp.registercenter.hibernate.vo.OpLoggerDbType"
- outer-join="true"
- not-found="ignore"
- lazy="false"/>
- <property name="islook" type="java.lang.String">
- <column name="ISLOOK" length="1" />
- </property>
- <property name="remoteaddr" type="java.lang.String">
- <column name="REMOTEADDR" length="15" />
- </property>
- <property name="systemtime" type="java.util.Date">
- <column name="SYSTEMTIME" length="7" not-null="true" />
- </property>
- <property name="delflag" type="java.lang.String">
- <column name="DELFLAG" length="1" not-null="true" />
- </property>
- </class>
- </hibernate-mapping>
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>
xml 代码
- <!-- 配置读取properties文件-->
- <bean id="placeholderConfig"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location">
- <value>classpath:init.properties</value>
- </property>
- </bean>
- !--Chart-->
- <bean id="opLoggerChartMangerTarget" class="com.sclh.rsp.registercenter.service.chart.OpLoggerChartImpl">
- <property name="opLoggerDAO">
- <ref bean="opLoggerDAO"/>
- </property>
- <property name="width">
- <value>${chart.width}</value>
- </property>
- <property name="height">
- <value>${chart.height}</value>
- </property>
- </bean>
- <bean id="opLoggerChartManger" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
- <property name="transactionManager">
- <ref bean="myTransactionManager"/>
- </property>
- <property name="target">
- <ref bean="opLoggerChartMangerTarget"/>
- </property>
- <property name="transactionAttributes">
- <props>
- <prop key="get*">PROPAGATION_SUPPORTS</prop>
- </props>
- </property>
- </bean>
java 代码
- chart.height=400
- chart.width=500
java 代码
- package com.sclh.rsp.registercenter.hibernate.vo;
- // default package
- // Generated by MyEclipse - Hibernate Tools
- import java.util.Date;
- /**
- * OpLogger generated by MyEclipse - Hibernate Tools
- */
- public class OpLogger extends AbstractOpLogger implements java.io.Serializable {
- // Constructors
- /** default constructor */
- public OpLogger() {
- }
- /** minimal constructor */
- public OpLogger(String id, String message, UserInfo userid, OpLoggerType type, OpLoggerDbType dbtype, Date systemtime, String delflag) {
- super(id, message, userid, type, dbtype, systemtime, delflag);
- }
- /** full constructor */
- public OpLogger(String id, String message, UserInfo userid, OpLoggerType type, OpLoggerDbType dbtype, String islook, String remoteaddr, Date systemtime, String delflag) {
- super(id, message, userid, type, dbtype, islook, remoteaddr, systemtime, delflag);
- }
- }
java 代码
- package com.sclh.rsp.registercenter.hibernate.vo;
- // default package
- import java.util.Date;
- /**
- * AbstractOpLogger generated by MyEclipse - Hibernate Tools
- */
- public abstract class AbstractOpLogger implements java.io.Serializable {
- // Fields
- private String id;
- private String message;
- private UserInfo userid;
- private OpLoggerType type;
- private OpLoggerDbType dbtype;
- private String islook;
- private String remoteaddr;
- private Date systemtime;
- private String delflag;
- // Constructors
- /** default constructor */
- public AbstractOpLogger() {
- }
- /** minimal constructor */
- public AbstractOpLogger(String id, String message, UserInfo userid, OpLoggerType type, OpLoggerDbType dbtype, Date systemtime, String delflag) {
- this.id = id;
- this.message = message;
- this.userid = userid;
- this.type = type;
- this.dbtype = dbtype;
- this.systemtime = systemtime;
- this.delflag = delflag;
- }
- /** full constructor */
- public AbstractOpLogger(String id, String message, UserInfo userid, OpLoggerType type, OpLoggerDbType dbtype, String islook, String remoteaddr, Date systemtime, String delflag) {
- this.id = id;
- this.message = message;
- this.userid = userid;
- this.type = type;
- this.dbtype = dbtype;
- this.islook = islook;
- this.remoteaddr = remoteaddr;
- this.systemtime = systemtime;
- this.delflag = delflag;
- }
- // Property accessors
- public String getId() {
- return this.id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getMessage() {
- return this.message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- public UserInfo getUserid() {
- return this.userid;
- }
- public void setUserid(UserInfo userid) {
- this.userid = userid;
- }
- public OpLoggerType getType() {
- return this.type;
- }
- public void setType(OpLoggerType type) {
- this.type = type;
- }
- public OpLoggerDbType getDbtype() {
- return this.dbtype;
- }
- public void setDbtype(OpLoggerDbType dbtype) {
- this.dbtype = dbtype;
- }
- public String getIslook() {
- return this.islook;
- }
- public void setIslook(String islook) {
- this.islook = islook;
- }
- public String getRemoteaddr() {
- return this.remoteaddr;
- }
- public void setRemoteaddr(String remoteaddr) {
- this.remoteaddr = remoteaddr;
- }
- public Date getSystemtime() {
- return this.systemtime;
- }
- public void setSystemtime(Date systemtime) {
- this.systemtime = systemtime;
- }
- public String getDelflag() {
- return this.delflag;
- }
- public void setDelflag(String delflag) {
- this.delflag = delflag;
- }
- }
java 代码
- /**
- * Project name: RCP
- *
- * Package name: com.sclh.rsp.registercenter.service.chart
- * Filename : GenerateWebBarChart3D.java
- * @Author : fhway
- * DateTime : 2007-10-18 下午04:43:00
- * Visoin : 1.0
- * Company : sclh
- * Copyright (c) 2007
- * 说明:
- */
- package com.sclh.rsp.registercenter.service.chart;
- import java.awt.Font;
- import java.awt.RenderingHints;
- import java.io.IOException;
- 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.plot.PlotOrientation;
- import org.jfree.chart.servlet.ServletUtilities;
- import org.jfree.chart.title.TextTitle;
- import org.jfree.data.category.CategoryDataset;
- /**
- * @author fhway
- *
- */
- public class GenerateWebBarChart3D {
- public static String getBarChart3D(CategoryDataset dataset,String title,String xName ,String yName, int width, int height, HttpSession session,PrintWriter pw){
- String filename = null;
- Font font = null;
- JFreeChart chart = ChartFactory.createBarChart3D(title, xName, yName, dataset, PlotOrientation.VERTICAL, false, false, false);
- chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
- TextTitle tt=new TextTitle(title);
- font = new Font("黑体",Font.CENTER_BASELINE,20);//这个地方是设置统计图标题的字体和大小
- tt.setFont(font);
- chart.setTitle(tt);
- chart.setBackgroundPaint(new java.awt.Color(244, 247, 251)); //统计图片的底色
- ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
- try {
- filename = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);
- //把image map 写入到 PrintWriter
- ChartUtilities.writeImageMap(pw, filename, info, true);
- } catch (IOException e) {
- System.out.println("Exception - " + e.toString());
- e.printStackTrace(System.out);
- filename = "public_error_500x300.png";
- }
- pw.flush();
- return filename;
- }
- }
java 代码
- /**
- * Project name: RCP
- *
- * Package name: com.sclh.rsp.registercenter.hibernate.dao.chat
- * Filename : GenerateWebPieChart3D.java
- * @Author : fhway
- * DateTime : 2007-10-18 上午10:21:38
- * Visoin : 1.0
- * Company : sclh
- * Copyright (c) 2007
- * 说明:
- */
- package com.sclh.rsp.registercenter.service.chart;
- import java.awt.Font;
- import java.awt.RenderingHints;
- import java.io.PrintWriter;
- import java.text.DecimalFormat;
- import java.text.NumberFormat;
- import java.util.Iterator;
- import java.util.Map;
- 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.labels.StandardPieSectionLabelGenerator;
- import org.jfree.chart.plot.PiePlot;
- import org.jfree.chart.servlet.ServletUtilities;
- import org.jfree.chart.title.TextTitle;
- import org.jfree.data.general.DefaultPieDataset;
- /**
- * @author fhway
- *
- */
- public class GenerateWebPieChart3D {
- public static String getPieChart3D(Map map,String title,int width,int height,HttpSession session,PrintWriter pw){
- String filename = null;
- Font font = null;
- DefaultPieDataset data = new DefaultPieDataset();
- Iterator it = null;
- it = map.entrySet().iterator();
- while(it.hasNext()){
- Map.Entry entry = (Map.Entry) it.next();
- data.setValue(String.valueOf(entry.getKey()), Double.valueOf(String.valueOf(entry.getValue())));
- }
- try{
- JFreeChart chart = ChartFactory.createPieChart3D(title, data, false, false, false);
- PiePlot plot = (PiePlot)chart.getPlot();
- plot.setNoDataMessage("查询数据为空!");
- plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
- plot.setForegroundAlpha(0.5f);
- plot.setCircular(false);
- font = new Font("simsun",Font.PLAIN,12);//这个地方是设置统计图标题的字体和大小
- plot.setLabelFont(font);
- chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
- TextTitle tt=new TextTitle(title);
- font = new Font("黑体",Font.CENTER_BASELINE,20);//这个地方是设置统计图标题的字体和大小
- tt.setFont(font);
- chart.setTitle(tt);
- chart.setBackgroundPaint(new java.awt.Color(244, 247, 251)); //统计图片的底色
- ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
- filename = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);
- //把image map 写入到 PrintWriter
- ChartUtilities.writeImageMap(pw, filename, info, true);
- pw.flush();
- } catch (Exception e) {
- System.out.println("Exception - " + e.toString());
- e.printStackTrace(System.out);
- filename = "public_error_500x300.png";
- }
- return filename;
- }
- }
java 代码
- /**
- * Project name: RCP
- *
- * Package name: com.sclh.rsp.registercenter.service.chat
- * Filename : ILoggerChat.java
- * @Author : fhway
- * DateTime : 2007-10-18 下午02:28:48
- * Visoin : 1.0
- * Company : sclh
- * Copyright (c) 2007
- * 说明:
- */
- package com.sclh.rsp.registercenter.service.chart;
- import java.io.PrintWriter;
- import javax.servlet.http.HttpSession;
- /**
- * @author fhway
- *
- */
- public interface IOpLoggerChart {
- /**
- *
- * @param title
- * @param session
- * @param pw
- * @return
- * @throws Exception
- * fhway 2007-10-18 下午05:21:03
- */
- public String getPieChart3D(String title, HttpSession session,PrintWriter pw) throws Exception;
- /**
- *
- * @param title
- * @param xName
- * @param yName
- * @param session
- * @param pw
- * @return
- * @throws Exception
- * fhway 2007-10-18 下午05:21:07
- */
- public String getBarChart3D(String title, String xName ,String yName, HttpSession session,PrintWriter pw) throws Exception;
- }

浙公网安备 33010602011771号