1.下载jfreechart以及jfreechart plugin的相关包至工程的lib目录下
2.action代码如下:
@ParentPackage("admin-package")
public class StatisticAction extends ActionSupport{
private JFreeChart chart;
public String top_click_chart(){
XYSeries dataSeries = new XYSeries(new Integer(1)); //pass a key for this serie
for (int i = 0; i <= 100; i++) {
dataSeries.add(i, RandomUtils.nextInt());
}
XYSeriesCollection xyDataset = new XYSeriesCollection(dataSeries);
ValueAxis xAxis = new NumberAxis("Raw Marks");
ValueAxis yAxis = new NumberAxis("Moderated Marks");
// set my chart variable
chart =
new JFreeChart(
"Moderation Function",
JFreeChart.DEFAULT_TITLE_FONT,
new XYPlot(
xyDataset,
xAxis,
yAxis,
new StandardXYItemRenderer(StandardXYItemRenderer.LINES)),
false);
chart.setBackgroundPaint(java.awt.Color.white);
return "chart";
}
public JFreeChart getChart() {
return chart;
}
public void setChart(JFreeChart chart) {
this.chart = chart;
}
}
注意上面的action中,一定要有一个JFreeChart类型的属性,名称一定要为chart.这是定死了的.
3.上面的action返回的应该为chart的resulttype,在action类中配置如下:
@Results( { @Result(name = "chart",type = "chart",params={"width","400","height","300"}) })
注意上面的params属性,表示输出的chart的宽度和高度.
4.struts.xml中,找到相应的admin-package包,在其中加入:
<result-types>
<result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult">
<param name="height">150</param>
<param name="width">200</param>
</result-type>
</result-types>
5.浏览器中打开地址:admin/statistic!top_click_chart,一张漂亮的曲线图出来了。