1、该系统所用到的技术有:
(1)Struts2.x
(2)JFreeChart
2、该系统的文件结构如下图所示:
3、源码如下:
(1)JSP页面源码
SelectVote.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body style="background-color: #6FBACD; 50%; height: 50%;">
<h1><font color="blue">请选择自己的兴趣爱好:</font></h1>
<s:form action="chartResult">
<s:checkbox name="interest" label="篮球" fieldValue="basketball" labelposition="center"></s:checkbox>
<s:checkbox name="interest" label="足球" fieldValue="football" labelposition="center"></s:checkbox>
<s:checkbox name="interest" label="网球" fieldValue="tennis" labelposition="center"></s:checkbox>
<s:checkbox name="interest" label="羽毛球" fieldValue="yumiaoqiu" labelposition="center"></s:checkbox>
<s:checkbox name="interest" label="登山" fieldValue="dengshan" labelposition="center"></s:checkbox>
<s:checkbox name="interest" label="游泳" fieldValue="swim" labelposition="center"></s:checkbox>
<s:checkbox name="interest" label="围棋" fieldValue="weiqi" labelposition="center"></s:checkbox>
<s:submit value="提交" labelposition="center"></s:submit>
</s:form>
</body>
</html>
(2)Action源码
ChartResultAction.java:
/**
* @Title:ChartResultAction.java
* @Package:com.you.action
* @Description:
* @author:Youhaidong(游海东)
* @date:2013-7-28 上午10:55:26
* @version V1.0
*/
package com.you.action;
import java.awt.Color;
import java.awt.Font;
import java.util.List;
import java.util.Map;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 类功能说明
* 类修改者 修改日期
* 修改说明
* <p>Title:ChartResultAction.java</p>
* <p>Description:游海东个人开发</p>
* <p>Copyright:Copyright(c)2013</p>
* @author:游海东
* @date:2013-7-28 上午10:55:26
* @version V1.0
*/
public class ChartResultAction extends ActionSupport {
/**
* @Fields serialVersionUID:序列化
*/
private static final long serialVersionUID = 1L;
//创建chart属性
private JFreeChart chart;
//创建interest属性
private List<String> interest;
public JFreeChart getChart(){
//创建JFreeChart对象
chart = ChartFactory.createBarChart("兴趣爱好统计分布图", "兴趣爱好", "统计次数", this.getDataset(),
PlotOrientation.VERTICAL, true, true, false);
//设置标题
chart.setTitle(new TextTitle("兴趣爱好统计", new Font("黑体",Font.BOLD,20)));
//获取CategoryPlot对象
CategoryPlot categoryPlot = (CategoryPlot)chart.getPlot();
//设置X,Y轴围起来的面板的颜色
categoryPlot.setBackgroundPaint(Color.LIGHT_GRAY);
//获取水平轴
CategoryAxis categoryAxis = categoryPlot.getDomainAxis();
//设置水平轴标签的字体
categoryAxis.setLabelFont(new Font("宋体",Font.BOLD,20));
//设置水平轴标签的倾斜度
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
//设置X轴的标题文字
categoryAxis.setLabelFont(new Font("宋体",Font.BOLD,20));
//设置X轴坐标上的文字
categoryAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));
//设置水平轴标签的背景色
categoryPlot.setDomainGridlinePaint(Color.BLUE);
//获取竖直轴
NumberAxis numberAxis = (NumberAxis)categoryPlot.getRangeAxis();
//设置Y轴坐标上的文字
numberAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20));
//设置Y轴的标题文字
numberAxis.setLabelFont(new Font("宋体",Font.BOLD,20));
//解决Legend乱码问题
LegendTitle legendTitle = chart.getLegend(0);
//设置Legend的字体
legendTitle.setItemFont(new Font("宋体",Font.BOLD,20));
return chart;
}
/* (non-Javadoc)
* <p>Title:execute</p>
* <p>Description:</p>
* @return
* @throws Exception
* @see com.opensymphony.xwork2.ActionSupport#execute()
*/
@Override
public String execute() throws Exception {
return SUCCESS;
}
//创建实现每次请求的increaseResult()方法
@SuppressWarnings({ "rawtypes", "unchecked" })
private void increaseResult(List<String> list){
//返回ActionContext实例
ActionContext content = ActionContext.getContext();
//获取Application
Map map = content.getApplication();
//遍历传入的请求参数list
for(String str : list)
{
//判断选中的选项
if(null == map.get(str))
{
map.put(str, 1);
}else{
map.put(str, (Integer)map.get(str) + 1);
}
}
}
//创建图像的数据集
@SuppressWarnings("rawtypes")
private CategoryDataset getDataset(){
//创建数据集
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
//处理当前的请求
this.increaseResult(this.getInterest());
//获取ActionContext实例
ActionContext content = ActionContext.getContext();
//获取Application对象
Map map = content.getApplication();
//为数据集中添加数值
dataset.setValue((Integer)map.get("basketball"), "篮球", "篮球");
dataset.setValue((Integer)map.get("football"), "足球", "足球");
dataset.setValue((Integer)map.get("tennis"), "网球", "网球");
dataset.setValue((Integer)map.get("yumiaoqiu"), "羽毛球", "羽毛球");
dataset.setValue((Integer)map.get("dengshan"), "登山", "登山");
dataset.setValue((Integer)map.get("swim"), "游泳", "游泳");
dataset.setValue((Integer)map.get("weiqi"), "围棋", "围棋");
return dataset;
}
/**
* @return the interest
*/
public List<String> getInterest() {
return interest;
}
/**
* @param interest the interest to set
*/
public void setInterest(List<String> interest) {
this.interest = interest;
}
/**
* @param chart the chart to set
*/
public void setChart(JFreeChart chart) {
this.chart = chart;
}
}
(3)struts.xml配置文件
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="jfreechart-default">
<action name="chartResult" class="com.you.action.ChartResultAction">
<result name="success" type="chart">
<param name="width">1300</param>
<param name="height">600</param>
</result>
</action>
</package>
</struts>
4、运行结果如下
(1)在浏览器地址栏输入:http://localhost:8080/votesystem/SelectVote.jsp,运行结果如下图:
(2)选择自己的兴趣爱好,点“提交”按钮
(3)当第二个人进入该系统,选择了自己的兴趣爱好,点击提交(以后进入系统的人都依次这样选择)
a、选择自己的兴趣爱好
b、点击提交之后