读书人

Jfreechart与Tapestry4.0 结合实现(上)

发布时间: 2012-11-12 12:31:56 作者: rapoo

Jfreechart与Tapestry4.0 结合实现(下)
5)Java类文件:
??? 5-1)StatChartServic.java类:
java 代码?

  1. package?com.ce.jfreechart.service;??
  2. ??
  3. import?java.awt.Color;??
  4. import?java.io.IOException;??
  5. import?java.text.SimpleDateFormat;??
  6. import?java.util.Calendar;??
  7. import?java.util.Date;??
  8. import?java.util.HashMap;??
  9. import?java.util.Map;??
  10. ??
  11. import?javax.servlet.http.HttpServletRequest;??
  12. import?javax.servlet.http.HttpServletResponse;??
  13. ??
  14. import?org.apache.tapestry.IRequestCycle;??
  15. import?org.apache.tapestry.engine.IEngineService;??
  16. import?org.apache.tapestry.engine.ILink;??
  17. import?org.apache.tapestry.services.Infrastructure;??
  18. import?org.apache.tapestry.services.LinkFactory;??
  19. import?org.jfree.chart.ChartFactory;??
  20. import?org.jfree.chart.ChartUtilities;??
  21. import?org.jfree.chart.JFreeChart;??
  22. import?org.jfree.chart.axis.CategoryAxis;??
  23. import?org.jfree.chart.axis.DateAxis;??
  24. import?org.jfree.chart.labels.StandardCategoryItemLabelGenerator;??
  25. import?org.jfree.chart.plot.CategoryPlot;??
  26. import?org.jfree.chart.plot.PiePlot;??
  27. import?org.jfree.chart.plot.PlotOrientation;??
  28. import?org.jfree.chart.plot.XYPlot;??
  29. import?org.jfree.chart.renderer.category.BarRenderer3D;??
  30. import?org.jfree.chart.renderer.xy.XYItemRenderer;??
  31. import?org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;??
  32. import?org.jfree.chart.urls.StandardPieURLGenerator;??
  33. import?org.jfree.data.category.DefaultCategoryDataset;??
  34. import?org.jfree.data.general.DefaultPieDataset;??
  35. import?org.jfree.data.time.Day;??
  36. import?org.jfree.data.time.Hour;??
  37. import?org.jfree.data.time.Minute;??
  38. import?org.jfree.data.time.Month;??
  39. import?org.jfree.data.time.TimeSeries;??
  40. import?org.jfree.data.time.TimeSeriesCollection;??
  41. import?org.jfree.ui.RectangleInsets;??
  42. import?com.ce.jfreechart.vo.PieChartVO;??
  43. ??
  44. /**?
  45. ?*?功能描述:生成统计结果图的服务类(现主要针对胼图)??
  46. ?*/??
  47. public?class?StatChartService?implements?IEngineService?{??
  48. ????private?HttpServletRequest?request;??
  49. ??
  50. ????private?HttpServletResponse?response;??
  51. ??
  52. ????private?LinkFactory?linkFactory;??
  53. ??
  54. ????private?Infrastructure?infrastructure;??
  55. ??
  56. ????private?final?static?String?SERVICE_NAME?=?"chart";??
  57. ??
  58. ????public?final?static?String?CHART_PARAS_OBJ_NAME?=?"pcVo";??
  59. ??
  60. ????public?void?setResponse(HttpServletResponse?response)?{??
  61. ????????this.response?=?response;??
  62. ????}??
  63. ??
  64. ????public?void?setRequest(HttpServletRequest?request)?{??
  65. ????????this.request?=?request;??
  66. ????}??
  67. ??
  68. ????public?void?setLinkFactory(LinkFactory?linkFactory)?{??
  69. ????????this.linkFactory?=?linkFactory;??
  70. ????}??
  71. ??
  72. ????public?ILink?getLink(boolean?post,?Object?parameter)?{??
  73. ????????Map?parameters?=?new?HashMap();??
  74. ????????return?linkFactory.constructLink(this,?post,?parameters,?false);??
  75. ????}??
  76. ??
  77. ????public?void?setInfrastructure(Infrastructure?infrastructure)?{??
  78. ????????this.infrastructure?=?infrastructure;??
  79. ????}??
  80. ??
  81. ????public?String?getName()?{??
  82. ????????return?SERVICE_NAME;??
  83. ????}??
  84. ??
  85. ????public?void?service(IRequestCycle?cycle)?throws?IOException?{??
  86. ????????PieChartVO?pcVo?=?(PieChartVO)?request.getSession()??
  87. ????????????????.getAttribute(CHART_PARAS_OBJ_NAME);??
  88. ????????if?(pcVo?==?null)?{??
  89. ????????????return;??
  90. ????????}??
  91. ??
  92. ????????Chart?chart?=?new?Chart();??
  93. ??
  94. ????????try?{??
  95. ????????????chart.getPieChart3D(request,?response,?pcVo);??
  96. ????????}?finally?{??
  97. ????????????request.getSession().removeAttribute(CHART_PARAS_OBJ_NAME);??
  98. ????????}??
  99. ????}??
  100. }??
  101. ??
  102. /**?
  103. ?*?功能描述:输出JFreeChart图的类?
  104. ?*/??
  105. class?Chart?{??
  106. ??
  107. ????/**?
  108. ?????*?方法描述:生成3D胼状图的方法?
  109. ?????*??
  110. ?????*?@param?request?
  111. ?????*????????????:HttpServletRequest?
  112. ?????*?@param?response?
  113. ?????*????????????:HttpServletResponse?
  114. ?????*?@param?PieChartVO??
  115. ?????*????????????:参数传递vo?
  116. ?????*?@throws?IOException?
  117. ?????*/??
  118. ????public?void?getPieChart3D(HttpServletRequest?request,??
  119. ????????????HttpServletResponse?response,?PieChartVO?pcVo)?throws?IOException?{??
  120. ????????response.setDateHeader("Expires",?0);??
  121. ????????response.setContentType("image/jpeg");??
  122. ??
  123. ????????String?title?=?pcVo.getTitle();??
  124. ??
  125. ????????String[]?name?=?pcVo.getName();??
  126. ??
  127. ????????double[]?value?=?pcVo.getValue();??
  128. ??
  129. ????????int?width?=?pcVo.getWidth();??
  130. ??
  131. ????????int?hight?=?pcVo.getHight();??
  132. ??
  133. ????????if?(title?==?null?||?"".equals(title))?{??
  134. ????????????title?=?"?";??
  135. ????????}??
  136. ????????if?(name?==?null?||?name.length?==?0?||?value?==?null??
  137. ????????????????||?value.length?==?0)?{??
  138. ????????????return;??
  139. ????????}??
  140. ????????if?(width?<=?0)?{??
  141. ????????????width?=?400;??
  142. ????????}??
  143. ????????if?(hight?<=?0)?{??
  144. ????????????hight?=?400;??
  145. ????????}??
  146. ??
  147. ????????DefaultPieDataset?defaultpiedataset?=?new?DefaultPieDataset();??
  148. ????????int?j?=?name.length?<=?value.length???name.length?:?value.length;??
  149. ????????for?(int?i?=?0;?i?<?j;?i++)?{??
  150. ????????????defaultpiedataset.setValue(name[i],?value[i]);??
  151. ????????}??
  152. ??
  153. ????????JFreeChart?chart?=?ChartFactory.createPieChart3D(title,??
  154. ????????????????defaultpiedataset,?true,?true,?false);??
  155. ??
  156. ????????chart.setBackgroundPaint(Color.WHITE);??
  157. ????????PiePlot?plot?=?(PiePlot)?chart.getPlot();?????
  158. ????????plot.setNoDataMessage("No?data?available");?????
  159. ????????ChartUtilities.writeChartAsJPEG(response.getOutputStream(),?chart,??
  160. ????????????????width,?hight);??
  161. ????}??
  162. ??
  163. }??
