JFreeChart1.0.6的一个通用Web Demo

这是我自己做的一个关于统计的WebDemo;

首先建立一个web项目;在src中加入以下java文件:

GenerateWebBarChart3D.java(用于Bar3D图的显示的) GenerateWebPieChart3D.java(用于Pie3D图显示的)  IOpLoggerChart.java(调用方法接口文件)OpLoggerChartImpl.java(调用实现)

java 代码
  1. /**  
  2.  * Project name: RCP  
  3.  *  
  4.  * Package name: com.sclh.rsp.registercenter.hibernate.dao.chat  
  5.  * Filename    : LoggerChat.java  
  6.  * @Author     : fhway  
  7.  * DateTime    : 2007-10-18 上午10:26:02  
  8.  * Visoin      : 1.0  
  9.  * Company     : sclh  
  10.  * Copyright (c) 2007   
  11.  *   
  12.  * 说明:日志类型分类,图的长度和宽度采取Ioc的方式注入  
  13.  * 可以统计的类别有   
  14.  * 1. 日志数据库操作类型  
  15.  * 2. 时间段访问量(以月为单位,当前一年)  
  16.  * sample HQL="select t.dbtype, count(t.dbtype) from oplogger t  group by(t.dbtype) order by to_number(t.dbtype)"  
  17.  */  
  18.   
  19. package com.sclh.rsp.registercenter.service.chart;   
  20.   
  21. import java.io.PrintWriter;   
  22. import java.util.HashMap;   
  23. import java.util.Iterator;   
  24. import java.util.List;   
  25. import java.util.Map;   
  26.   
  27. import javax.servlet.http.HttpSession;   
  28.   
  29. import org.hibernate.HibernateException;   
  30. import org.hibernate.criterion.Projections;   
  31. import org.jfree.data.category.CategoryDataset;   
  32. import org.jfree.data.category.DefaultCategoryDataset;   
  33. import org.springframework.context.ApplicationContext;   
  34. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  35.   
  36. import com.sclh.rsp.registercenter.hibernate.dao.IOpLogger;   
  37. import com.sclh.rsp.registercenter.hibernate.vo.OpLogger;   
  38. import com.sclh.rsp.registercenter.hibernate.vo.OpLoggerDbType;   
  39.   
  40. /**  
  41.  * @author fhway  
  42.  *  
  43.  */  
  44. public class OpLoggerChartImpl implements IOpLoggerChart{   
  45.     /**  
  46.      * 定义输出图片的长度和宽度  
  47.      */  
  48.     private int width;   
  49.     private int height;   
  50.     /*DAO方法 */  
  51.     private IOpLogger opLoggerDAO;   
  52.     /* 图标数据 */  
  53.     private Map map;   
  54.        
  55.     public OpLoggerChartImpl(){   
  56.         map = new HashMap();   
  57.         if(width == 0 ){ width = 500;}   
  58.         if(height == 0){ height = 400;}   
  59.     }   
  60.        
  61.     /**  
  62.      * 获取图标数据  
  63.      * @return  
  64.      * @throws Exception  
  65.      * fhway 2007-10-18 上午10:49:55  
  66.      */  
  67.     private Map getLoggerDBTypeDataMap() throws Exception{   
  68.         Map map = new HashMap();   
  69.         List ll = opLoggerDAO.getCriteria(OpLogger.class)   
  70.             .setProjection(Projections.projectionList()   
  71.                     .add(Projections.property("dbtype"))   
  72.                     .add(Projections.count("dbtype"))   
  73.                     .add(Projections.groupProperty("dbtype")))   
  74.                     .list();   
  75.         Iterator it = ll.iterator();   
  76.         while(it.hasNext()){   
  77.             Object[] object = (Object[]) it.next();   
  78.             OpLoggerDbType odb = (OpLoggerDbType)object[0];   
  79.             map.put(odb.getName(), object[1].toString());   
  80.         }   
  81.         return map;   
  82.     }   
  83.   
  84.        
  85.     /**  
  86.      * 获取一个演示用的简单数据集对象  
  87.      * @return  
  88.      * @throws Exception   
  89.      * @throws HibernateException   
  90.      */  
  91.     private CategoryDataset getLoggerDBTypeDataDataSet() throws Exception {   
  92.            
  93.         DefaultCategoryDataset dataset = new DefaultCategoryDataset();   
  94.         List ll = opLoggerDAO.getCriteria(OpLogger.class)   
  95.             .setProjection(Projections.projectionList()   
  96.                     .add(Projections.property("dbtype"))   
  97.                     .add(Projections.count("dbtype"))   
  98.                     .add(Projections.groupProperty("dbtype")))   
  99.                     .list();   
  100.         Iterator it = ll.iterator();   
  101.         while(it.hasNext()){   
  102.             Object[] object = (Object[]) it.next();   
  103.             OpLoggerDbType odb = (OpLoggerDbType)object[0];   
  104.             dataset.addValue(new Integer(object[1].toString()).intValue(), "", odb.getName());   
  105.         }   
  106.         return dataset;   
  107.     }   
  108.        
  109.     /**  
  110.      * 图形生成  
  111.      * @param map  
  112.      * @param title  
  113.      * @param session  
  114.      * @param pw  
  115.      * @return  
  116.      * fhway 2007-10-18 上午10:50:15  
  117.      * @throws Exception   
  118.      */  
  119.     public String getPieChart3D(String title, HttpSession session,PrintWriter pw) throws Exception{   
  120.         Map map = getLoggerDBTypeDataMap();   
  121.         return GenerateWebPieChart3D.getPieChart3D(map,title,width,height, session, pw);   
  122.     }   
  123.        
  124.     public String getBarChart3D(String title, String xName ,String yName, HttpSession session,PrintWriter pw) throws Exception{   
  125.         CategoryDataset dataset = getLoggerDBTypeDataDataSet();   
  126.         return GenerateWebBarChart3D.getBarChart3D(dataset,title,xName , yName, width, height, session,pw);   
  127.     }   
  128.        
  129.     /**  
  130.      * @return the height  
  131.      */  
  132.     public int getHeight() {   
  133.         return height;   
  134.     }   
  135.     /**  
  136.      * @param height the height to set  
  137.      */  
  138.     public void setHeight(int height) {   
  139.         this.height = height;   
  140.     }   
  141.     /**  
  142.      * @return the width  
  143.      */  
  144.     public int getWidth() {   
  145.         return width;   
  146.     }   
  147.     /**  
  148.      * @param width the width to set  
  149.      */  
  150.     public void setWidth(int width) {   
  151.         this.width = width;   
  152.     }   
  153.   
  154.     /**  
  155.      * @return the map  
  156.      */  
  157.     public Map getMap() {   
  158.         return map;   
  159.     }   
  160.   
  161.     /**  
  162.      * @param map the map to set  
  163.      */  
  164.     public void setMap(Map map) {   
  165.         this.map = map;   
  166.     }   
  167.        
  168.     /**  
  169.      * @return the opLoggerDAO  
  170.      */  
  171.     public IOpLogger getOpLoggerDAO() {   
  172.         return opLoggerDAO;   
  173.     }   
  174.   
  175.     /**  
  176.      * @param opLoggerDAO the opLoggerDAO to set  
  177.      */  
  178.     public void setOpLoggerDAO(IOpLogger opLoggerDAO) {   
  179.         this.opLoggerDAO = opLoggerDAO;   
  180.     }   
  181.   
  182.     public static void main(String[] args) throws Exception{   
  183.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");   
  184.         IOpLoggerChart lc = (IOpLoggerChart) context.getBean("loggerChartManger");   
  185.            
  186.            
  187.     }   
  188.        
  189.        
  190.        
  191.   
  192. }   
