`

JFreeChart

阅读更多

关键字: jfreechart 自己最近学习了JfreeChart 的使用,觉得这个冬冬的功能非常强大,总结一下。主要介绍JfreeChart 生成图片的几种方式

先从网上找点介绍。
一、简介
    WW 的发展使得基于因特网的应用程序不再局限于静态或者简单的动态内容提供。传统的一些以软件包形式发布应用程序例如报表系统等都在逐渐搬到因特网上。但是这 两者之间有着天壤之别,虽然对于数据获取、业务处理等方面基本类似,但是最大的差别在于用户界面。为了能在web浏览器上显示要求用户界面使用 HTML以及图片的方式来展现数据,而传统的一些利用操作系统本身的控件来开发的用户界面无法适应琳琅满目的客户端,因此在这里也变得无能为力。回到本文 的题目上来,为了创建一个可以在web浏览器上查看到图表一般有两种做法:第一种就是使用applet利用java本身对图形的支持来显示一个图表;第二 种就是直接在web服务器端生成好图表图片文件后发送给浏览器。第一种方式显然对于客户端要求太高,随着现在主流浏览器放弃对JAVA的支持后,这种方式 只适合一些局域网的应用,而对于因特网的环境就显得不太适合。因此我们下面将介绍一个JAVA的图表引擎JFreeChart 用来产生基于WEB的图表。
    JFreeChart 项目简介 JFreeChart 是开放源代码站点SourceForge.net上的一个JAVA项目,它主要用来各种各样的图表,这些图表包括:饼图、柱状图(普 通柱状图以及堆栈柱状图)、线图、区域图、分布图、混合图、甘特图以及一些仪表盘等等。这些不同式样的图表基本上可以满足目前的要求。

二、JFreeChart 获取。

         JFreeChartJFreeChart 公司在开源网站SourceForge.net上的一个项目,该公司的主要产品有如下:
         1.JFreeReport :报表解决工具
         2.JFreeChart :Java 图形解决方案(Application/Applet/Servlet/Jsp)
         3.JCommon :JFreeReport和JFreeChart 的公共类库
         4.JFreeDesigner :JFreeReport的报表设计工具
     我们可以从jfree官方网站上获取最新版本和相关资料(但是jfree的document需要40美金才能获取),
     获取地址:http://www.jfree.org/jfreechart /index.html(同时可以获得简明介绍)
     我们以当前最新版本:jfreechart -1.0.1.zip为例子进行说明。

三、JFreeChart 配置安装
     1 、解压jfreechart -1.0.1.zip.zip到指定位置,其中source是jfreechart 的源码,jfreechart -1.0.1-demo.jar   是例子程序,可以先运行一下看看各种效果,就知道他的nb之处了。
    2 、为了配置成功,我们需要关注的文件有如下三个:
    设置classpath。加入下面三个jar包。
    jfreechart -1.0.1.jar 、jcommon-1.0.0.jar、gnujaxp.jar
    加上第三个jar包有时web.xml会报错,把它去掉就好了。
        至此jfreechart 的配置就完成了,下面就可以进行jfreechart 的开发了。这里值得提出的是jfreechart 的类
        结构设计前后兼容性不是很好,不同版本的jfreechart 中类库结构可能不一样,有时候可能需要查源码。如果是中文显示的时候可能依据观感需要改变源码的字体。


四、JFreeChart 功能介绍
     JFreeChart 目前是最好的java图形解决方案,基本能够解决目前的图形方面的需求,主要包括如下几个方面:
     pie   charts   (2D   and   3D) :饼图(平面和立体)
     bar   charts   (regular   and   stacked,   with   an   optional   3D   effect) :柱状图
     line   and   area   charts :曲线图
     scatter   plots   and   bubble   charts
     time   series,   high/low/open/close   charts   and   candle   stick   charts :时序图
     combination   charts :复合图
     Pareto   charts
     Gantt   charts :甘特图
     wind   plots,   meter   charts   and   symbol   charts
     wafer   map   charts
     ( 态图表,饼图(二维和三维)   ,   柱状图   ( 水平,垂直),线图,点图,时间变化图,甘特图,   股票行情图,混和图,   温度计图,   刻度图等常用商用图表)
     图形可以导出成PNG和JPEG格式,同时还可以与PDF和EXCEL关联
     JFreeChart 核心类库介绍:
             研究jfreechart 源码发现源码的主要由两个大的包组成:org.jfree.chart,org.jfree.data。其中前者主要与图形
     本身有关,后者与图形显示的数据有关。具体研究如果大家有兴趣的话可以自己研究    。
          核心类主要有:
            org.jfree.chart.JFreeChart :图表对象,任何类型的图表的最终表现形式都是在该对象进行一些属性的定制。JFreeChart 引擎本身提供了一个工厂类用于创建不同类型的图表对象
            org.jfree.data.category.XXXDataSet: 数据集对象,用于提供显示图表所用的数据。根据不同类型的图表对应着很多类型的数据集对象类
            org.jfree.chart.plot.XXXPlot :图表区域对象,基本上这个对象决定着什么样式的图表,创建该对象的时候需要Axis、Renderer以及数据集对象的支持
            org.jfree.chart.axis.XXXAxis :用于处理图表的两个轴:纵轴和横轴
            org.jfree.chart.render.XXXRender :负责如何显示一个图表对象
            org.jfree.chart.urls.XXXURLGenerator: 用于生成Web图表中每个项目的鼠标点击链接
            XXXXXToolTipGenerator: 用于生成图象的帮助提示,不同类型图表对应不同类型的工具提示类

    个人感觉JFreeChart 可以满足大部分图片创建的需要,美中不足的是:对字体的设置做的不够好,特别是使用中文的时候字体很不清晰。因为这个原因建议你自己去修改他的源代码,最好使用properties文件去设置字体.还有就是文档要钱所以要多花点时间去看源代码。或多上社区.

五.开始开发
    对JfreeChart 有了初步了解并做好准备工作之后,开始作例子程序试验。在这里我只介绍饼图的做法,而这张图采用不同的方式进行输出,其他类型的图片可以参考jfreechart 提供的例子,做法都差不多。

1) 直接生成图片

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ltc603.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=public%20class%20PieChartPicture%20%7B%0Apublic%20static%20void%20main(String%5B%5D%20args)%20%0A%7B%0A%20PieDataset%20dataset%20%3D%20getDataSet()%3B%0A%20JFreeChart%20chart%20%3D%20ChartFactory.createPieChart3D(%0A%20%20%20%20%22%20%E9%A1%B9%E7%9B%AE%E8%BF%9B%E5%BA%A6%E5%88%86%E5%B8%83%22%2C%20%2F%2F%20chart%20title%0A%20%20%20%20dataset%2C%2F%2F%20data%0A%20%20%20%20true%2C%2F%2F%20include%20legend%0A%20%20%20%20true%2C%0A%20%20%20%20false%0A%20%20%20)%3B%0A%20%20PiePlot3D%20%20plot%3D(PiePlot3D)chart.getPlot()%3B%0A%20%20%20%20%2F%2F%20%E5%9B%BE%E7%89%87%E4%B8%AD%E6%98%BE%E7%A4%BA%E7%99%BE%E5%88%86%E6%AF%94%3A%E9%BB%98%E8%AE%A4%E6%96%B9%E5%BC%8F%0A%20%20%20%20%2F%2Fplot.setLabelGenerator(new%20%20%20%20%20%20%20%20%20%20%20StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT))%3B%0A%2F%2F%20%E5%9B%BE%E7%89%87%E4%B8%AD%E6%98%BE%E7%A4%BA%E7%99%BE%E5%88%86%E6%AF%94%3A%E8%87%AA%E5%AE%9A%E4%B9%89%E6%96%B9%E5%BC%8F%EF%BC%8C%7B0%7D%20%E8%A1%A8%E7%A4%BA%E9%80%89%E9%A1%B9%EF%BC%8C%20%7B1%7D%20%E8%A1%A8%E7%A4%BA%E6%95%B0%E5%80%BC%EF%BC%8C%20%7B2%7D%20%E8%A1%A8%E7%A4%BA%E6%89%80%E5%8D%A0%E6%AF%94%E4%BE%8B%20%2C%E5%B0%8F%E6%95%B0%E7%82%B9%E5%90%8E%E4%B8%A4%E4%BD%8D%0A%20plot.setLabelGenerator(new%20StandardPieSectionLabelGenerator(%22%7B0%7D%3D%7B1%7D(%7B2%7D)%22%2C%20NumberFormat.getNumberInstance()%2C%20new%20DecimalFormat(%220.00%25%22)))%3B%20%0A%2F%2F%20%E5%9B%BE%E4%BE%8B%E6%98%BE%E7%A4%BA%E7%99%BE%E5%88%86%E6%AF%94%3A%E8%87%AA%E5%AE%9A%E4%B9%89%E6%96%B9%E5%BC%8F%EF%BC%8C%20%7B0%7D%20%E8%A1%A8%E7%A4%BA%E9%80%89%E9%A1%B9%EF%BC%8C%20%7B1%7D%20%E8%A1%A8%E7%A4%BA%E6%95%B0%E5%80%BC%EF%BC%8C%20%7B2%7D%20%E8%A1%A8%E7%A4%BA%E6%89%80%E5%8D%A0%E6%AF%94%E4%BE%8B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20plot.setLegendLabelGenerator(new%20StandardPieSectionLabelGenerator(%22%7B0%7D%3D%7B1%7D(%7B2%7D)%22))%3B%20%0A%2F%2F%20%E8%AE%BE%E7%BD%AE%E8%83%8C%E6%99%AF%E8%89%B2%E4%B8%BA%E7%99%BD%E8%89%B2%20%0Achart.setBackgroundPaint(Color.white)%3B%20%0A%2F%2F%20%E6%8C%87%E5%AE%9A%E5%9B%BE%E7%89%87%E7%9A%84%E9%80%8F%E6%98%8E%E5%BA%A6(0.0-1.0)%20%0A%20plot.setForegroundAlpha(1.0f)%3B%20%0A%2F%2F%20%E6%8C%87%E5%AE%9A%E6%98%BE%E7%A4%BA%E7%9A%84%E9%A5%BC%E5%9B%BE%E4%B8%8A%E5%9C%86%E5%BD%A2(false)%E8%BF%98%E6%A4%AD%E5%9C%86%E5%BD%A2(true)%20%0Aplot.setCircular(true)%3B%20%0A%2F%2F%20%E8%AE%BE%E7%BD%AE%E5%9B%BE%E6%A0%87%E9%A2%98%E7%9A%84%E5%AD%97%E4%BD%93%20%0AFont%20font%20%3D%20new%20Font(%22%20%E9%BB%91%E4%BD%93%22%2CFont.CENTER_BASELINE%2C20)%3B%20%0ATextTitle%20title%20%3D%20new%20TextTitle(%22%20%E9%A1%B9%E7%9B%AE%E7%8A%B6%E6%80%81%E5%88%86%E5%B8%83%22)%3B%20%0Atitle.setFont(font)%3B%20%20%0Achart.setTitle(title)%3B%20%0AFileOutputStream%20fos_jpg%20%3D%20null%3B%20%0Atry%20%7B%20%0A%20%20%20%20%20fos_jpg%3Dnew%20FileOutputStream(%22D%3A%5C%5C%20%E9%A1%B9%E7%9B%AE%E7%8A%B6%E6%80%81%E5%88%86%E5%B8%83.jpg%22)%3B%20%0A%20%20%20%20%20ChartUtilities.writeChartAsJPEG(fos_jpg%2C100%2Cchart%2C640%2C480%2Cnull)%3B%20%0A%20%20%20%20%20fos_jpg.close()%3B%20%0A%7D%20catch%20(Exception%20e)%20%7B%20%0A%20%7D%20%0A%7D%20%0Aprivate%20static%20PieDataset%20getDataSet()%20%7B%20%0ADefaultPieDataset%20dataset%20%3D%20new%20DefaultPieDataset()%3B%20%0Adataset.setValue(%22%20%E5%B8%82%E5%9C%BA%E5%89%8D%E6%9C%9F%22%2C%20new%20Double(10))%3B%20%0Adataset.setValue(%22%20%E7%AB%8B%E9%A1%B9%22%2C%20new%20Double(15))%3B%20%0Adataset.setValue(%22%20%E8%AE%A1%E5%88%92%22%2C%20new%20Double(10))%3B%20%0Adataset.setValue(%22%20%E9%9C%80%E6%B1%82%E4%B8%8E%E8%AE%BE%E8%AE%A1%22%2C%20new%20Double(10))%3B%20%0Adataset.setValue(%22%20%E6%89%A7%E8%A1%8C%E6%8E%A7%E5%88%B6%22%2C%20new%20Double(35))%3B%20%0Adataset.setValue(%22%20%E6%94%B6%E5%B0%BE%22%2C%20new%20Double(10))%3B%20%0Adataset.setValue(%22%20%E8%BF%90%E7%BB%B4%22%2Cnew%20Double(10))%3B%20%0Areturn%20dataset%3B%20%20%20%20%20%20%20%20%0A%7D%0A%7D%20" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. public   class  PieChartPicture {  
  2. public   static   void  main(String[] args)   
  3. {  
  4.  PieDataset dataset = getDataSet();  
  5.  JFreeChart  chart = ChartFactory.createPieChart3D(  
  6.     " 项目进度分布" // chart title   
  7.     dataset,// data   
  8.     true , // include legend   
  9.     true ,  
  10.     false   
  11.    );  
  12.   PiePlot3D  plot=(PiePlot3D)chart.getPlot();  
  13.     // 图片中显示百分比:默认方式   
  14.     //plot.setLabelGenerator(new           StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));   
  15. // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位   
  16.  plot.setLabelGenerator(new  StandardPieSectionLabelGenerator( "{0}={1}({2})" , NumberFormat.getNumberInstance(),  new  DecimalFormat( "0.00%" )));   
  17. // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例                   
  18.  plot.setLegendLabelGenerator(new  StandardPieSectionLabelGenerator( "{0}={1}({2})" ));   
  19. // 设置背景色为白色    
  20. chart.setBackgroundPaint(Color.white);   
  21. // 指定图片的透明度(0.0-1.0)    
  22.  plot.setForegroundAlpha(1 .0f);   
  23. // 指定显示的饼图上圆形(false)还椭圆形(true)    
  24. plot.setCircular(true );   
  25. // 设置图标题的字体    
  26. Font font = new  Font( " 黑体" ,Font.CENTER_BASELINE, 20 );   
  27. TextTitle title = new  TextTitle( " 项目状态分布" );   
  28. title.setFont(font);    
  29. chart.setTitle(title);   
  30. FileOutputStream fos_jpg = null ;   
  31. try  {   
  32.      fos_jpg=new  FileOutputStream( "D:\\ 项目状态分布.jpg" );   
  33.      ChartUtilities.writeChartAsJPEG(fos_jpg,100 ,chart, 640 , 480 , null );   
  34.      fos_jpg.close();   
  35. catch  (Exception e) {   
  36.  }   
  37. }   
  38. private   static  PieDataset getDataSet() {   
  39. DefaultPieDataset dataset = new  DefaultPieDataset();   
  40. dataset.setValue(" 市场前期" new  Double( 10 ));   
  41. dataset.setValue(" 立项" new  Double( 15 ));   
  42. dataset.setValue(" 计划" new  Double( 10 ));   
  43. dataset.setValue(" 需求与设计" new  Double( 10 ));   
  44. dataset.setValue(" 执行控制" new  Double( 35 ));   
  45. dataset.setValue(" 收尾" new  Double( 10 ));   
  46. dataset.setValue(" 运维" , new  Double( 10 ));   
  47. return  dataset;          
  48. }  
  49. }   
public class PieChartPicture {
public static void main(String[] args) 
{
 PieDataset dataset = getDataSet();
 JFreeChart
 chart = ChartFactory.createPieChart3D(
    " 项目进度分布", // chart title
    dataset,// data
    true,// include legend
    true,
    false
   );
  PiePlot3D  plot=(PiePlot3D)chart.getPlot();
    // 图片中显示百分比:默认方式
    //plot.setLabelGenerator(new           StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));
// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
 plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%"))); 
// 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例                
 plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})")); 
// 设置背景色为白色 
chart.setBackgroundPaint(Color.white); 
// 指定图片的透明度(0.0-1.0) 
 plot.setForegroundAlpha(1.0f); 
// 指定显示的饼图上圆形(false)还椭圆形(true) 
plot.setCircular(true); 
// 设置图标题的字体 
Font font = new Font(" 黑体",Font.CENTER_BASELINE,20); 
TextTitle title = new TextTitle(" 项目状态分布"); 
title.setFont(font);  
chart.setTitle(title); 
FileOutputStream fos_jpg = null; 
try { 
     fos_jpg=new FileOutputStream("D:\\ 项目状态分布.jpg"); 
     ChartUtilities.writeChartAsJPEG(fos_jpg,100,chart,640,480,null); 
     fos_jpg.close(); 
} catch (Exception e) { 
 } 
} 
private static PieDataset getDataSet() { 
DefaultPieDataset dataset = new DefaultPieDataset(); 
dataset.setValue(" 市场前期", new Double(10)); 
dataset.setValue(" 立项", new Double(15)); 
dataset.setValue(" 计划", new Double(10)); 
dataset.setValue(" 需求与设计", new Double(10)); 
dataset.setValue(" 执行控制", new Double(35)); 
dataset.setValue(" 收尾", new Double(10)); 
dataset.setValue(" 运维",new Double(10)); 
return dataset;        
}
} 



2) 采用servlet和struts的action方式输出
采用这种方式输出,不用生成图片。
A .servlet输出

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ltc603.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=public%20class%20PieByServlet%20extends%20HttpServlet%7B%20%0Apublic%20void%20service(ServletRequest%20req%2C%20ServletResponse%20res)%20%0Athrows%20ServletException%2C%20IOException%20%0A%7B%20%0A%20%20%20res.setContentType(%22image%2Fjpeg%22)%3B%20%0A%20%20%20PieDataset%20dataset%20%3D%20getDataSet()%3B%20%0A%20%20%20JFreeChart%20chart%20%3D%20ChartFactory.createPieChart3D(%20%0A%20%20%20%22%20%E6%B0%B4%E6%9E%9C%E9%A5%BC%E5%9B%BE%22%2C%20%2F%2F%20chart%20title%20%0A%20%20%20dataset%2C%2F%2F%20data%20%0A%20%20%20true%2C%20%2F%2F%20include%20legend%20%0A%20%20%20true%2C%20%0A%20%20%20false%20)%3B%20%0A%20%20%20%2F%2F%E8%AE%BE%E7%BD%AE%E5%9B%BE%E8%A1%A8%E5%B1%9E%E6%80%A7%0A%2F%2F%20%E8%BE%93%E5%87%BA%E5%9B%BE%E7%89%87%20%0A%20ChartUtilities.writeChartAsJPEG(res.getOutputStream()%2C100%2Cchart%2C800%2C600%2Cnull)%3B%20%0A%7D%20" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. public   class  PieByServlet  extends  HttpServlet{   
  2. public   void  service(ServletRequest req, ServletResponse res)   
  3. throws  ServletException, IOException   
  4. {   
  5.    res.setContentType("image/jpeg" );   
  6.    PieDataset dataset = getDataSet();   
  7.    JFreeChart  chart = ChartFactory.createPieChart3D(   
  8.    " 水果饼图" // chart title    
  9.    dataset,// data    
  10.    true // include legend    
  11.    true ,   
  12.    false  );   
  13.    //设置图表属性   
  14. // 输出图片    
  15.  ChartUtilities.writeChartAsJPEG(res.getOutputStream(),100 ,chart, 800 , 600 , null );   
  16. }   
public class PieByServlet extends HttpServlet{ 
public void service(ServletRequest req, ServletResponse res) 
throws ServletException, IOException 
{ 
   res.setContentType("image/jpeg"); 
   PieDataset dataset = getDataSet(); 
   JFreeChart
 chart = ChartFactory.createPieChart3D( 
   " 水果饼图", // chart title 
   dataset,// data 
   true, // include legend 
   true, 
   false ); 
   //设置图表属性
// 输出图片 
 ChartUtilities.writeChartAsJPEG(res.getOutputStream(),100,chart,800,600,null); 
} 


B .struts的action方式输出
只将这条语句加上try catch即可,并返回null。

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ltc603.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=try%7B%20%20%20%20%20%20%20%0AChartUtilities.writeChartAsJPEG(response.getOutputStream()%2C100%2Cchart%2C800%2C600%2Cnull)%3B%20%0A%20%7D%20catch%20(Exception%20e)%20%7B%20%0A%7D%0Areturn%20null%3B%20" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. try {         
  2. ChartUtilities.writeChartAsJPEG(response.getOutputStream(),100 ,chart, 800 , 600 , null );   
  3.  } catch  (Exception e) {   
  4. }  
  5. return   null ;   
try{       
ChartUtilities.writeChartAsJPEG(response.getOutputStream(),100,chart,800,600,null); 
 } catch (Exception e) { 
}
return null; 



其实采用这两种方式与生成图片的方式改动并不大
加上语句response.setContentType("image/jpeg");
ChartUtilities.writeChartAsJPEG(new FileOutputStream("D:\\ 项目状态分布.jpg");,100,chart,640,480,null);
文件流改成response的输出流就可以了
hartUtilities.writeChartAsJPEG(response.getOutputStream(),100,chart,800,600,null);

3)jsp+servlet+javabean方式
1. Create ChartViewer servlet

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ltc603.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=public%20class%20ChartViewer%20extends%20HttpServlet%20%7B%20%0Apublic%20void%20init()%20throws%20ServletException%20%7B%0A%7D%20%0A%2F%2FProcess%20the%20HTTP%20Get%20request%0Apublic%20void%20doGet(HttpServletRequest%20request%2C%20HttpServletResponse%20response)%20%20throws%20ServletException%2C%20IOException%20%7B%20%0A%2F%2F%20get%20the%20chart%20from%20session%0AHttpSession%20session%20%3D%20request.getSession()%3B%20%0ABufferedImage%20chartImage%20%3D%20(BufferedImage)%20session.getAttribute(%22chartImage%22)%3B%20%0A%2F%2F%20set%20the%20content%20type%20so%20the%20browser%20can%20see%20this%20as%20a%20picture%20%0Aresponse.setContentType(%22image%2Fpng%22)%3B%20%0A%2F%2F%20send%20the%20picture%20%0APngEncoder%20encoder%20%3D%20new%20PngEncoder(chartImage%2C%20false%2C%200%2C%209)%3B%20%0Aresponse.getOutputStream().write(encoder.pngEncode())%3B%0A%7D%20%0A%2F%2FProcess%20the%20HTTP%20Post%20request%20%0Apublic%20void%20doPost(HttpServletRequest%20request%2C%20HttpServletResponse%20response)%20%20throws%20ServletException%2C%20IOException%20%7B%20%0AdoGet(request%2C%20response)%3B%20%0A%7D%20%0A%2F%2FProcess%20the%20HTTP%20Put%20request%20%0Apublic%20void%20doPut(HttpServletRequest%20request%2C%20HttpServletResponse%20response)%20throws%20ServletException%2C%20IOException%20%7B%20%0A%7D%20%0A%2F%2FClean%20up%20resources%20%0Apublic%20void%20destroy()%20%7B%20%7D%20%0A%7D%20" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. public   class  ChartViewer  extends  HttpServlet {   
  2. public   void  init()  throws  ServletException {  
  3. }   
  4. //Process the HTTP Get request   
  5. public   void  doGet(HttpServletRequest request, HttpServletResponse response)   throws  ServletException, IOException {   
  6. // get the chart from session   
  7. HttpSession session = request.getSession();   
  8. BufferedImage chartImage = (BufferedImage) session.getAttribute("chartImage" );   
  9. // set the content type so the browser can see this as a picture    
  10. response.setContentType("image/png" );   
  11. // send the picture    
  12. PngEncoder encoder = new  PngEncoder(chartImage,  false 0 9 );   
  13. response.getOutputStream().write(encoder.pngEncode());  
  14. }   
  15. //Process the HTTP Post request    
  16. public   void  doPost(HttpServletRequest request, HttpServletResponse response)   throws  ServletException, IOException {   
  17. doGet(request, response);   
  18. }   
  19. //Process the HTTP Put request    
  20. public   void  doPut(HttpServletRequest request, HttpServletResponse response)  throws  ServletException, IOException {   
  21. }   
  22. //Clean up resources    
  23. public   void  destroy() { }   
  24. }   
public class ChartViewer extends HttpServlet { 
public void init() throws ServletException {
} 
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException { 
// get the chart from session
HttpSession session = request.getSession(); 
BufferedImage chartImage = (BufferedImage) session.getAttribute("chartImage"); 
// set the content type so the browser can see this as a picture 
response.setContentType("image/png"); 
// send the picture 
PngEncoder encoder = new PngEncoder(chartImage, false, 0, 9); 
response.getOutputStream().write(encoder.pngEncode());
} 
//Process the HTTP Post request 
public void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException { 
doGet(request, response); 
} 
//Process the HTTP Put request 
public void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
} 
//Clean up resources 
public void destroy() { } 
} 



2. Create a servlet map in web.xml

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ltc603.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%20%0A%3Cweb-app%3E%20%0A%20%3Cservlet%3E%20%0A%20%3Cservlet-name%3EChartViewer%3C%2Fservlet-name%3E%20%0A%20%3Cservlet-class%3Emyapp.webwork.servlets.ChartViewer%3C%2Fservlet-class%3E%20%0A%3C%2Fservlet%3E%20%0A%3Cservlet-mapping%3E%20%0A%3Cservlet-name%3EChartViewer%3C%2Fservlet-name%3E%20%0A%3Curl-pattern%3E%2Fservlet%2FChartViewer%3C%2Furl-pattern%3E%20%0A%3C%2Fservlet-mapping%3E%20%0A%3C%2Fweb-app%3E%20" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <?xml version= "1.0"  encoding= "UTF-8" ?>   
  2. <web-app>   
  3.  <servlet>   
  4.  <servlet-name>ChartViewer</servlet-name>   
  5.  <servlet-class >myapp.webwork.servlets.ChartViewer</servlet- class >   
  6. </servlet>   
  7. <servlet-mapping>   
  8. <servlet-name>ChartViewer</servlet-name>   
  9. <url-pattern>/servlet/ChartViewer</url-pattern>   
  10. </servlet-mapping>   
  11. </web-app>   
<?xml version="1.0" encoding="UTF-8"?> 
<web-app> 
 <servlet> 
 <servlet-name>ChartViewer</servlet-name> 
 <servlet-class>myapp.webwork.servlets.ChartViewer</servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>ChartViewer</servlet-name> 
<url-pattern>/servlet/ChartViewer</url-pattern> 
</servlet-mapping> 
</web-app> 



3. Create a chart in a java bean (Pie3DDemo.java)

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ltc603.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=public%20class%20Pie3DDemo%20%7B%20%0Aprivate%20DefaultPieDataset%20getDataset()%20%7B%20%0A%2F%2F%20categories...%20%0AString%5B%5D%20section%20%3D%20new%20String%5B%5D%20%7B%20%22Jan%22%2C%22Feb%22%2C%22Mar%22%2C%22Apr%22%2C%22May%22%2C%22Jun%22%2C%20%22Jul%22%2C%22Aug%22%2C%22Sep%22%2C%22Oct%22%2C%22Nov%22%2C%22Dec%22%20%7D%3B%20%0A%2F%2F%20data...%20%0Adouble%5B%5D%20data%20%3D%20new%20double%5Bsection.length%5D%3B%20%0A%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20data.length%3B%20i%2B%2B)%20%7B%20%0A%20%20%20%20%20data%5Bi%5D%20%3D%2010%20%2B%20(Math.random()%20*%2010)%3B%20%0A%7D%20%0A%2F%2F%20create%20the%20dataset...%20%0ADefaultPieDataset%20dataset%20%3D%20new%20DefaultPieDataset()%3B%20%0Afor%20(int%20i%20%3D%200%3B%20i%20%3C%20data.length%3B%20i%2B%2B)%20%7B%20%0A%20%20%20%20%20%20%20dataset.setValue(section%5Bi%5D%2C%20data%5Bi%5D)%3B%20%0A%7D%20%0Areturn%20dataset%3B%20%0A%7D%20%0Apublic%20String%20getChartViewer(HttpServletRequest%20request%2C%20HttpServletResponse%20response)%20%7B%20%0A%20DefaultPieDataset%20dataset%20%3D%20getDataset()%3B%20%0A%2F%2F%20create%20the%20chart...%20%0A%20JFreeChart%20chart%20%3D%20ChartFactory.createPie3DChart(%20%0A%20%20%22Pie3D%20Chart%20Demo%22%2C%20%20%2F%2F%20chart%20title%20%0A%20%20dataset%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20data%20%0A%20%20true%2C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20include%20legend%20%0A%20%20true%2C%20%0A%20%20false%20%0A%20)%3B%20%0A%2F%2F%20set%20the%20background%20color%20for%20the%20chart...%20%0Achart.setBackgroundPaint(Color.cyan)%3B%20%0APiePlot%20plot%20%3D%20(PiePlot)%20chart.getPlot()%3B%20%0Aplot.setNoDataMessage(%22No%20data%20available%22)%3B%20%0A%2F%2F%20set%20drilldown%20capability...%20%0A%20plot.setURLGenerator(new%20StandardPieURLGenerator(%22Bar3DDemo.jsp%22%2C%22section%22))%3B%20%0Aplot.setLabelGenerator(null)%3B%20%0A%2F%2F%20OPTIONAL%20CUSTOMISATION%20COMPLETED.%20%0AChartRenderingInfo%20info%20%3D%20null%3B%20%0AHttpSession%20session%20%3D%20request.getSession()%3B%20%0Atry%20%7B%20%0A%2F%2FCreate%20RenderingInfo%20object%20%0A%20response.setContentType(%22text%2Fhtml%22)%3B%20%0A%20info%20%3D%20new%20ChartRenderingInfo(new%20StandardEntityCollection())%3B%20%0A%20BufferedImage%20chartImage%20%3D%20chart.createBufferedImage(640%2C%20400%2C%20info)%3B%20%0A%2F%2F%20putting%20chart%20as%20BufferedImage%20in%20session%2C%20%0A%2F%2F%20thus%20making%20it%20available%20for%20the%20image%20reading%20action%20Action.%20%0Asession.setAttribute(%22chartImage%22%2C%20chartImage)%3B%20%0APrintWriter%20writer%20%3D%20new%20PrintWriter(response.getWriter())%3B%20%0AChartUtilities.writeImageMap(writer%2C%20%22imageMap%22%2C%20info)%3B%20%0Awriter.flush()%3B%20%0A%7D%20catch%20(Exception%20e)%20%7B%20%7D%20%0A%0AString%20pathInfo%20%3D%20%22http%3A%2F%2F%22%3B%20%0ApathInfo%20%2B%3D%20request.getServerName()%3B%20%0Aint%20port%20%3D%20request.getServerPort()%3B%20%0ApathInfo%20%2B%3D%20%22%3A%22%2BString.valueOf(port)%3B%20%0ApathInfo%20%2B%3D%20request.getContextPath()%3B%20%0AString%20chartViewer%20%3D%20pathInfo%20%2B%20%22%2Fservlet%2FChartViewer%22%3B%20%0Areturn%20chartViewer%3B%20%0A%7D%20" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. public   class  Pie3DDemo {   
  2. private  DefaultPieDataset getDataset() {   
  3. // categories...    
  4. String[] section = new  String[] {  "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec"  };   
  5. // data...    
  6. double [] data =  new   double [section.length];   
  7.  for  ( int  i =  0 ; i < data.length; i++) {   
  8.      data[i] = 10  + (Math.random() *  10 );   
  9. }   
  10. // create the dataset...    
  11. DefaultPieDataset dataset = new  DefaultPieDataset();   
  12. for  ( int  i =  0 ; i < data.length; i++) {   
  13.        dataset.setValue(section[i], data[i]);   
  14. }   
  15. return  dataset;   
  16. }   
  17. public  String getChartViewer(HttpServletRequest request, HttpServletResponse response) {   
  18.  DefaultPieDataset dataset = getDataset();   
  19. // create the chart...    
  20.  JFreeChart  chart = ChartFactory.createPie3DChart(   
  21.   "Pie3D Chart Demo" ,   // chart title    
  22.   dataset,             // data    
  23.   true ,                 // include legend    
  24.   true ,   
  25.   false    
  26.  );   
  27. // set the background color for the chart...    
  28. chart.setBackgroundPaint(Color.cyan);   
  29. PiePlot plot = (PiePlot) chart.getPlot();   
  30. plot.setNoDataMessage("No data available" );   
  31. // set drilldown capability...    
  32.  plot.setURLGenerator(new  StandardPieURLGenerator( "Bar3DDemo.jsp" , "section" ));   
  33. plot.setLabelGenerator(null );   
  34. // OPTIONAL CUSTOMISATION COMPLETED.    
  35. ChartRenderingInfo info = null ;   
  36. HttpSession session = request.getSession();   
  37. try  {   
  38. //Create RenderingInfo object    
  39.  response.setContentType("text/html" );   
  40.  info = new  ChartRenderingInfo( new  StandardEntityCollection());   
  41.  BufferedImage chartImage = chart.createBufferedImage(640 400 , info);   
  42. // putting chart as BufferedImage in session,    
  43. // thus making it available for the image reading action Action.    
  44. session.setAttribute("chartImage" , chartImage);   
  45. PrintWriter writer = new  PrintWriter(response.getWriter());   
  46. ChartUtilities.writeImageMap(writer, "imageMap" , info);   
  47. writer.flush();   
  48. catch  (Exception e) { }   
  49.   
  50. String pathInfo = "http://" ;   
  51. pathInfo += request.getServerName();   
  52. int  port = request.getServerPort();   
  53. pathInfo += ":" +String.valueOf(port);   
  54. pathInfo += request.getContextPath();   
  55. String chartViewer = pathInfo + "/servlet/ChartViewer" ;   
  56. return  chartViewer;   
  57. }   
public class Pie3DDemo { 
private DefaultPieDataset getDataset() { 
// categories... 
String[] section = new String[] { "Jan","Feb","Mar","Apr","May","Jun", "Jul","Aug","Sep","Oct","Nov","Dec" }; 
// data... 
double[] data = new double[section.length]; 
 for (int i = 0; i < data.length; i++) { 
     data[i] = 10 + (Math.random() * 10); 
} 
// create the dataset... 
DefaultPieDataset dataset = new DefaultPieDataset(); 
for (int i = 0; i < data.length; i++) { 
       dataset.setValue(section[i], data[i]); 
} 
return dataset; 
} 
public String getChartViewer(HttpServletRequest request, HttpServletResponse response) { 
 DefaultPieDataset dataset = getDataset(); 
// create the chart... 
 JFreeChart
 chart = ChartFactory.createPie3DChart( 
  "Pie3D Chart Demo",  // chart title 
  dataset,             // data 
  true,                // include legend 
  true, 
  false 
 ); 
// set the background color for the chart... 
chart.setBackgroundPaint(Color.cyan); 
PiePlot plot = (PiePlot) chart.getPlot(); 
plot.setNoDataMessage("No data available"); 
// set drilldown capability... 
 plot.setURLGenerator(new StandardPieURLGenerator("Bar3DDemo.jsp","section")); 
plot.setLabelGenerator(null); 
// OPTIONAL CUSTOMISATION COMPLETED. 
ChartRenderingInfo info = null; 
HttpSession session = request.getSession(); 
try { 
//Create RenderingInfo object 
 response.setContentType("text/html"); 
 info = new ChartRenderingInfo(new StandardEntityCollection()); 
 BufferedImage chartImage = chart.createBufferedImage(640, 400, info); 
// putting chart as BufferedImage in session, 
// thus making it available for the image reading action Action. 
session.setAttribute("chartImage", chartImage); 
PrintWriter writer = new PrintWriter(response.getWriter()); 
ChartUtilities.writeImageMap(writer, "imageMap", info); 
writer.flush(); 
} catch (Exception e) { } 

String pathInfo = "http://"; 
pathInfo += request.getServerName(); 
int port = request.getServerPort(); 
pathInfo += ":"+String.valueOf(port); 
pathInfo += request.getContextPath(); 
String chartViewer = pathInfo + "/servlet/ChartViewer"; 
return chartViewer; 
} 



6. 页面

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ltc603.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%20%0A%3Chtml%3E%20%0A%3Chead%3E%20%0A%3Ctitle%3EPie%20Chart%20Demo%3C%2Ftitle%3E%20%0A%3C%2Fhead%3E%20%0A%3Cjsp%3AuseBeanid%3D%22myChart%22scope%3D%22session%22class%3D%22myapp.webwork.beans.Pie3DDemo%22%20%2F%3E%20%0A%3Cbody%3E%20%0A%3Ch2%3EPie%20Chart%20Demo%3C%2Fh2%3E%20%0A%3C%25String%20chartViewer%20%3D%20myChart.getChartViewer(request%2C%20response)%3B%25%3E%20%0A%3Cimg%20src%3D%22%3C%25%3DchartViewer%25%3E%22%20border%3D0%20usemap%3D%22%23imageMap%22%3E%20%0A%3C%2Fbody%3E%20%0A%3C%2Fhtml%3E%20" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1.    
  2. <html>   
  3. <head>   
  4. <title>Pie Chart Demo</title>   
  5. </head>   
  6. <jsp:useBeanid="myChart" scope= "session" class = "myapp.webwork.beans.Pie3DDemo"  />   
  7. <body>   
  8. <h2>Pie Chart Demo</h2>   
  9. <%String chartViewer = myChart.getChartViewer(request, response);%>   
  10. <img src="<%=chartViewer%>"  border= 0  usemap= "#imageMap" >   
  11. </body>   
  12. </html>   
 
<html> 
<head> 
<title>Pie Chart Demo</title> 
</head> 
<jsp:useBeanid="myChart"scope="session"class="myapp.webwork.beans.Pie3DDemo" /> 
<body> 
<h2>Pie Chart Demo</h2> 
<%String chartViewer = myChart.getChartViewer(request, response);%> 
<img src="<%=chartViewer%>" border=0 usemap="#imageMap"> 
</body> 
</html> 



4)采用工具类ChartUtil和DisplayChart(jfreechart 的servlet)输出
我用了上面的几个方法输出图片,发现页面里只能输出一个图片,
不过下面的方法可以输出多个图片,而且是几种方式中最简单的一个,推荐使用。

这种方式和上面的三种比较类似,是将javabean里的生成图片的方法写的一个工具类ChartUtil里面。

1 .添加工具类ChartUtil
public class ChartUtil {
//  产生时间序列图,返回图片名称

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ltc603.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=public%20%20static%20String%20generatePieChart(DefaultPieDataset%20dataset%2CString%20title%2Cint%20width%2Cint%20height%2CHttpSession%20session%2C%20PrintWriter%20pw)%20%7B%20%0A%0AString%20filename%20%3D%20null%3B%20%0A%20try%20%7B%20%0A%20if%20(session%20!%3D%20null)%20%0A%20%7B%20%0AChartDeleter%20deleter%20%3D%20(ChartDeleter)session.getAttribute(%22JFreeChart_Deleter%22)%3B%20%0A%20session.removeAttribute(%22JFreeChart_Deleter%22)%3B%20%0A%20session.setAttribute(%22JFreeChart_Deleter%22%2C%20deleter)%3B%20%0A%7D%20%0AJFreeChart%20chart%20%3D%20ChartFactory.createPieChart3D(%20%0Atitle%2C%20%20%2F%2F%20chart%20title%20%0Adataset%2C%20%2F%2F%20data%20%0Atrue%2C%20%20%2F%2F%20include%20legend%20%0Atrue%2C%20%0Afalse%20)%3B%20%0A%2F%2F%20%20Write%20the%20chart%20image%20to%20the%20temporary%20directory%20%0AChartRenderingInfo%20info%20%3D%20new%20ChartRenderingInfo(new%20StandardEntityCollection())%3B%20%0A%2F%2FIf%20the%20last%20parameter%20is%20null%2C%20the%20chart%20is%20a%20%22one%20time%22-chart%20and%20will%20be%20deleted%20after%20the%20first%20serving.%20%0A%2F%2FIf%20the%20last%20parameter%20is%20a%20session%20object%2C%20the%20chart%20remains%20until%20session%20time%20out.%20%0Afilename%20%3D%20ServletUtilities.saveChartAsPNG(chart%2C%20width%2C%20height%2C%20info%2C%20session)%3B%20%0A%2F%2F%20%20Write%20the%20image%20map%20to%20the%20PrintWriter%20%0A%20ChartUtilities.writeImageMap(pw%2C%20filename%2C%20info%2Ctrue)%3B%20%0A%20pw.flush()%3B%20%0A%7D%20catch%20(Exception%20e)%20%7B%20%0A%20System.out.println(%22Exception%20-%20%22%20%2B%20e.toString())%3B%20%0A%20e.printStackTrace(System.out)%3B%20%0A%20filename%20%3D%20%22picture_error.png%22%3B%20%7D%20%0Areturn%20filename%3B%20%0A%7D%20%0A%7D%20" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. public    static  String generatePieChart(DefaultPieDataset dataset,String title, int  width, int  height,HttpSession session, PrintWriter pw) {   
  2.   
  3. String filename = null ;   
  4.  try  {   
  5.  if  (session !=  null )   
  6.  {   
  7. ChartDeleter deleter = (ChartDeleter)session.getAttribute("JFreeChart_Deleter" );   
  8.  session.removeAttribute("JFreeChart_Deleter" );   
  9.  session.setAttribute("JFreeChart_Deleter" , deleter);   
  10. }   
  11. JFreeChart  chart = ChartFactory.createPieChart3D(   
  12. title,  // chart title    
  13. dataset, // data    
  14. true ,   // include legend    
  15. true ,   
  16. false  );   
  17. //  Write the chart image to the temporary directory    
  18. ChartRenderingInfo info = new  ChartRenderingInfo( new  StandardEntityCollection());   
  19. //If the last parameter is null, the chart is a "one time"-chart and will be deleted after the first serving.    
  20. //If the last parameter is a session object, the chart remains until session time out.    
  21. filename = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);   
  22. //  Write the image map to the PrintWriter    
  23.  ChartUtilities.writeImageMap(pw, filename, info,true );   
  24.  pw.flush();   
  25. catch  (Exception e) {   
  26.  System.out.println("Exception - "  + e.toString());   
  27.  e.printStackTrace(System.out);   
  28.  filename = "picture_error.png" ; }   
  29. return  filename;   
  30. }   
  31. }   
public  static String generatePieChart(DefaultPieDataset dataset,String title,int width,int height,HttpSession session, PrintWriter pw) { 

String filename = null; 
 try { 
 if (session != null) 
 { 
ChartDeleter deleter = (ChartDeleter)session.getAttribute("JFreeChart_Deleter"); 
 session.removeAttribute("JFreeChart_Deleter"); 
 session.setAttribute("JFreeChart_Deleter", deleter); 
} 
JFreeChart
 chart = ChartFactory.createPieChart3D( 
title,  // chart title 
dataset, // data 
true,  // include legend 
true, 
false ); 
//  Write the chart image to the temporary directory 
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); 
//If the last parameter is null, the chart is a "one time"-chart and will be deleted after the first serving. 
//If the last parameter is a session object, the chart remains until session time out. 
filename = ServletUtilities.saveChartAsPNG(chart, width, height, info, session); 
//  Write the image map to the PrintWriter 
 ChartUtilities.writeImageMap(pw, filename, info,true); 
 pw.flush(); 
} catch (Exception e) { 
 System.out.println("Exception - " + e.toString()); 
 e.printStackTrace(System.out); 
 filename = "picture_error.png"; } 
return filename; 
} 
} 




2、在action里统计数据,设置好数据集dataset。传到页面

3、 在页面里取出
DefaultPieDataset piedataset=(DefaultPieDataset)request.getAttribute("piedataset");
// 用ChartUtil工具类产生图片

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ltc603.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=String%20p%20%3D%20ChartUtil.generatePieChart(piedataset%2C%22%20%E9%A1%B9%E7%9B%AE%E6%94%B6%E6%94%AF%E7%BA%BF%E5%9B%BE%22%2C500%2C300%2Cnull%2C%20new%20PrintWriter(out))%3B%20%0AString%20p1%20%3D%20request.getContextPath()%20%2B%20%22%2Fservlet%2FDisplayChart%3Ffilename%3D%22%20%2B%20p%3B%20" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. String p = ChartUtil.generatePieChart(piedataset, " 项目收支线图" , 500 , 300 , null new  PrintWriter(out));   
  2. String p1 = request.getContextPath() + "/servlet/DisplayChart?filename="  + p;   
String p = ChartUtil.generatePieChart(piedataset," 项目收支线图",500,300,null, new PrintWriter(out)); 
String p1 = request.getContextPath() + "/servlet/DisplayChart?filename=" + p; 


通过以下方式输出

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ltc603.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3Ctd%3E%3Cimg%20src%3D%22%3C%25%3D%20p1%20%25%3E%22%20width%3D500%20height%3D300%20border%3D0%20usemap%3D%22%23%3C%25%3D%20p%20%25%3E%22%3E%3C%2Ftd%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <td><img src= "<%= p1 %>"  width= 500  height= 300  border= 0  usemap= "#<%= p %>" ></td>  
<td><img src="<%= p1 %>" width=500 height=300 border=0 usemap="#<%= p %>"></td>



4、在web.xml中添加

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ltc603.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3Cservlet%3E%0A%20%20%3Cservlet-name%3EDisplayChart%3C%2Fservlet-name%3E%0A%20%20%3Cservlet-class%3Eorg.jfree.chart.servlet.DisplayChart%3C%2Fservlet-class%3E%0A%3C%2Fservlet%3E%0A%3Cservlet-mapping%3E%0A%20%20%20%20%3Cservlet-name%3EDisplayChart%3C%2Fservlet-name%3E%0A%20%20%20%20%3Curl-pattern%3E%2Fservlet%2FDisplayChart%3C%2Furl-pattern%3E%0A%3C%2Fservlet-mapping%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <servlet>  
  2.   <servlet-name>DisplayChart</servlet-name>  
  3.   <servlet-class >org.jfree.chart.servlet.DisplayChart</servlet- class >  
  4. </servlet>  
  5. <servlet-mapping>  
  6.     <servlet-name>DisplayChart</servlet-name>  
  7.     <url-pattern>/servlet/DisplayChart</url-pattern>  
  8. </servlet-mapping>  
<servlet>
  <servlet-name>DisplayChart</servlet-name>
  <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>DisplayChart</servlet-name>
    <url-pattern>/servlet/DisplayChart</url-pattern>
</servlet-mapping>



5) ApplicationFrame 方式

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ltc603.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=public%20class%20PieChartDemo1%20extends%20ApplicationFrame%20%7B%20%0Apublic%20PieChartDemo1(String%20title)%20%7B%20%0Asuper(title)%3B%20%0AsetContentPane(createDemoPanel())%3B%20%0A%7D%20%0Aprivate%20static%20JFreeChart%20createChart(PieDataset%20dataset)%20%7B%20%0AJFreeChart%20chart%20%3D%20ChartFactory.createPieChart(%20%0A.......%0Areturn%20chart%3B%20%0A%7D%20%0A%0Apublic%20static%20JPanel%20createDemoPanel()%20%7B%20%0AJFreeChart%20chart%20%3D%20createChart(createDataset())%3B%20%0Areturn%20new%20ChartPanel(chart)%3B%20%0A%7D%20%0A%0Apublic%20static%20void%20main(String%5B%5D%20args)%20%7B%20%0APieChartDemo1%20demo%20%3D%20new%20PieChartDemo1(%22Pie%20Chart%20Demo%201%22)%3B%20%0Ademo.pack()%3B%20%0ARefineryUtilities.centerFram
分享到:
评论

相关推荐

    jfreechart-1.5.2.jar,jfreechart|jfreechart

    jfreechart-1.5.2.jar,jfreechart|jfreechart

    Jfreechart

    Jfreechart 乱码分析和解决方法

    jfreechart

    jfreechart Java图形报表开源代码

    JFreeChart双Y轴折线图实例,可以直接运行

    JFreeChart双Y轴折线图实例,可以直接运行,实例类为LineChartDemo1.JAVA,有注释。 若想在web工程使用只需如下。 String filename = ServletUtilities.saveChartAsPNG(jfreechart, 600, 400, null, session); ...

    jfreechart1.0.1 资源插件

    jfreechart-1.0.1.jar junit.jar servlet.jar JFreeChart目前是最好的java图形解决方案,基本能够解决目前的图形方面的需求,主要包括如下几个方面: 饼图, 柱状图 ,线图,点图,时间变化图,甘特图, 股票行情图,混和...

    JFreeChart 折线、柱状组合图

    public JFreeChart createChart(String Ytitle, String title, CategoryDataset lineData, CategoryDataset barData) { //参考附件 return chart; } //struts 部分 &lt;!--begin 维优特例 --&gt; ...

    JFreeChart的JAR包和API文档(包含例子)

    JFreeChart的JAR包和API文档JFreeChart的JAR包和API文档JFreeChart的JAR包和API文档JFreeChart的JAR包和API文档JFreeChart的JAR包和API文档JFreeChart的JAR包和API文档JFreeChart的JAR包和API文档JFreeChart的JAR包...

    JFreeChart使用的jar包

    JFreeChart的使用,需要导入的2个jar文件: jcommon-1.0.23.jar和jfreechart-1.0.19.jar。 可以去官网下载:http://sourceforge.net/projects/jfreechart/files/ 上传以作后续使用时备份.

    jfreechart所需jar包.zip

    jfreechart折线图所需jar包

    JFreeChart使用教程(中文).pdf

    JFreeChart是一组功能强大、灵活易用的Java绘图API,使用它可以生成多种通用性的报表,包括柱状图、饼图、曲线图、甘特图等。它能够 用在Swing和Web等中制作自定义的图表或报表,并且得到广泛的应用。本文将通过引领...

    jfreechart-1.0.9+api

    jfreechart

    jfreechart的一个小总结

    关于jfreechart总结的一个小文档,是转载的,所有各位亲,如果愿意可以看看

    JFreeChart 指南 & JFreeChart 示例

    JFreeChart 指南 JFreeChart Guide JFreeChart 示例 JFreeChart demo

    jfreechart开发jar包

    jfreechart开发jar包,真正要引用到eclipse build path的是jfreechart-1.0.14\lib目录下的gnujaxp.jar、jcommon-1.017.jar和jfreechart-1.0.14.jar三个包,我用的就是这3个jar包。。

    jfreechart 饼形图

    jfreechart百分比在饼上, 有分离的饼。 private static JFreeChart createChart(PieDataset piedataset) { JFreeChart jfreechart = ChartFactory.createPieChart("Pie Chart Demo 2", piedataset, true, true, ...

    jfreechart 折线图 应用

    package: jfreechart-1.0.9.jar jcommon-1.0.12.jar 本人也上传了,可以在本人上传资源中找到 环境: myeclipse 6.5 jdk 1.6 该程序为main 函数,定义了三个方法,分别是jfreechart三种实现折线图的类型,返回的是...

    jfreechart 绘制横向立体柱状图

    利用jfreechart绘制的横向立体柱状图,横坐标是数值,纵坐标是数据分类,需要提前引入jfreechart包文件到工程里,才不会报错

Global site tag (gtag.js) - Google Analytics