1.导入需要的jar包(下载见附件)
struts2-jfreechart-plugin-2.0.11.jar
jcommon-1.0.0.jar
jfreechart-1.0.14.jar
2.编写xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="AnalysisAction" > <result-types> <result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"/> </result-types> <action name="getGenderBar" method="getGenderBar" class="analysisAction"> <result name="success" type="chart"> <param name="chart">chart</param> <param name="height">300</param> <param name="width">480</param> </result> </action> <action name="getGenderPie" method="getGenderPie" class="analysisAction"> <result name="success" type="chart"> <param name="chart">chart</param> <param name="height">300</param> <param name="width">480</param> </result> </action> </package> </struts>
* 注意开头部分result-type name="chart",status2的返回类型没有“char”类型,需要添加此返回类型。
3.编写生成图片的工具类BarChartUtil.java和status2的Acion
BarChartUtil.java
import java.awt.BasicStroke;
import com.rlzy.basic.AnalysisInfo;
public class BarChartUtil {
public static CategoryDataset createBarDataset(List<AnalysisInfo> list,
int type) {
DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset();
String rowkey = "";
String columnkey = "";
for (AnalysisInfo analysisInfo : list) {
columnkey = AnalysisTypeConstants.getTypeName(type, analysisInfo
.getType());
rowkey = columnkey;
localDefaultCategoryDataset.addValue(analysisInfo.getCount(),
rowkey, columnkey);
}
return localDefaultCategoryDataset;
}
public static PieDataset createPieDataset( List<AnalysisInfo> list,int type)
{
DefaultPieDataset localDefaultPieDataset = new DefaultPieDataset();
String key="";
for(AnalysisInfo analysisInfo:list){
key = AnalysisTypeConstants.getTypeName(type, analysisInfo.getType());
localDefaultPieDataset.setValue(key,analysisInfo.getCount());
}
return localDefaultPieDataset;
}
public static JFreeChart createBarChart(String title,
String categoryAxisLabel, String valueAxisLabel,
CategoryDataset paramCategoryDataset) {
// 创建主题样式
StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
// 设置标题字体
standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20));
// 设置图例的字体
standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15));
// 设置轴向的字体
standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15));
// 应用主题样式
ChartFactory.setChartTheme(standardChartTheme);
JFreeChart jfreechart = ChartFactory.createBarChart3D(title, // 图表标题
categoryAxisLabel, // 目录轴的显示标签
valueAxisLabel, // 数值轴的显示标签
paramCategoryDataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
true, // 是否显示图例(对于简单的柱状图必须是false)
true, // 是否生成工具
true // 是否生成URL链接
);
// chart.getTitle().setFont(new Font("宋体", Font.BOLD,12));
NumberAxis axis = (NumberAxis) jfreechart.getCategoryPlot()
.getRangeAxis();
axis.setTickUnit(new NumberTickUnit(5));// 0.5为一个间隔单位 Y
return jfreechart;
}
public static JFreeChart createPaiChart(String title,
PieDataset pieDataset) {
// 创建主题样式
StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
// 设置标题字体
standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20));
// 设置图例的字体
standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15));
// 设置轴向的字体
standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15));
// 应用主题样式
ChartFactory.setChartTheme(standardChartTheme);
JFreeChart chart = ChartFactory.createPieChart3D(
title, // 图表标题
pieDataset, true, // 是否显示图例
false, false);
PiePlot plot = (PiePlot) chart.getPlot();
resetPiePlot(plot);
return chart;
}
private static void resetPiePlot(PiePlot plot) {
String unitSytle = "{0}={1}({2})";
plot.setNoDataMessage("无对应的数据,请重新查询。");
plot.setNoDataMessagePaint(Color.red);
// 指定 section 轮廓线的厚度(OutlinePaint不能为null)
plot.setOutlineStroke(new BasicStroke(0));
// 设置第一个 section 的开始位置,默认是12点钟方向
plot.setStartAngle(90);
// plot.setToolTipGenerator(new StandardPieToolTipGenerator(unitSytle,
// NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
// 指定图片的透明度
plot.setForegroundAlpha(0.65f);
// 引出标签显示样式
// plot.setLabelGenerator(new StandardPieSectionLabelGenerator(unitSytle,
// NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
// 图例显示样式
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
unitSytle, NumberFormat.getNumberInstance(), new DecimalFormat(
"0.00%")));
}
}
AnalysisAction.java
public class AnalysisAction extends ActionSupport {
private BasicInfo basicInfo;
private BasicService basicService;
private static final long serialVersionUID = 1L;
private JFreeChart chart;
@Override
public String execute() throws Exception {
return SUCCESS;
}
public String getGenderBar() throws Exception {
List<AnalysisInfo> analysisList = basicService.getAnalysisByGender();
CategoryDataset dataset = BarChartUtil.createBarDataset(analysisList, 1);
chart = BarChartUtil.createBarChart("性别统计图", "性别", "人数", dataset);
return SUCCESS;
}
public String getGenderPie() throws Exception {
List<AnalysisInfo> analysisList = basicService.getAnalysisByGender();
PieDataset dataset = BarChartUtil.createPieDataset(analysisList, 1);
chart = BarChartUtil.createPaiChart("性别统计图", dataset);
return SUCCESS;
}
public BasicService getBasicService() {
return basicService;
}
public void setBasicService(BasicService basicService) {
this.basicService = basicService;
}
public void setChart(JFreeChart chart) {
this.chart = chart;
}
public JFreeChart getChart() {
return chart;
}
}
AnalysisInfo .java
import com.rlzy.util.Util;
public class AnalysisInfo {
private String type; // 类型
private int count; // 个数
public AnalysisInfo(String type, int count) {
super();
this.type = type;
this.count = count;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public AnalysisInfo() {
super();
}
}
4.编写展示jsp页面
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>人力资源管理系统</title>
<base href="<%=basePath%>">
<link href="<%=path%>/styles/sty.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<%=path %>/js/development-bundle/jquery.js"></script>
<style type="text/css">
<!--
body { background:#f2f2f2; }
-->
</style>
</head>
<script type="text/javascript">
$().ready(function() {
$("#gender").change(function(){
var gender = $("#gender").val();
if(gender=="1"){
$("#genderSrc").attr("src", "getGenderBar.action")
}else{
$("#genderSrc").attr("src", "getGenderPie.action");
}
});
})
</script>
<body>
<div class=" title_ljt">
<div class=" w50 floatleft">
<div class="border">
<div class=" title9">
<div class=" title9_a"></div>
<div class=" title9_b">按性别统计图示</div>
<div class=" title9_c">
<select id="gender" name="gender"><option value="1">柱状图</option> <option value="2">饼状图</option> </select>
</div>
</div>
<div class="h5"></div>
<div class="w100 aligncenter">
<img id="genderSrc" src="getGenderBar.action" />
</div>
<div class="h10"></div>
</div>
</div></div>
</body>
</html>