- aflyer
- 等级:
- 性别:
- 文章: 42
- 积分: 128
- 来自: 成都
|
这是我自己做的一个关于统计的WebDemo;
首先建立一个web项目;在src中加入以下java文件:
GenerateWebBarChart3D.java(用于Bar3D图的显示的)
java 代码
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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;
-
-
-
-
-
- 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);
-
- 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;
- }
-
- }
GenerateWebPieChart3D.java(用于Pie3D图显示的)
java 代码
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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;
-
-
-
-
-
- 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);
-
- 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 代码
IOpLoggerChart.java(调用方法接口文件)
-
-
-
-
-
-
-
-
-
-
-
-
-
- package com.sclh.rsp.registercenter.service.chart;
-
- import java.io.PrintWriter;
-
- import javax.servlet.http.HttpSession;
-
-
-
-
-
- public interface IOpLoggerChart {
-
-
-
-
-
-
-
-
-
-
- public String getPieChart3D(String title, HttpSession session,PrintWriter pw) throws Exception;
-
-
-
-
-
-
-
-
-
-
-
-
- public String getBarChart3D(String title, String xName ,String yName, HttpSession session,PrintWriter pw) throws Exception;
-
- }
OpLoggerChartImpl.java(调用实现)
java 代码
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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;
-
-
-
-
-
- public class OpLoggerChartImpl implements IOpLoggerChart{
-
-
-
- private int width;
- private int height;
-
- private IOpLogger opLoggerDAO;
-
- private Map map;
-
- public OpLoggerChartImpl(){
- map = new HashMap();
- if(width == 0 ){ width = 500;}
- if(height == 0){ height = 400;}
- }
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
-
-
-
-
- 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);
- }
-
-
-
-
- public int getHeight() {
- return height;
- }
-
-
-
- public void setHeight(int height) {
- this.height = height;
- }
-
-
-
- public int getWidth() {
- return width;
- }
-
-
-
- public void setWidth(int width) {
- this.width = width;
- }
-
-
-
-
- public Map getMap() {
- return map;
- }
-
-
-
-
- public void setMap(Map map) {
- this.map = map;
- }
-
-
-
-
- public IOpLogger getOpLoggerDAO() {
- return opLoggerDAO;
- }
-
-
-
-
- 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");
-
-
- }
-
-
-
-
- }
OpLoggerChart.jsp(jsp调用实现)
js 代码
- <%@ 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.getBarChart3D("日志数据操作统计图", "日志类型", "数值", 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>
java 代码
代码基本就是这些,下面是项目的配置问题:
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 '日志表';
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|