JAVA历程
希望我的这个博客能给那些想在java的海洋中拼搏的人有所帮助。谢谢光临!
posts - 8,  comments - 6,  trackbacks - 0

采用工具类ChartUtil和DisplayChart(jfreechart的servlet)输出

将javabean里的生成图片的方法写的一个工具类ChartUtil里面。

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

代码
  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. }      

 

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

3、 在页面里取出

代码
  1. DefaultPieDataset piedataset=(DefaultPieDataset)request.getAttribute("piedataset");    
  2. String p = ChartUtil.generatePieChart(piedataset," 项目收支线图",500,300,null, new PrintWriter(out));       
  3. String p1 = request.getContextPath() + "/servlet/DisplayChart?filename=" + p;   
  4.   
  5. <td><img src="<%= p1 %>" width=500 height=300 border=0 usemap="#<%= p %>"></td>     

 

4、在web.xml中添加

代码
  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>  
posted on 2009-10-14 14:16 I LOVE JAVA 阅读(598) 评论(0)  编辑  收藏 所属分类: java基础知识java高端技术

只有注册用户登录后才能发表评论。


网站导航:
 

<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(1)

随笔分类

随笔档案

文章分类

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