Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1449563
  • 博文数量: 3500
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 43870
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-03 20:31
文章分类

全部博文(3500)

文章存档

2008年(3500)

我的朋友

分类:

2008-05-04 19:08:02

一起学习
三.使创建WEB图表更简单 上面我们讲了在WEB中创建图表的过程,以及给出了一个例子;但我觉得这样还不够简单。如果我们把图表在WEB中显示的过程封装起来,在创建WEB图表的过程中只实现创建图表,这样使创建WEB图表的过程简化和创建一般的图表一样简单。 下面是我的设计: 设计一个WEBChart类,它是一个abstract 类,有一个abstract 方法CreateChart; 要创一个WEB图表只需要继承WEBChart类实现它的CreateChart方法; /** * Web页显示图片的基类,继承这个类,通过实现CreateChart方法产生图象; */ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.jfree.chart.servlet.ChartDeleter; import org.jfree.chart.servlet.DisplayChart; import org.jfree.chart.servlet.ServletUtilities; import org.jfree.chart.*; public abstract class WebChart { public String url = null; public WebChart(HttpServletRequest request, HttpServletResponse response, int height, int weight ) { log("web Chart Create"); url=CreateImageFile(request,response,height,weight); } /** * 方法用于获取图片文件文件名; * request,response用于传递参数,转发请求 * heith:为图片的高度 * weight:为图片的宽度 */ public String CreateImageFile(HttpServletRequest request, HttpServletResponse response, int height, int weight) { try { String filename = null; HttpSession session = request.getSession(); //Create Chart JFreeChart chart = CreateChart(request, response); filename = ServletUtilities.saveChartAsPNG(chart, height, weight, session); ServletUtilities.sendTempFile(filename, response); //log("image filename create :" filename); // return Image File name return request.getContextPath() "/servlet/DisplayChart?filename=" filename; } catch (Exception e) { // log("WebChart: " e.getMessage()); } return null; //log("ImageFactory Create"); } /** * abstract 返回一个JFreeChart对象 * request,response用于传递参数,转发请求 */ protected abstract JFreeChart CreateChart(HttpServletRequest request, HttpServletResponse response); } 设计一个WebChartFactory类,它有一个static 方法CreateImage用于创建一个WEBChart子类,返回图表图象的url; public class WebChartFactory { public WebChartFactory() { System.out.println("WebChartFactory Create"); } public static String CreateImage(WebChart webchart){ //System.out.println(webchart.getClass()); return webchart.url; } }; 在Jsp中显示图象,只需要如下处理 WebChartSubClass是WebChart的子类实现了只是实现了CreateChart方法; 文章到此,也算完结了。如果您有任何建议和疑问请写信和我联系。 Email:ugrock@sohu.com 下载本文示例代码


简化JFreeChart创建WEB图表简化JFreeChart创建WEB图表简化JFreeChart创建WEB图表简化JFreeChart创建WEB图表简化JFreeChart创建WEB图表简化JFreeChart创建WEB图表简化JFreeChart创建WEB图表简化JFreeChart创建WEB图表简化JFreeChart创建WEB图表简化JFreeChart创建WEB图表简化JFreeChart创建WEB图表简化JFreeChart创建WEB图表
阅读(144) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~