PieChartPage.java页面类
java 代码?
  1. package?com.ce.jfreechart.page;??
  2. ??
  3. import?javax.servlet.http.HttpServletRequest;??
  4. ??
  5. import?org.apache.tapestry.annotations.InjectObject;??
  6. import?org.apache.tapestry.event.PageBeginRenderListener;??
  7. import?org.apache.tapestry.event.PageEvent;??
  8. ??
  9. import?com.ce.jfreechart.service.StatChartService;??
  10. import?com.ce.jfreechart.vo.PieChartVO;??
  11. ??
  12. /**?
  13. ?*?功能描述:公司人员统计页?
  14. ?*/??
  15. public?abstract?class?PieChartPage?extends?org.apache.tapestry.html.BasePage?implements?PageBeginRenderListener{??
  16. ????private?final?String?title??=?"公司人员统计饼状图";??
  17. ??????
  18. ????@InjectObject("service:tapestry.globals.HttpServletRequest)?
  19. ????public?abstract?HttpServletRequest?getRequest();?
  20. ?????
  21. ????/**??
  22. ?????*?页面开始表现时触发,或form重表现时也会触发?
  23. ????*/???
  24. ????public?void?pageBeginRender(PageEvent?event)?{?
  25. ?????????
  26. ????????if?(!event.getRequestCycle().isRewinding())?{?
  27. ????????????try{?????
  28. ?????????????????
  29. ????????????????//画饼图?
  30. ????????????????String[]?name?=?{"管理人员","市场人员","开发人员","OEM人员","其他人员"};??
  31. ????????????????double[]?value?=?{10.02D,20.23D,56.80D,12.30D,5.98D};??
  32. ????????????????PieChartVO?pcVo?=?new?PieChartVO();??
  33. ????????????????pcVo.setTitle(title);??
  34. ????????????????pcVo.setName(name);??
  35. ????????????????pcVo.setValue(value);??
  36. ????????????????pcVo.setWidth(450);??
  37. ????????????????pcVo.setHight(300);??
  38. ????????????????if(this.getRequest().getSession().getAttribute(StatChartService.CHART_PARAS_OBJ_NAME)==null)???
  39. ????????????????????this.getRequest().getSession().setAttribute(StatChartService.CHART_PARAS_OBJ_NAME,?pcVo);??
  40. ????????????}catch(Exception?e){??
  41. ????????????????e.printStackTrace();??
  42. ????????????}?????
  43. ????????}??
  44. ????}??
  45. }??
参数Vo对象:java 代码?
  1. /**?
  2. ?*?功能描述:生成JFreeChart饼图的参数Vo对象?
  3. ?*/??
  4. public?class?PieChartVO?implements?Serializable?{??
  5. ??
  6. ????private?static?final?long?serialVersionUID?=?1L;??
  7. ??
  8. ????/**?
  9. ?????*?图表标题?
  10. ?????*/??
  11. ????private?String?title;??
  12. ??
  13. ????/**?
  14. ?????*?具体项名称数组?
  15. ?????*/??
  16. ????private?String[]?name;??
  17. ??
  18. ????/**?
  19. ?????*?具体项值数组?
  20. ?????*/??
  21. ????private?double[]?value;??
  22. ??
  23. ????/**?
  24. ?????*?图表宽度?
  25. ?????*/??
  26. ????private?int?width;??
  27. ??
  28. ????/**?
  29. ?????*?图表高度?
  30. ?????*/??
  31. ????private?int?hight;??
  32. ??
  33. ????方法略......??
  34. }??
PieChart.html页面文件
xml 代码?
  1. >??
  2. <html>??
  3. ??<head>??
  4. ????<title>PieChart.html<!---->title>??
  5. ??????
  6. ????<meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3">??
  7. ????<meta?http-equiv="description"?content="this?is?my?page">??
  8. ????<meta?http-equiv="content-type"?content="text/html;?charset=UTF-8">??
  9. ??????
  10. ????<!---->??
  11. ??
  12. ??<!---->head>??
  13. ????
  14. ??<body>??
  15. ??<!---->??
  16. ??????????????<div?align="center"><img?src="app?service=chart"?border="0"/><!---->div>??
  17. ??<!---->body>??
  18. <!---->html>??
PieChart.page文件
xml 代码?
  1. xml?version="1.0"?encoding="GBK"?>??
  2. ??"-//Apache?Software?Foundation//Tapestry?Specification?4.0//EN"???
  3. ??"http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">??
  4. ??
  5. <page-specification?class="com.ce.jfreechart.page.PieChartPage">??????????
  6. <!---->page-specification>??
1 楼 axiang_2898 2007-05-25 你好,关于jfreechart放入值的问题:
如你上面的:
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的智能有多高!!

读书人网 >软件架构设计

热点推荐