JFreechart生成带热点的饼图

来源:互联网 发布:电脑加速软件 编辑:程序博客网 时间:2024/05/16 05:15

JFreechart生成带热点的饼图(原文地址:http://zhaobing315.iteye.com/blog/1000822)

jfreechartServletHTC三星Linux 

      前段时间公司要做报表开发,顺便研究了下JFreechart的简单使用,个人觉得JFreechart生成的图片效果很差,如:图像、字体模糊,缺乏动态效果,比flash相差很远。不过作为学习,跟大家交流下JFreechart的开发过程。

 

      遇到问题:

      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));  
  65.   
  66.         //设置图表部分  
  67.         CategoryPlot plot = (CategoryPlot) chart.getPlot();  
  68.         plot.setForegroundAlpha(0.5f);  
  69.         CategoryAxis categoryAxis = plot.getDomainAxis();//取得横轴  
  70.         categoryAxis.setTickLabelFont(xfont);//设置横轴分类标签字体    
  71.         categoryAxis.setLabelFont(xfont);//设置横轴显示标签的字体  
  72.         categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);//分类标签以45度倾斜  
  73.   
  74.         NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();//取得纵轴  
  75.         numberAxis.setTickLabelFont(yfont);//设置纵轴分类标签字体    
  76.         numberAxis.setLabelFont(yfont);//设置纵轴显示标签字体  
  77.   
  78.         //渲染柱子  
  79.         BarRenderer3D render = (BarRenderer3D) plot.getRenderer();  
  80.         render.setSeriesPaint(0, Color.blue);//设置柱子颜色  
  81.         render.setSeriesPaint(1, Color.BLUE);  
  82.         render.setSeriesPaint(2, Color.RED);  
  83.         render.setSeriesPaint(3, Color.GREEN);  
  84.         render.setItemMargin(0.5);//设置柱子间隔  
  85.         render.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());  
  86.         render.setBaseItemLabelsVisible(true);//显示每个柱子的数值  
  87.         plot.setRenderer(render);  
  88.         return SUCCESS;  
  89.     }  
  90.   
  91.     public JFreeChart getChart()  
  92.     {  
  93.         return chart;  
  94.     }  
  95.   
  96.     public void setChart(JFreeChart chart)  
  97.     {  
  98.         this.chart = chart;  
  99.     }  
  100.       
  101. }  

 3.web.xml配置

 

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>/DisplayChart</url-pattern>  
  8. </servlet-mapping>  
 

 4.struts.xml配置

 

Xml代码  收藏代码
  1.       <action name="firstChart" class="com.milton.hot.FirstChart">  
  2.               <result name="first">/first.jsp</result>  
  3.       </action>  
  4.       <action name="secondChart" class="com.milton.hot.SecondChart">  
  5.        <result type="chart">  
  6.         <param name="width">500</param>  
  7. <param name="height">500</param>  
  8. <param name="imageType">jpg</param>  
  9.        </result>  
  10.       </action>  

 5.first.jsp的<body>中添加如下代码

 

Html代码  收藏代码
  1. <p align="center">  
  2. <s:property value="mapMessage" escape="false"/>  
  3. <img src="<s:property value='src'/>" border=0 usemap="#map0">  
  4. </p>  

 6.second.jsp(热点链接进入后的柱状图页面)

 

Html代码  收藏代码
  1. <body>  
  2. <!-- 得到饼图热点类型 -->  
  3. <%  
  4.     String type = request.getParameter("type");  
  5.     String str = new String(type.getBytes("ISO_8859_1"), "utf-8");  
  6. %>  
  7. <%=str%>  
  8. <p align="center"><img  
  9.     src="<%=request.getContextPath()%>/secondChart.action?type=<%=str%>">  
  10. </p>  
  11. </body>  

 7.部署在web服务器后运行

 8.浏览器输入http://10.0.0.46:8080/JFreeChart/firstChart.action后即可看到first.jsp中带热点的饼图

 9.点击热点区域即可看到对应的柱状图

原创粉丝点击