设为首页   |  加入收藏夹 快速导航:  热门文章  |  最新文章  |  梦想博客  
当前位置:编程之家 -> 文章频道 ->java 
站内搜索:  

JfreeChart学习总结(2)

作者:java虫 来源:blog 整理日期:2007-08-07


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

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

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

B .struts的action方式输出
只将这条语句加上try catch即可,并返回null。
代码
  1. try{          
  2. ChartUtilities.writeChartAsJPEG(response.getOutputStream(),100,chart,800,600,null);    
  3.  } catch (Exception e) {    
  4. }   
  5. 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

代码
  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 ServletE
    xception, 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, false09);    
  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. }   

2. Create a servlet map in web.xml

[1]  [2]  [3]  [4]