OpLoggerChart.jsp(jsp调用实现)
java 代码
  1. <%@ page contentType="text/html;charset=GBK"%>   
  2. <%@ page import = "java.io.PrintWriter" %>   
  3. <%@ page import="org.springframework.web.context.WebApplicationContext"%>   
  4. <%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%>   
  5. <%@ page import="com.sclh.rsp.registercenter.service.chart.IOpLoggerChart"%>   
  6.   
  7. <%   
  8. ServletContext servletContext = (ServletContext) request.getSession().getServletContext();   
  9. WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);   
  10. IOpLoggerChart lc = (IOpLoggerChart) ctx.getBean("opLoggerChartManger");   
  11.   
  12. String filename = lc.getPieChart3D("日志数据操作统计图", session, new PrintWriter(out));   
  13.   
  14. String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;   
  15. %>   
  16. <HTML>   
  17. <HEAD>   
  18. <TITLE>日志数据操作统计图</TITLE>   
  19. </HEAD>   
  20. <BODY>   
  21. <P ALIGN="CENTER">   
  22. <img src="<%=graphURL%>" width=560 height=500 border=0 usemap="#<%=filename%>">   
  23. </P>   
  24. </BODY>   
  25. </HTML>  

代码基本就是这些,下面是项目的配置问题:

