准备:需要的jar
jfreechar的jar
Struts的jar
Struts整合jfreechar的插件包 struts2-jfreechart-plugin-2.1.8.1.jar
1.struts.xml配置
<package name="char" namespace="/char" extends="jfreechart-default"> <action name="*-*" class="com.wepull.bookSys.action.{1}Action" method="{2}"> <result name = "success" type = "chart" > <param name = "width" > 600 </param > <param name = "height" > 400 </param > </result > </action> </package>
2. action配置
package com.wepull.jfreeChart.action;
import org.jfree.chart.JFreeChart;
import com.opensymphony.xwork2.ActionSupport;
import com.wepull.jfreeChart.chart.CreateChart;
public class ChartAction extends ActionSupport {
private JFreeChart chart; //属性名必须为chart
public String showChart(){
chart = CreateChart.exportPiechart();
return SUCCESS;
}
public JFreeChart getChart() { //必须通过get方法
return chart;
}
}
3. CreateChart 类
public class CreateChart {
public static JFreeChart exportPiechart() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("经济舱总售票" + 105, 105);
dataset.setValue("商务舱总售票" + 30, 30);
dataset.setValue("头等舱总售票" + 50, 50);
JFreeChart chart = ChartFactory.createPieChart3D("客户统计图", dataset,
true, true, true);
chart.setTitle(new TextTitle("售票统计图", new Font("黑体", Font.ITALIC, 14)));// 标题字体
// 设置图例部分
LegendTitle legend = chart.getLegend(0);
// 设置图的部分
PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelFont(new Font("宋体", Font.BOLD, 12));// 设置实际统计图的字体
plot.setBackgroundAlpha(0.9f);
plot.setForegroundAlpha(0.50f);
return chart;
}
}
4. 前台页面
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<a href="chart/Chart-showChart">查看统计图</a>
</body>
</html>