Jfreechart与Tapestry4.0 结合实现(下)
5)Java类文件:
??? 5-1)StatChartServic.java类:
java 代码?
- package?com.ce.jfreechart.service;??
- ??
- import?java.awt.Color;??
- import?java.io.IOException;??
- import?java.text.SimpleDateFormat;??
- import?java.util.Calendar;??
- import?java.util.Date;??
- import?java.util.HashMap;??
- import?java.util.Map;??
- ??
- import?javax.servlet.http.HttpServletRequest;??
- import?javax.servlet.http.HttpServletResponse;??
- ??
- import?org.apache.tapestry.IRequestCycle;??
- import?org.apache.tapestry.engine.IEngineService;??
- import?org.apache.tapestry.engine.ILink;??
- import?org.apache.tapestry.services.Infrastructure;??
- import?org.apache.tapestry.services.LinkFactory;??
- import?org.jfree.chart.ChartFactory;??
- import?org.jfree.chart.ChartUtilities;??
- import?org.jfree.chart.JFreeChart;??
- import?org.jfree.chart.axis.CategoryAxis;??
- import?org.jfree.chart.axis.DateAxis;??
- import?org.jfree.chart.labels.StandardCategoryItemLabelGenerator;??
- import?org.jfree.chart.plot.CategoryPlot;??
- import?org.jfree.chart.plot.PiePlot;??
- import?org.jfree.chart.plot.PlotOrientation;??
- import?org.jfree.chart.plot.XYPlot;??
- import?org.jfree.chart.renderer.category.BarRenderer3D;??
- import?org.jfree.chart.renderer.xy.XYItemRenderer;??
- import?org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;??
- import?org.jfree.chart.urls.StandardPieURLGenerator;??
- import?org.jfree.data.category.DefaultCategoryDataset;??
- import?org.jfree.data.general.DefaultPieDataset;??
- import?org.jfree.data.time.Day;??
- import?org.jfree.data.time.Hour;??
- import?org.jfree.data.time.Minute;??
- import?org.jfree.data.time.Month;??
- import?org.jfree.data.time.TimeSeries;??
- import?org.jfree.data.time.TimeSeriesCollection;??
- import?org.jfree.ui.RectangleInsets;??
- import?com.ce.jfreechart.vo.PieChartVO;??
- ??
- /**?
- ?*?功能描述:生成统计结果图的服务类(现主要针对胼图)??
- ?*/??
- public?class?StatChartService?implements?IEngineService?{??
- ????private?HttpServletRequest?request;??
- ??
- ????private?HttpServletResponse?response;??
- ??
- ????private?LinkFactory?linkFactory;??
- ??
- ????private?Infrastructure?infrastructure;??
- ??
- ????private?final?static?String?SERVICE_NAME?=?"chart";??
- ??
- ????public?final?static?String?CHART_PARAS_OBJ_NAME?=?"pcVo";??
- ??
- ????public?void?setResponse(HttpServletResponse?response)?{??
- ????????this.response?=?response;??
- ????}??
- ??
- ????public?void?setRequest(HttpServletRequest?request)?{??
- ????????this.request?=?request;??
- ????}??
- ??
- ????public?void?setLinkFactory(LinkFactory?linkFactory)?{??
- ????????this.linkFactory?=?linkFactory;??
- ????}??
- ??
- ????public?ILink?getLink(boolean?post,?Object?parameter)?{??
- ????????Map?parameters?=?new?HashMap();??
- ????????return?linkFactory.constructLink(this,?post,?parameters,?false);??
- ????}??
- ??
- ????public?void?setInfrastructure(Infrastructure?infrastructure)?{??
- ????????this.infrastructure?=?infrastructure;??
- ????}??
- ??
- ????public?String?getName()?{??
- ????????return?SERVICE_NAME;??
- ????}??
- ??
- ????public?void?service(IRequestCycle?cycle)?throws?IOException?{??
- ????????PieChartVO?pcVo?=?(PieChartVO)?request.getSession()??
- ????????????????.getAttribute(CHART_PARAS_OBJ_NAME);??
- ????????if?(pcVo?==?null)?{??
- ????????????return;??
- ????????}??
- ??
- ????????Chart?chart?=?new?Chart();??
- ??
- ????????try?{??
- ????????????chart.getPieChart3D(request,?response,?pcVo);??
- ????????}?finally?{??
- ????????????request.getSession().removeAttribute(CHART_PARAS_OBJ_NAME);??
- ????????}??
- ????}??
- }??
- ??
- /**?
- ?*?功能描述:输出JFreeChart图的类?
- ?*/??
- class?Chart?{??
- ??
- ????/**?
- ?????*?方法描述:生成3D胼状图的方法?
- ?????*??
- ?????*?@param?request?
- ?????*????????????:HttpServletRequest?
- ?????*?@param?response?
- ?????*????????????:HttpServletResponse?
- ?????*?@param?PieChartVO??
- ?????*????????????:参数传递vo?
- ?????*?@throws?IOException?
- ?????*/??
- ????public?void?getPieChart3D(HttpServletRequest?request,??
- ????????????HttpServletResponse?response,?PieChartVO?pcVo)?throws?IOException?{??
- ????????response.setDateHeader("Expires",?0);??
- ????????response.setContentType("image/jpeg");??
- ??
- ????????String?title?=?pcVo.getTitle();??
- ??
- ????????String[]?name?=?pcVo.getName();??
- ??
- ????????double[]?value?=?pcVo.getValue();??
- ??
- ????????int?width?=?pcVo.getWidth();??
- ??
- ????????int?hight?=?pcVo.getHight();??
- ??
- ????????if?(title?==?null?||?"".equals(title))?{??
- ????????????title?=?"?";??
- ????????}??
- ????????if?(name?==?null?||?name.length?==?0?||?value?==?null??
- ????????????????||?value.length?==?0)?{??
- ????????????return;??
- ????????}??
- ????????if?(width?<=?0)?{??
- ????????????width?=?400;??
- ????????}??
- ????????if?(hight?<=?0)?{??
- ????????????hight?=?400;??
- ????????}??
- ??
- ????????DefaultPieDataset?defaultpiedataset?=?new?DefaultPieDataset();??
- ????????int?j?=?name.length?<=?value.length???name.length?:?value.length;??
- ????????for?(int?i?=?0;?i?<?j;?i++)?{??
- ????????????defaultpiedataset.setValue(name[i],?value[i]);??
- ????????}??
- ??
- ????????JFreeChart?chart?=?ChartFactory.createPieChart3D(title,??
- ????????????????defaultpiedataset,?true,?true,?false);??
- ??
- ????????chart.setBackgroundPaint(Color.WHITE);??
- ????????PiePlot?plot?=?(PiePlot)?chart.getPlot();?????
- ????????plot.setNoDataMessage("No?data?available");?????
- ????????ChartUtilities.writeChartAsJPEG(response.getOutputStream(),?chart,??
- ????????????????width,?hight);??
- ????}??
- ??
- }??
java 代码?
- package?com.ce.jfreechart.page;??
- ??
- import?javax.servlet.http.HttpServletRequest;??
- ??
- import?org.apache.tapestry.annotations.InjectObject;??
- import?org.apache.tapestry.event.PageBeginRenderListener;??
- import?org.apache.tapestry.event.PageEvent;??
- ??
- import?com.ce.jfreechart.service.StatChartService;??
- import?com.ce.jfreechart.vo.PieChartVO;??
- ??
- /**?
- ?*?功能描述:公司人员统计页?
- ?*/??
- public?abstract?class?PieChartPage?extends?org.apache.tapestry.html.BasePage?implements?PageBeginRenderListener{??
- ????private?final?String?title??=?"公司人员统计饼状图";??
- ??????
- ????@InjectObject("service:tapestry.globals.HttpServletRequest)?
- ????public?abstract?HttpServletRequest?getRequest();?
- ?????
- ????/**??
- ?????*?页面开始表现时触发,或form重表现时也会触发?
- ????*/???
- ????public?void?pageBeginRender(PageEvent?event)?{?
- ?????????
- ????????if?(!event.getRequestCycle().isRewinding())?{?
- ????????????try{?????
- ?????????????????
- ????????????????//画饼图?
- ????????????????String[]?name?=?{"管理人员","市场人员","开发人员","OEM人员","其他人员"};??
- ????????????????double[]?value?=?{10.02D,20.23D,56.80D,12.30D,5.98D};??
- ????????????????PieChartVO?pcVo?=?new?PieChartVO();??
- ????????????????pcVo.setTitle(title);??
- ????????????????pcVo.setName(name);??
- ????????????????pcVo.setValue(value);??
- ????????????????pcVo.setWidth(450);??
- ????????????????pcVo.setHight(300);??
- ????????????????if(this.getRequest().getSession().getAttribute(StatChartService.CHART_PARAS_OBJ_NAME)==null)???
- ????????????????????this.getRequest().getSession().setAttribute(StatChartService.CHART_PARAS_OBJ_NAME,?pcVo);??
- ????????????}catch(Exception?e){??
- ????????????????e.printStackTrace();??
- ????????????}?????
- ????????}??
- ????}??
- }??
- /**?
- ?*?功能描述:生成JFreeChart饼图的参数Vo对象?
- ?*/??
- public?class?PieChartVO?implements?Serializable?{??
- ??
- ????private?static?final?long?serialVersionUID?=?1L;??
- ??
- ????/**?
- ?????*?图表标题?
- ?????*/??
- ????private?String?title;??
- ??
- ????/**?
- ?????*?具体项名称数组?
- ?????*/??
- ????private?String[]?name;??
- ??
- ????/**?
- ?????*?具体项值数组?
- ?????*/??
- ????private?double[]?value;??
- ??
- ????/**?
- ?????*?图表宽度?
- ?????*/??
- ????private?int?width;??
- ??
- ????/**?
- ?????*?图表高度?
- ?????*/??
- ????private?int?hight;??
- ??
- ????方法略......??
- }??
xml 代码?
- >??
- <html>??
- ??<head>??
- ????<title>PieChart.html<!---->title>??
- ??????
- ????<meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3">??
- ????<meta?http-equiv="description"?content="this?is?my?page">??
- ????<meta?http-equiv="content-type"?content="text/html;?charset=UTF-8">??
- ??????
- ????<!---->??
- ??
- ??<!---->head>??
- ????
- ??<body>??
- ??<!---->??
- ??????????????<div?align="center"><img?src="app?service=chart"?border="0"/><!---->div>??
- ??<!---->body>??
- <!---->html>??
xml 代码?
- xml?version="1.0"?encoding="GBK"?>??
- ??"-//Apache?Software?Foundation//Tapestry?Specification?4.0//EN"???
- ??"http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">??
- ??
- <page-specification?class="com.ce.jfreechart.page.PieChartPage">??????????
- <!---->page-specification>??
如你上面的:
String[] name = {"管理人员","市场人员","开发人员","OEM人员","管理人员"};
double[] value = {10.02D,20.23D,56.80D,12.30D,5.98D};
PieChartVO pcVo = new PieChartVO();
pcVo.setTitle(title); 6
pcVo.setName(name);
pcVo.setValue(value);
上面的name有二个:管理人员,也就是显示图形时只会显示最后一名称的值,如何把这二个值相加显示呢? 2 楼 kevin_gzhz 2007-06-14 对于在name里出现两个相同"管理人员"的情况我没有尝试过,不过这些都是根据需求自定义的,出现这种情况应该是自己手动去合并数值才是对的,没必要去测试JFreeChart的智能有多高!!