1.由于项目采用了Spring和Hibernate的框架,所以必须要进行一些设定.

2.数据库表的sql如下:

java 代码
  1. create table OPLOGGER   
  2. (   
  3.   ID         VARCHAR2(12) not null,   
  4.   MESSAGE    VARCHAR2(1000) not null,   
  5.   USERID     VARCHAR2(12) not null,   
  6.   TYPE       VARCHAR2(2) not null,   
  7.   DBTYPE     VARCHAR2(2) not null,   
  8.   ISLOOK     CHAR(1),   
  9.   REMOTEADDR VARCHAR2(15),   
  10.   SYSTEMTIME DATE not null,   
  11.   DELFLAG    CHAR(1) not null  
  12. )   
  13. tablespace RSC   
  14.   pctfree 10  
  15.   initrans 1  
  16.   maxtrans 255  
  17.   storage   
  18.   (   
  19.     initial 64K   
  20.     minextents 1  
  21.     maxextents unlimited   
  22.   );   
  23. comment on table OPLOGGER   
  24.   is '日志表';   
  25. alter table OPLOGGER   
  26.   add constraint PK_LOG primary key (ID)   
  27.   using index    
  28.   tablespace RSC   
  29.   pctfree 10  
  30.   initrans 2  
  31.   maxtrans 255  
  32.   storage   
  33.   (   
  34.     initial 64K   
  35.     minextents 1  
  36.     maxextents unlimited   
  37.   );   
  38. create table OPLOGGER_DBTYPE   
  39. (   
  40.   ID         VARCHAR2(2) not null,   
  41.   NAME       VARCHAR2(30) not null,   
  42.   ALIAS      VARCHAR2(50),   
  43.   COMM       VARCHAR2(200),   
  44.   SYSTEMTIME DATE not null,   
  45.   DELFLAG    CHAR(1) not null  
  46. )   
  47. tablespace RSC   
  48.   pctfree 10  
  49.   initrans 1  
  50.   maxtrans 255  
  51.   storage   
  52.   (   
  53.     initial 16K   
  54.     minextents 1  
  55.     maxextents unlimited   
  56.   );   
  57. comment on table OPLOGGER_DBTYPE   
  58.   is '日志DB操作类别';   
  59. alter table OPLOGGER_DBTYPE   
  60.   add constraint PK_OPLOGGER_DBTYPE primary key (ID)   
  61.   using index    
  62.   tablespace RSC   
  63.   pctfree 10  
  64.   initrans 2  
  65.   maxtrans 255  
  66.   storage   
  67.   (   
  68.     initial 64K   
  69.     minextents 1  
  70.     maxextents unlimited   
  71.   );   
  72. create table OPLOGGER_TYPE   
  73. (   
  74.   ID         VARCHAR2(2) not null,   
  75.   NAME       VARCHAR2(30) not null,   
  76.   ALIAS      VARCHAR2(50),   
  77.   COMM       VARCHAR2(200),   
  78.   SYSTEMTIME DATE not null,   
  79.   DELFLAG    CHAR(1) not null  
  80. )   
  81. tablespace RSC   
  82.   pctfree 10  
  83.   initrans 1  
  84.   maxtrans 255  
  85.   storage   
  86.   (   
  87.     initial 16K   
  88.     minextents 1  
  89.     maxextents unlimited   
  90.   );   
  91. comment on table OPLOGGER_TYPE   
  92.   is '日志类别';   
  93. alter table OPLOGGER_TYPE   
  94.   add constraint PK_OPLOGGER_TYPE primary key (ID)   
  95.   using index    
  96.   tablespace RSC   
  97.   pctfree 10  
  98.   initrans 2  
  99.   maxtrans 255  
  100.   storage   
  101.   (   
  102.     initial 64K   
  103.     minextents 1  
  104.     maxextents unlimited   
  105.   );   
  106.   

