需要增加的jar包:
jfreechart-1.0.13.jar
jcommon-1.0.16.jar
struts2-jfreechart-plugin-2.1.8.1.jar
动作类
import java.io.Serializable;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import com.opensymphony.xwork2.ActionSupport;
public class GetChartAction extends ActionSupport implements Serializable {
private JFreeChart chart;
public JFreeChart getChart() {
return chart;
}
public String execute(){
ValueAxis xAxis = new NumberAxis("年度");
ValueAxis yAxis = new NumberAxis("产值");
XYSeries xySeries = new XYSeries("棉花");
xySeries.add(0,300);
xySeries.add(1,200);
xySeries.add(2,400);
xySeries.add(3,500);
xySeries.add(4,600);
xySeries.add(5,500);
xySeries.add(6,800);
xySeries.add(7,1000);
xySeries.add(8,1100);
XYSeriesCollection xyDataset = new XYSeriesCollection(xySeries);
XYPlot xyPlot = new XYPlot(xyDataset,xAxis,yAxis,new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES_AND_LINES));
chart = new JFreeChart(xyPlot);
return SUCCESS;
}
}
配置文件中的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="chart" extends="jfreechart-default">
<action name="chart" class="cn.wk.action.GetChartAction">
<result type="chart" name="success">
<param name="height">500</param>
<param name="width">600</param>
</result>
</action>
</package>
</struts>