- kevin_gzhz
- 等级: 初级会员
- 性别:
- 文章: 20
- 积分: 47
|
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);
- }
- }
- }
-
-
-
-
- class Chart {
-
-
-
-
-
-
-
-
-
-
-
-
- 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);
- }
-
- }
PieChartPage.java页面类
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();
- }
- }
- }
- }
参数Vo对象:
java 代码
-
-
-
- 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;
-
- 方法略......
- }
PieChart.html页面文件
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>
PieChart.page文件
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>
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|
- axiang_2898
- 等级: 初级会员
- 性别:
- 文章: 44
- 积分: 40
- 来自: 上海
|
你好,关于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有二个:管理人员,也就是显示图形时只会显示最后一名称的值,如何把这二个值相加显示呢?
|
返回顶楼 |
|
|
- kevin_gzhz
- 等级: 初级会员
- 性别:
- 文章: 20
- 积分: 47
|
对于在name里出现两个相同"管理人员"的情况我没有尝试过,不过这些都是根据需求自定义的,出现这种情况应该是自己手动去合并数值才是对的,没必要去测试JFreeChart的智能有多高!!
|
返回顶楼 |
|
|