3.主要Hibernate的配置文件如下

AbstractOpLogger.java

OpLogger.java

OpLogger.hbm.xml
java 代码
  1. <?xml version="1.0"?>   
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">   
  4. <!--    
  5.     Mapping file autogenerated by MyEclipse - Hibernate Tools   
  6. -->   
  7. <hibernate-mapping>   
  8.     <class name="com.sclh.rsp.registercenter.hibernate.vo.OpLogger" table="OPLOGGER" schema="RSC">   
  9.         <id name="id" type="java.lang.String">   
  10.             <column name="ID" length="12" />   
  11.             <generator class="sequence">   
  12.                 <param name="sequence">SEQ_OPLOGGER</param>   
  13.             </generator>   
  14.         </id>   
  15.         <property name="message" type="java.lang.String">   
  16.             <column name="MESSAGE" length="1000" not-null="true" />   
  17.         </property>   
  18.         <many-to-one name="userid"    
  19.                      column="USERID"    
  20.                      class="com.sclh.rsp.registercenter.hibernate.vo.UserInfo"  
  21.                      outer-join="true"  
  22.                      not-found="ignore"  
  23.                      lazy="false"/>   
  24.         <many-to-one name="type"    
  25.                      column="TYPE"    
  26.                      class="com.sclh.rsp.registercenter.hibernate.vo.OpLoggerType"  
  27.                      outer-join="true"  
  28.                      not-found="ignore"  
  29.                      lazy="false"/>   
  30.         <many-to-one name="dbtype"    
  31.                      column="DBTYPE"    
  32.                      class="com.sclh.rsp.registercenter.hibernate.vo.OpLoggerDbType"  
  33.                      outer-join="true"  
  34.                      not-found="ignore"  
  35.                      lazy="false"/>   
  36.         <property name="islook" type="java.lang.String">   
  37.             <column name="ISLOOK" length="1" />   
  38.         </property>   
  39.         <property name="remoteaddr" type="java.lang.String">   
  40.             <column name="REMOTEADDR" length="15" />   
  41.         </property>   
  42.         <property name="systemtime" type="java.util.Date">   
  43.             <column name="SYSTEMTIME" length="7" not-null="true" />   
  44.         </property>   
  45.         <property name="delflag" type="java.lang.String">   
  46.             <column name="DELFLAG" length="1" not-null="true" />   
  47.         </property>   
  48.     </class>   
  49. </hibernate-mapping>   
4. Web.xml配置 加上以下代码
xml 代码
  1.     <servlet>  
  2.         <servlet-name>DisplayChart</servlet-name>  
  3.         <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>  
  4.     </servlet>  
  5. ..........   
  6.     <servlet-mapping>  
  7.         <servlet-name>DisplayChart</servlet-name>  
  8.         <url-pattern>/servlet/DisplayChart</url-pattern>  
  9.     </servlet-mapping>  
