论坛首页 Java企业应用论坛

JFreeChart1.0.6的一个通用Web Demo

浏览 13559 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-10-24  

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

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

GenerateWebBarChart3D.java(用于Bar3D图的显示的) 

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.            
  47.         chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);   
  48.            
  49.         TextTitle tt=new TextTitle(title);   
  50.         font = new Font("黑体",Font.CENTER_BASELINE,20);//这个地方是设置统计图标题的字体和大小   
  51.         tt.setFont(font);   
  52.         chart.setTitle(tt);   
  53.            
  54.         chart.setBackgroundPaint(new java.awt.Color(244247251)); //统计图片的底色   
  55.            
  56.         ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());   
  57.         try {   
  58.             filename = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);   
  59.             //把image map 写入到 PrintWriter   
  60.             ChartUtilities.writeImageMap(pw, filename, info, true);   
  61.         } catch (IOException e) {   
  62.             System.out.println("Exception - " + e.toString());   
  63.             e.printStackTrace(System.out);   
  64.             filename = "public_error_500x300.png";   
  65.         }   
  66.         pw.flush();   
  67.   
  68.         return filename;   
  69.     }   
  70.   
  71. }   

GenerateWebPieChart3D.java(用于Pie3D图显示的)

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 代码
 IOpLoggerChart.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. }   

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调用实现)

js 代码
  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.getBarChart3D("日志数据操作统计图""日志类型""数值", 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>  
java 代码

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

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 '日志表';   
   发表时间:2007-10-24  
有那么复杂吗?
0 请登录后投票
   发表时间:2007-10-25  
嘿嘿,我就用了一个接口,一个bean,一个数据类,一个生成类,
一个标签,最后在封装成工厂,就可以实现柱,饼,线,能力图,
而且可以在页面自定义,全部plot参数哦
小自恋一把。。。
0 请登录后投票
   发表时间:2007-10-26  
jfreechart有那么复杂么?
一个写一个 Dataset 一个servlet就搞定了把

其他类不用怎么改动
0 请登录后投票
   发表时间:2007-10-26  
asdfasdf 写道
jfreechart有那么复杂么?
一个写一个 Dataset 一个servlet就搞定了把

其他类不用怎么改动

以前有个人这样做,在压力测试的时候容器非常容易崩溃,虚拟机分配了4G内存。
0 请登录后投票
   发表时间:2007-10-30  
jfreechart没有那么复杂,把包导进来自己改写一下生成报表体的代码,就OK了,我曾经给jfreechart的SpiderWebPlot(蜘蛛网图/雷达图的一种)加上了刻度,和刻度标签,还算比较容易改的。
0 请登录后投票
   发表时间:2007-11-01  
呵呵!不是代码复杂,而是我贴的代码是生产用的代码.使用默认的方式当然简单了.
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics