现在的位置: 首页 > 综合 > 正文

JFreechart生成带热点的图

2013年08月23日 ⁄ 综合 ⁄ 共 5838字 ⁄ 字号 评论关闭

  遇到问题:

      windows下图片中文显示乱码--这个主要是由于没有给中文设置字体造成的

      某些linux下图片中文显示乱码--这个主要是由于该linux系统缺乏中文字体,向linux的字体目录下导入中文字体即可(具体方法网上很多)

      这两种问题本人已测试通过。

 

      本例使用struts2框架,导入jfreechart-1.0.13.jar和struts2-jfreechart-plugin-2.2.1.1.jar

 

1.带热点饼图的Action

com.milton.hot.FirstChart

 

Java代码  收藏代码
  1. package com.milton.hot;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5.   
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8.   
  9. import org.apache.struts2.ServletActionContext;  
  10. import org.jfree.chart.ChartFactory;  
  11. import org.jfree.chart.ChartRenderingInfo;  
  12. import org.jfree.chart.ChartUtilities;  
  13. import org.jfree.chart.JFreeChart;  
  14. import org.jfree.chart.entity.StandardEntityCollection;  
  15. import org.jfree.chart.labels.StandardPieToolTipGenerator;  
  16. import org.jfree.chart.plot.PiePlot;  
  17. import org.jfree.chart.servlet.ServletUtilities;  
  18. import org.jfree.chart.title.LegendTitle;  
  19. import org.jfree.chart.title.TextTitle;  
  20. import org.jfree.chart.urls.StandardPieURLGenerator;  
  21. import org.jfree.data.general.DefaultPieDataset;  
  22.   
  23. import com.opensymphony.xwork2.ActionSupport;  
  24. /** 
  25.  *  
  26.  * 带热点的饼图 
  27.  * @author Milton 
  28.  * 
  29.  */  
  30. public class FirstChart extends ActionSupport  
  31. {  
  32.     private String mapMessage;  
  33.     private String src;  
  34.     /** 
  35.      * 生成饼图 
  36.      */  
  37.     public String execute() throws Exception  
  38.     {  
  39.         //设置数据  
  40.         DefaultPieDataset data = new DefaultPieDataset();  
  41.         data.setValue("三星I9000"10000);  
  42.         data.setValue("HTC G7"20000);  
  43.         data.setValue("HTC G2"15000);  
  44.         data.setValue("诺基亚5230"16000);  
  45.         data.setValue("摩托罗拉ME525"50000);  
  46.         //生成JFreeChart对象  
  47.         JFreeChart chart = ChartFactory  
  48.                 .createPieChart3D("手机销量统计图", data, truetruetrue);  
  49.   
  50.         chart.setTitle(new TextTitle("手机销量统计图"new Font("隶书", Font.BOLD, 25)));  
  51.         //建一个图例  
  52.         LegendTitle legendTitle = chart.getLegend(0);  
  53.         //设置图例字体  
  54.         legendTitle.setItemFont(new Font("宋体",Font.BOLD,14));  
  55.           
  56.         PiePlot plot = (PiePlot) chart.getPlot();  
  57.           
  58.         //根据key指定各个数据饼图的颜色  
  59.         plot.setSectionPaint("三星I9000", Color.RED);  
  60.         plot.setSectionPaint("HTC G7", Color.BLUE);  
  61.         plot.setSectionPaint("HTC G2", Color.GREEN);  
  62.         plot.setSectionPaint("诺基亚5230", Color.ORANGE);  
  63.         plot.setSectionPaint("摩托罗拉ME525", Color.GRAY);  
  64.         //设置plot字体  
  65.         plot.setLabelFont(new Font("宋体",Font.BOLD,18));  
  66.         //设置背景透明度(0~1)  
  67.         plot.setBackgroundAlpha(0.1f);  
  68.           
  69.         //设置热点  
  70.         plot.setNoDataMessage("No data available");  
  71.         plot.setURLGenerator(new StandardPieURLGenerator("second.jsp""type"));  
  72.         plot.setToolTipGenerator(new StandardPieToolTipGenerator());   
  73.           
  74.         StandardEntityCollection sec = new StandardEntityCollection();  
  75.   
  76.         //生成RenderingInfo实例,info参数就是图片的热点信息  
  77.         ChartRenderingInfo info = new ChartRenderingInfo(sec);   
  78.         HttpServletRequest request = ServletActionContext.getRequest();  
  79.         String filename = ServletUtilities.saveChartAsJPEG(chart, 750300, info, request.getSession());  
  80.         mapMessage = ChartUtilities.getImageMap("map0", info);  
  81.         src = request.getContextPath()+"/DisplayChart?filename=" + filename;  
  82.         return "first";  
  83.     }  
  84.     public void setMapMessage(String mapMessage)  
  85.     {  
  86.         this.mapMessage = mapMessage;  
  87.     }  
  88.     public void setSrc(String src)  
  89.     {  
  90.         this.src = src;  
  91.     }  
  92.     public String getMapMessage()  
  93.     {  
  94.         return mapMessage;  
  95.     }  
  96.     public String getSrc()  
  97.     {  
  98.         return src;  
  99.     }  
  100.       
  101. }  

 2.热点链接进去后的柱状图的Action