5. Spring文件配置:
xml 代码
  1. <!-- 配置读取properties文件-->  
  2. <bean id="placeholderConfig"  
  3.     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  4.     <property name="location">  
  5.         <value>classpath:init.properties</value>  
  6.     </property>  
  7. </bean>  
  8. !--Chart-->  
  9.  <bean id="opLoggerChartMangerTarget" class="com.sclh.rsp.registercenter.service.chart.OpLoggerChartImpl">  
  10.    <property name="opLoggerDAO">  
  11.      <ref bean="opLoggerDAO"/>  
  12.    </property>  
  13.    <property name="width">  
  14.      <value>${chart.width}</value>  
  15.    </property>  
  16.    <property name="height">  
  17.      <value>${chart.height}</value>  
  18.    </property>  
  19.       
  20.  </bean>  
  21.  <bean id="opLoggerChartManger" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
  22.    <property name="transactionManager">  
  23.      <ref bean="myTransactionManager"/>  
  24.    </property>  
  25.    <property name="target">  
  26.      <ref bean="opLoggerChartMangerTarget"/>  
  27.    </property>  
  28.    <property name="transactionAttributes">  
  29.      <props>  
  30.        <prop key="get*">PROPAGATION_SUPPORTS</prop>  
  31.      </props>  
  32.    </property>  
  33.  </bean>  
6. 资源文件init.properties
java 代码
  1. chart.height=400  
  2. chart.width=500  
7.还有其他一些代码和方法没有贴出是因为对于理解JFreChart没有多大的帮助!说的不准确的地方请指正! 

 

java 代码
  1. package com.sclh.rsp.registercenter.hibernate.vo;   
  2. // default package   
  3. // Generated by MyEclipse - Hibernate Tools   
  4.   
  5. import java.util.Date;   
  6.   
  7.   
  8. /**  
  9.  * OpLogger generated by MyEclipse - Hibernate Tools  
  10.  */  
  11. public class OpLogger extends AbstractOpLogger implements java.io.Serializable {   
  12.   
  13.     // Constructors   
  14.   
  15.     /** default constructor */  
  16.     public OpLogger() {   
  17.     }   
  18.   
  19.     /** minimal constructor */  
  20.     public OpLogger(String id, String message, UserInfo userid, OpLoggerType type, OpLoggerDbType dbtype, Date systemtime, String delflag) {   
  21.         super(id, message, userid, type, dbtype, systemtime, delflag);           
  22.     }   
  23.        
  24.     /** full constructor */  
  25.     public OpLogger(String id, String message, UserInfo userid, OpLoggerType type, OpLoggerDbType dbtype, String islook, String remoteaddr, Date systemtime, String delflag) {   
  26.         super(id, message, userid, type, dbtype, islook, remoteaddr, systemtime, delflag);           
  27.     }   
  28.       
  29. }   

 

