首页 > 代码库 > struts2中 JFreeChart使用

struts2中 JFreeChart使用

添加3个包

struts2-jfreechart-plugin-2.3.16.3.jar

jcommon-1.0.16.jar

jfreechart-1.0.13.jar

 

struts.xml中配置

查看struts2-jfreechart-plugin-2.3.16.3.jar中的struts-plugin.xml文件

查看其中的定义的东西 然后写着struts.xml中

1     <package name="freechart" extends="jfreechart-default">
2         <action name="freechart" class="cn.itcast.chart.MyFreeChart">
3 
4                 <result type="chart" name="success" >
5                     <param name="height">800</param>
6                     <param name="width">600</param>
7                 </result>
8         </action>
9     </package>

写Action

 1 package cn.itcast.chart;
 2 
 3 import java.io.Serializable;
 4 
 5 import org.jfree.chart.JFreeChart;
 6 import org.jfree.chart.axis.NumberAxis;
 7 import org.jfree.chart.axis.ValueAxis;
 8 import org.jfree.chart.plot.XYPlot;
 9 import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
10 import org.jfree.data.xy.XYSeries;
11 import org.jfree.data.xy.XYSeriesCollection;
12 
13 import com.opensymphony.xwork2.ActionSupport;
14 
15 public class MyFreeChart extends ActionSupport implements Serializable {
16     private JFreeChart chart;
17 
18     public JFreeChart getChart() {
19         return chart;
20     }
21     public String execute(){
22         ValueAxis xAxis = new NumberAxis("年度");
23         ValueAxis yAxis = new NumberAxis("产值");
24         XYSeries xySeries = new XYSeries("绿豆");
25         xySeries.add(0,300);
26         xySeries.add(1,200);
27         xySeries.add(2,400);
28         xySeries.add(3,500);
29         xySeries.add(4,600);
30         xySeries.add(5,500);
31         xySeries.add(6,800);
32         xySeries.add(7,1000);
33         xySeries.add(8,1100);
34         XYSeriesCollection xyDataset = new XYSeriesCollection(xySeries);
35         XYPlot xyPlot = new XYPlot(xyDataset,xAxis,yAxis,new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES_AND_LINES));
36         chart = new JFreeChart(xyPlot);
37         return SUCCESS;
38     }
39 }

就能返回图表