com.milton.hot.SecondChart

 

Java代码  收藏代码
  1. package com.milton.hot;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5.   
  6. import javax.servlet.http.HttpServletRequest;  
  7.   
  8. import org.apache.struts2.ServletActionContext;  
  9. import org.jfree.chart.ChartFactory;  
  10. import org.jfree.chart.JFreeChart;  
  11. import org.jfree.chart.axis.CategoryAxis;  
  12. import org.jfree.chart.axis.CategoryLabelPositions;  
  13. import org.jfree.chart.axis.NumberAxis;  
  14. import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;  
  15. import org.jfree.chart.plot.CategoryPlot;  
  16. import org.jfree.chart.plot.PlotOrientation;  
  17. import org.jfree.chart.renderer.category.BarRenderer3D;  
  18. import org.jfree.chart.title.TextTitle;  
  19. import org.jfree.data.category.DefaultCategoryDataset;  
  20.   
  21. import com.opensymphony.xwork2.ActionSupport;  
  22.   
  23. public class SecondChart extends ActionSupport  
  24. {  
  25.     private JFreeChart chart;  
  26.       
  27.     /** 
  28.      * 生成柱状图 
  29.      */  
  30.     public String execute() throws Exception  
  31.     {  
  32.         HttpServletRequest request = ServletActionContext.getRequest();  
  33.         String type = new String(request.getParameter("type").getBytes("ISO_8859_1"), "UTF-8");  
  34.           
  35.         if(type.equals("Java"))  
  36.         {  
  37.             //根据类型做不同处理  
  38.         }  
  39.           
  40.         //中文必须设置字体,否则页面显示小方块  
  41.         Font xfont = new Font("宋体", Font.PLAIN, 20);// X轴    
  42.         Font yfont = new Font("宋体", Font.PLAIN, 12);// Y轴    
  43.         Font kfont = new Font("宋体", Font.PLAIN, 12);// 底部    
  44.         Font titleFont = new Font("隶书", Font.BOLD, 25); // 图片标题    
  45.   
  46.         DefaultCategoryDataset data = new DefaultCategoryDataset();  
  47.         data.addValue(15000"""第一季度");  
  48.         data.addValue(10000"""第二季度");  
  49.         data.addValue(20000"""第三季度");  
  50.         data.addValue(5000"""第四季度");  
  51.           
  52.         chart = ChartFactory.createBarChart3D(  
  53.                 type+"季度销量统计图",//图表标题  
  54.                 "季度",//横轴显示标签    
  55.                 "销量",//纵轴显示标签    
  56.                 data,//数据集  
  57.                 PlotOrientation.VERTICAL,//设置图表方向 (水平、垂直)  
  58.                 false,//是否显示图例(对于简单的柱状图必须是false)  
  59.                 false,//是否生成工具  
  60.                 true//是否生成URL链接  
  61.                 );  
  62.   
  63.         //设置标题    
  64.         chart.setTitle(new TextTitle(type+"季度销量统计图", titleFont));  

抱歉!评论已关闭.