java 代码
  1. package com.sclh.rsp.registercenter.hibernate.vo;   
  2. // default package   
  3.   
  4. import java.util.Date;   
  5.   
  6.   
  7. /**  
  8.  * AbstractOpLogger generated by MyEclipse - Hibernate Tools  
  9.  */  
  10.   
  11. public abstract class AbstractOpLogger  implements java.io.Serializable {   
  12.   
  13.   
  14.     // Fields       
  15.   
  16.      private String id;   
  17.      private String message;   
  18.      private UserInfo userid;   
  19.      private OpLoggerType type;   
  20.      private OpLoggerDbType dbtype;   
  21.      private String islook;   
  22.      private String remoteaddr;   
  23.      private Date systemtime;   
  24.      private String delflag;   
  25.   
  26.   
  27.     // Constructors   
  28.   
  29.     /** default constructor */  
  30.     public AbstractOpLogger() {   
  31.     }   
  32.   
  33.     /** minimal constructor */  
  34.     public AbstractOpLogger(String id, String message, UserInfo userid, OpLoggerType type, OpLoggerDbType dbtype, Date systemtime, String delflag) {   
  35.         this.id = id;   
  36.         this.message = message;   
  37.         this.userid = userid;   
  38.         this.type = type;   
  39.         this.dbtype = dbtype;   
  40.         this.systemtime = systemtime;   
  41.         this.delflag = delflag;   
  42.     }   
  43.        
  44.     /** full constructor */  
  45.     public AbstractOpLogger(String id, String message, UserInfo userid, OpLoggerType type, OpLoggerDbType dbtype, String islook, String remoteaddr, Date systemtime, String delflag) {   
  46.         this.id = id;   
  47.         this.message = message;   
  48.         this.userid = userid;   
  49.         this.type = type;   
  50.         this.dbtype = dbtype;   
  51.         this.islook = islook;   
  52.         this.remoteaddr = remoteaddr;   
  53.         this.systemtime = systemtime;   
  54.         this.delflag = delflag;   
  55.     }   
  56.   
  57.       
  58.     // Property accessors   
  59.   
  60.     public String getId() {   
  61.         return this.id;   
  62.     }   
  63.        
  64.     public void setId(String id) {   
  65.         this.id = id;   
  66.     }   
  67.   
  68.     public String getMessage() {   
  69.         return this.message;   
  70.     }   
  71.        
  72.     public void setMessage(String message) {   
  73.         this.message = message;   
  74.     }   
  75.   
  76.     public UserInfo getUserid() {   
  77.         return this.userid;   
  78.     }   
  79.        
  80.     public void setUserid(UserInfo userid) {   
  81.         this.userid = userid;   
  82.     }   
  83.   
  84.     public OpLoggerType getType() {   
  85.         return this.type;   
  86.     }   
  87.        
  88.     public void setType(OpLoggerType type) {   
  89.         this.type = type;   
  90.     }   
  91.   
  92.     public OpLoggerDbType getDbtype() {   
  93.         return this.dbtype;   
  94.     }   
  95.        
  96.     public void setDbtype(OpLoggerDbType dbtype) {   
  97.         this.dbtype = dbtype;   
  98.     }   
  99.   
  100.     public String getIslook() {   
  101.         return this.islook;   
  102.     }   
  103.        
  104.     public void setIslook(String islook) {   
  105.         this.islook = islook;   
  106.     }   
  107.   
  108.     public String getRemoteaddr() {   
  109.         return this.remoteaddr;   
  110.     }   
  111.        
  112.     public void setRemoteaddr(String remoteaddr) {   
  113.         this.remoteaddr = remoteaddr;   
  114.     }   
  115.   
  116.     public Date getSystemtime() {   
  117.         return this.systemtime;   
  118.     }   
  119.        
  120.     public void setSystemtime(Date systemtime) {   
  121.         this.systemtime = systemtime;   
  122.     }   
  123.   
  124.     public String getDelflag() {   
  125.         return this.delflag;   
  126.     }   
  127.        
  128.     public void setDelflag(String delflag) {   
  129.         this.delflag = delflag;   
  130.     }   
  131.     
  132. }  

 

