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; width: 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>
ChartResultAction.java:
/*** @Title:ChartResultAction.java* @u.action* @Description:* @author:Youhaidong(游海东)* @date:2013-7-28 上午10:55:26* @version V1.0*/
u.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 = ateBarChart("兴趣爱好统计分布图", "兴趣爱好", "统计次数", Dataset(), PlotOrientation.VERTICAL, true, true, false);//设置标题chart.setTitle(new TextTitle("兴趣爱好统计", new Font("黑体",Font.BOLD,20)));//获取CategoryPlot对象CategoryPlot categoryPlot = (Plot();//设置X,Y轴围起来的面板的颜色categoryPlot.setBackgroundPaint(Color.LIGHT_GRAY);//获取水平轴CategoryAxis categoryAxis = DomainAxis();//设置水平轴标签的字体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 = (RangeAxis();//设置Y轴坐标上的文字numberAxis.setTickLabelFont(new Font("宋体",Font.BOLD,20)); //设置Y轴的标题文字numberAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//解决Legend乱码问题 LegendTitle legendTitle = Legend(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()*/@Overridepublic String execute() throws Exception {return SUCCESS;}//创建实现每次请求的increaseResult()方法@SuppressWarnings({ "rawtypes", "unchecked" })private void increaseResult(List<String> list){//返回ActionContext实例ActionContext content = Context();//获取ApplicationMap map = Application();//遍历传入的请求参数listfor(String str : list){//判断选中的选项if(null == (str)){map.put(str, 1);}else{map.put(str, ((str) + 1);}}}//创建图像的数据集@SuppressWarnings("rawtypes")private CategoryDataset getDataset(){//创建数据集DefaultCategoryDataset dataset = new DefaultCategoryDataset();//处理当前的请求this.Interest());//获取ActionContext实例ActionContext content = Context();//获取Application对象Map map = Application();//为数据集中添加数值dataset.setValue((("basketball"), "篮球", "篮球");dataset.setValue((("football"), "足球", "足球");dataset.setValue((("tennis"), "网球", "网球");dataset.setValue((("yumiaoqiu"), "羽毛球", "羽毛球");dataset.setValue((("dengshan"), "登山", "登山");dataset.setValue((("swim"), "游泳", "游泳");dataset.setValue((("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;}}
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"".0.dtd">
<struts><package name="struts2" extends="jfreechart-default"><action name="chartResult" class=u.action.ChartResultAction"><result name="success" type="chart"><param name="width">1300</param><param name="height">600</param></result></action></package></struts>
(1)在浏览器地址栏输入:localhost:8080/votesystem/SelectVote.jsp,运行结果如下图:
(2)选择自己的兴趣爱好,点“提交”按钮
(3)当第二个人进入该系统,选择了自己的兴趣爱好,点击提交(以后进入系统的人都依次这样选择)
a、选择自己的兴趣爱好
b、点击提交之后
本文发布于:2024-01-28 17:34:00,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17064344459087.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |