在struts2中使用JFreeChartdasdasd

来源:百度文库 编辑:神马文学网 时间:2024/04/28 11:26:09
1、在struts2中,默认的struts-default.xml中,并没有包含chart的result-type,它是插件的形式使用的。把 struts2的解压包的lib里找到struts2-jfreechart-plugin-2.0.11.jar,拷贝到你的项目的 classpath里,同时在struts.xml里面增加一个chart的result-type。
Java代码
  1.   
  2.     "chart" class="org.apache.struts2.dispatcher.ChartResult"/>   
  3.   
  4. "ListAllFruit4Bar2" class="chart.ListAllFruit4Bar2Action">   
  5.     "success" type="chart">   
  6.         "chart">chart   
  7.         "height">400   
  8.         "width">700   
  9.        
  10.   
  11. "ListAllFruit4Pie2" class="chart.ListAllFruit4Pie2Action">   
  12.     "success" type="chart">   
  13.         "chart">chart   
  14.         "height">400   
  15.         "width">700   
  16.        
  17.   

2、需要再添加jfreechart-1.0.9.jar和jcommon-1.0.12.jar(JasperReports和JFreeChart都需要的)到你的classpath里面。
3、在你的action代码里,返回一个JFreeChart对象即可。
ListAllFruit4Bar2Action代码:
Java代码
  1. package chart;   
  2.   
  3. import org.jfree.chart.JFreeChart;   
  4.   
  5. import com.opensymphony.xwork2.ActionSupport;   
  6.   
  7. public class ListAllFruit4Bar2Action extends ActionSupport {   
  8.     private static final long serialVersionUID = 1L;   
  9.     private JFreeChart chart;   
  10.        
  11.     @Override  
  12.     public String execute() throws Exception {   
  13.         chart = FruitService.createBarChart();   
  14.         return SUCCESS;   
  15.     }   
  16.        
  17.     public JFreeChart getChart() {   
  18.         return chart;   
  19.     }   
  20. }  

ListAllFruit4Pie2Action代码:
Java代码
  1. package chart;   
  2.   
  3. import org.jfree.chart.JFreeChart;   
  4.   
  5. import com.opensymphony.xwork2.ActionSupport;   
  6.   
  7. public class ListAllFruit4Pie2Action extends ActionSupport {   
  8.     private static final long serialVersionUID = 1L;   
  9.     private JFreeChart chart;   
  10.        
  11.     @Override  
  12.     public String execute() throws Exception {   
  13.         chart = FruitService.createPaiChart();   
  14.         return SUCCESS;   
  15.     }   
  16.        
  17.     public JFreeChart getChart() {   
  18.         return chart;   
  19.     }   
  20. }  