java 代码
  1. /**  
  2.  * Project name: RCP  
  3.  *  
  4.  * Package name: com.sclh.rsp.registercenter.service.chart  
  5.  * Filename    : GenerateWebBarChart3D.java  
  6.  * @Author     : fhway  
  7.  * DateTime    : 2007-10-18 下午04:43:00  
  8.  * Visoin      : 1.0  
  9.  * Company     : sclh  
  10.  * Copyright (c) 2007   
  11.  * 说明:  
  12.  */  
  13.   
  14. package com.sclh.rsp.registercenter.service.chart;   
  15.   
  16. import java.awt.Font;   
  17. import java.awt.RenderingHints;   
  18. import java.io.IOException;   
  19. import java.io.PrintWriter;   
  20.   
  21. import javax.servlet.http.HttpSession;   
  22.   
  23. import org.jfree.chart.ChartFactory;   
  24. import org.jfree.chart.ChartRenderingInfo;   
  25. import org.jfree.chart.ChartUtilities;   
  26. import org.jfree.chart.JFreeChart;   
  27. import org.jfree.chart.entity.StandardEntityCollection;   
  28. import org.jfree.chart.plot.PlotOrientation;   
  29. import org.jfree.chart.servlet.ServletUtilities;   
  30. import org.jfree.chart.title.TextTitle;   
  31. import org.jfree.data.category.CategoryDataset;   
  32.   
  33. /**  
  34.  * @author fhway  
  35.  *  
  36.  */  
  37. public class GenerateWebBarChart3D {   
  38.   
  39.     public static String getBarChart3D(CategoryDataset dataset,String title,String xName ,String yName, int width, int height, HttpSession session,PrintWriter pw){   
  40.   
  41.         String filename = null;   
  42.         Font font = null;   
  43.   
  44.         JFreeChart chart = ChartFactory.createBarChart3D(title, xName, yName, dataset, PlotOrientation.VERTICAL, falsefalsefalse);   
  45.            
  46.         chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);   
  47.            
  48.         TextTitle tt=new TextTitle(title);   
  49.         font = new Font("黑体",Font.CENTER_BASELINE,20);//这个地方是设置统计图标题的字体和大小   
  50.         tt.setFont(font);   
  51.         chart.setTitle(tt);   
  52.            
  53.         chart.setBackgroundPaint(new java.awt.Color(244247251)); //统计图片的底色   
  54.            
  55.         ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());   
  56.         try {   
  57.             filename = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);   
  58.             //把image map 写入到 PrintWriter   
  59.             ChartUtilities.writeImageMap(pw, filename, info, true);   
  60.         } catch (IOException e) {   
  61.             System.out.println("Exception - " + e.toString());   
  62.             e.printStackTrace(System.out);   
  63.             filename = "public_error_500x300.png";   
  64.         }   
  65.         pw.flush();   
  66.   
  67.         return filename;   
  68.     }   
  69.   
  70. }   

java 代码
  1. /**  
  2.  * Project name: RCP  
  3.  *  
  4.  * Package name: com.sclh.rsp.registercenter.hibernate.dao.chat  
  5.  * Filename    : GenerateWebPieChart3D.java  
  6.  * @Author     : fhway  
  7.  * DateTime    : 2007-10-18 上午10:21:38  
  8.  * Visoin      : 1.0  
  9.  * Company     : sclh  
  10.  * Copyright (c) 2007   
  11.  * 说明:  
  12.  */  
  13.   
  14. package com.sclh.rsp.registercenter.service.chart;   
  15.   
  16. import java.awt.Font;   
  17. import java.awt.RenderingHints;   
  18. import java.io.PrintWriter;   
  19. import java.text.DecimalFormat;   
  20. import java.text.NumberFormat;   
  21. import java.util.Iterator;   
  22. import java.util.Map;   
  23.   
  24. import javax.servlet.http.HttpSession;   
  25.   
  26. import org.jfree.chart.ChartFactory;   
  27. import org.jfree.chart.ChartRenderingInfo;   
  28. import org.jfree.chart.ChartUtilities;   
  29. import org.jfree.chart.JFreeChart;   
  30. import org.jfree.chart.entity.StandardEntityCollection;   
  31. import org.jfree.chart.labels.StandardPieSectionLabelGenerator;   
  32. import org.jfree.chart.plot.PiePlot;   
  33. import org.jfree.chart.servlet.ServletUtilities;   
  34. import org.jfree.chart.title.TextTitle;   
  35. import org.jfree.data.general.DefaultPieDataset;   
  36.   
  37. /**  
  38.  * @author fhway  
  39.  *  
  40.  */  
  41. public class GenerateWebPieChart3D {   
  42.        
  43.     public static String getPieChart3D(Map map,String title,int width,int height,HttpSession session,PrintWriter pw){   
  44.         String filename = null;   
  45.         Font font = null;   
  46.            
  47.         DefaultPieDataset data = new DefaultPieDataset();   
  48.         Iterator it = null;   
  49.         it = map.entrySet().iterator();   
  50.         while(it.hasNext()){   
  51.             Map.Entry entry = (Map.Entry) it.next();   
  52.             data.setValue(String.valueOf(entry.getKey()), Double.valueOf(String.valueOf(entry.getValue())));   
  53.         }   
  54.         try{   
  55.             JFreeChart chart = ChartFactory.createPieChart3D(title, data, falsefalsefalse);   
  56.             PiePlot plot = (PiePlot)chart.getPlot();   
  57.             plot.setNoDataMessage("查询数据为空!");   
  58.             plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));   
  59.             plot.setForegroundAlpha(0.5f);   
  60.             plot.setCircular(false);   
  61.                
  62.             font = new Font("simsun",Font.PLAIN,12);//这个地方是设置统计图标题的字体和大小   
  63.             plot.setLabelFont(font);   
  64.                
  65.             chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);   
  66.                
  67.             TextTitle tt=new TextTitle(title);   
  68.             font = new Font("黑体",Font.CENTER_BASELINE,20);//这个地方是设置统计图标题的字体和大小   
  69.             tt.setFont(font);   
  70.             chart.setTitle(tt);   
  71.                
  72.             chart.setBackgroundPaint(new java.awt.Color(244247251)); //统计图片的底色   
  73.                
  74.             ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());   
  75.             filename = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);   
  76.             //把image map 写入到 PrintWriter   
  77.             ChartUtilities.writeImageMap(pw, filename, info, true);   
  78.             pw.flush();   
  79.         } catch (Exception e) {   
  80.             System.out.println("Exception - " + e.toString());   
  81.             e.printStackTrace(System.out);   
  82.             filename = "public_error_500x300.png";   
  83.         }   
  84.         return filename;   
  85.     }   
  86. }  

 

 

java 代码
  1. /**  
  2.  * Project name: RCP  
  3.  *  
  4.  * Package name: com.sclh.rsp.registercenter.service.chat  
  5.  * Filename    : ILoggerChat.java  
  6.  * @Author     : fhway  
  7.  * DateTime    : 2007-10-18 下午02:28:48  
  8.  * Visoin      : 1.0  
  9.  * Company     : sclh  
  10.  * Copyright (c) 2007   
  11.  * 说明:  
  12.  */  
  13.   
  14. package com.sclh.rsp.registercenter.service.chart;   
  15.   
  16. import java.io.PrintWriter;   
  17.   
  18. import javax.servlet.http.HttpSession;   
  19.   
  20. /**  
  21.  * @author fhway  
  22.  *  
  23.  */  
  24. public interface IOpLoggerChart {   
  25.   
  26.     /**  
  27.      *   
  28.      * @param title  
  29.      * @param session  
  30.      * @param pw  
  31.      * @return  
  32.      * @throws Exception  
  33.      * fhway 2007-10-18 下午05:21:03  
  34.      */  
  35.     public String getPieChart3D(String title, HttpSession session,PrintWriter pw) throws Exception;   
  36.        
  37.     /**  
  38.      *   
  39.      * @param title  
  40.      * @param xName  
  41.      * @param yName  
  42.      * @param session  
  43.      * @param pw  
  44.      * @return  
  45.      * @throws Exception  
  46.      * fhway 2007-10-18 下午05:21:07  
  47.      */  
  48.     public String getBarChart3D(String title, String xName ,String yName, HttpSession session,PrintWriter pw) throws Exception;   
  49.        
  50. }   
posted @ 2007-10-24 09:45  fhway  阅读(278)  评论(0)    收藏  举报