FruitService代码:
Java代码
  1. package chart;   
  2.   
  3. import org.jfree.chart.ChartFactory;   
  4. import org.jfree.chart.JFreeChart;   
  5. import org.jfree.chart.plot.PlotOrientation;   
  6. import org.jfree.data.category.CategoryDataset;   
  7. import org.jfree.data.category.DefaultCategoryDataset;   
  8.   
  9. public class FruitService {   
  10.   
  11.     public static JFreeChart createBarChart() {   
  12.         CategoryDataset dataset = getDataSet2();   
  13.         JFreeChart chart = ChartFactory.createBarChart3D(   
  14.                 "水果产量图"// 图表标题   
  15.                 "水果"// 目录轴的显示标签   
  16.                 "产量"// 数值轴的显示标签   
  17.                 dataset, // 数据集   
  18.                 PlotOrientation.VERTICAL, // 图表方向:水平、垂直   
  19.                 true,   // 是否显示图例(对于简单的柱状图必须是false)   
  20.                 true,   // 是否生成工具   
  21.                 true    // 是否生成URL链接   
  22.                 );   
  23.         return chart;   
  24.     }   
  25.   
  26.     private static CategoryDataset getDataSet2() {   
  27.         DefaultCategoryDataset dataset = new DefaultCategoryDataset();   
  28.         dataset.addValue(100"北京""苹果");   
  29.         dataset.addValue(100"上海""苹果");   
  30.         dataset.addValue(100"广州""苹果");   
  31.         dataset.addValue(200"北京""梨子");   
  32.         dataset.addValue(200"上海""梨子");   
  33.         dataset.addValue(200"广州""梨子");   
  34.         dataset.addValue(300"北京""葡萄");   
  35.         dataset.addValue(300"上海""葡萄");   
  36.         dataset.addValue(300"广州""葡萄");   
  37.         dataset.addValue(400"北京""香蕉");   
  38.         dataset.addValue(400"上海""香蕉");   
  39.         dataset.addValue(400"广州""香蕉");   
  40.         dataset.addValue(500"北京""荔枝");   
  41.         dataset.addValue(500"上海""荔枝");   
  42.         dataset.addValue(500"广州""荔枝");   
  43.         return dataset;   
  44.     }   
  45.         
  46.     public static JFreeChart createPaiChart() {   
  47.         DefaultPieDataset data = getDataSet();   
  48.         JFreeChart chart = ChartFactory.createPieChart3D("水果产量图",  // 图表标题   
  49.         data,    
  50.         true// 是否显示图例   
  51.         false,   
  52.         false  
  53.         );   
  54.         PiePlot plot = (PiePlot) chart.getPlot();   
  55.         resetPiePlot(plot);   
  56.         return chart;   
  57.     }   
  58.        
  59.     private static void resetPiePlot(PiePlot plot) {   
  60.         String unitSytle = "{0}={1}({2})";   
  61.            
  62.         plot.setNoDataMessage("无对应的数据,请重新查询。");   
  63.         plot.setNoDataMessagePaint(Color.red);   
  64.            
  65.         //指定 section 轮廓线的厚度(OutlinePaint不能为null)   
  66.         plot.setOutlineStroke(new BasicStroke(0));   
  67.            
  68.         //设置第一个 section 的开始位置,默认是12点钟方向   
  69.         plot.setStartAngle(90);            
  70.   
  71.         plot.setToolTipGenerator(new StandardPieToolTipGenerator(unitSytle,   
  72.                 NumberFormat.getNumberInstance(),   
  73.                 new DecimalFormat("0.00%")));   
  74.            
  75.         //指定图片的透明度   
  76.         plot.setForegroundAlpha(0.65f);   
  77.            
  78.         //引出标签显示样式   
  79.         plot.setLabelGenerator(new StandardPieSectionLabelGenerator(unitSytle,   
  80.                 NumberFormat.getNumberInstance(),   
  81.                 new DecimalFormat("0.00%")));   
  82.                
  83.         //图例显示样式   
  84.         plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(unitSytle,   
  85.                 NumberFormat.getNumberInstance(),   
  86.                 new DecimalFormat("0.00%")));   
  87.     }   
  88.   
  89.     private static DefaultPieDataset getDataSet() {   
  90.         DefaultPieDataset dataset = new DefaultPieDataset();   
  91.         dataset.setValue("苹果",100);   
  92.         dataset.setValue("梨子",200);   
  93.         dataset.setValue("葡萄",300);   
  94.         dataset.setValue("香蕉",400);   
  95.         dataset.setValue("荔枝",500);   
  96.         return dataset;   
  97.     }   
  98.   
  99. }  

4、同时在jsp页面里显示--柱图和饼图。
增加一个ListAllFruit4BarAndPie的action:
Java代码
  1. "ListAllFruit4BarAndPie" class="">   
  2.     "success" type="dispatcher">/WEB-INF/chart/ListAllFruit4BarAndPie.jsp   
  3.   

ListAllFruit4BarAndPie.jsp代码:
Java代码
  1. <%@ page contentType="text/html; charset=UTF-8" %>   
  2.   
  3.   
  4.     ListAllFruit4BarAndPie   
  5.   
  6.   
  7.     

    使用柱图和饼图列取所有水果产量

      
  8.     柱图:"chart/ListAllFruit4Bar2.action">
      
  9.     饼图:"chart/ListAllFruit4Pie2.action">   
  10.   
  11.   


            
生成的HTML的效果图如下: