调查统计系统--统计分析

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>设计调查</title>
        <link rel="stylesheet" type="text/css" href='<s:url value="/styles.css" />'>
        <script type="text/javascript" src="<s:url value="/jquery-1.7.1.js" />"></script>
    </head>
    <body>
        <s:include value="/header.jsp" />
        <s:set var="sId" value="id" />
        <table>
                <tr>
                    <td colspan="2" class="tdWhiteLine"></td>
                </tr>
                <tr>
                    <td colspan="2" class="tdHeader">分析调查:</td>
                </tr>
                <tr>
                    <td colspan="2" class="tdWhiteLine"></td>
                </tr>
                <tr>
                    <td colspan="2" class="tdHeader"><s:property value="title" /></td>
                </tr>
                <tr>
                    <td colspan="2" class="tdWhiteLine"></td>
                </tr>
                <!-- 遍历调查的页面集合 -->
                <s:iterator var="p" value="pages" status="pst">
                    <!-- 设置变量,对当前的Page的id属性进行保持 -->
                    <s:set var="pId" value="#p.id" />
                    <tr>
                        <td colspan="2" class="tdPHeaderL"><s:property value="#p.title" /></td>
                    </tr>
                    <tr>
                        <td width="30px"></td>
                        <td>
                            <table>
    <!-- 遍历问题集合 -->
    <s:iterator var="q" value="#p.questions" status="qst">
        <s:set var="qId" value="#q.id" />
        <s:set var="qt" value="#q.questionType" />
        <tr>
            <!-- count:从1开始 index:从0开始 -->
            <td class="tdQHeaderL" style="border-bottom: 1px solid white"><s:property value="#qst.count+'.' + #q.title" /></td>
            <td class="tdQHeaderR" style="border-bottom: 1px solid white">
                <s:form action="ChartOutputAction" namespace="/" method="post" target="_blank">
                    <input type="hidden" name="qid" value='<s:property value="#qId" />'>
                    <!-- 判断当前题型是否矩阵式题型 -->
                    <s:if test='#qt > 5 '>
                        <!-- 提交给另外一个action,改变form的提交地址 -->
                        <s:submit action="MatrixStatisticsAction" value="查看矩阵式问题统计结果" cssClass="btn"/>
                    </s:if>
                    <s:elseif test='#qt lt 5'>
                        <s:set var="chartList" value="#{0:'平面饼图',
                                                        1:'立体饼图',
                                                        2:'横向平面柱状图',
                                                        3:'纵向平面柱状图',
                                                        4:'横向立体柱状图',
                                                        5:'纵向立体柱状图',
                                                        6:'平面折线图',
                                                        7:'立体折线图'}"/>
                        <s:select name="chartType" list="#chartList" listKey="key" listValue="value" />
                        <s:submit value="查看" cssClass="btn" />
                    </s:elseif>
                </s:form>
            </td>
        </tr>
    </s:iterator>
                            </table>
                        </td>
                    </tr>
                </s:iterator>
            </table>
    </body>
</html>
analyzeSurveyList.jsp

针对非矩阵式题型的设计,可有饼图 柱状图 折线图的action

package cn.itcast.surveypark.struts2.action;

import java.awt.Font;

import javax.annotation.Resource;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.Rotation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.surveypark.domain.Question;
import cn.itcast.surveypark.domain.statistics.OptionStatisticsModel;
import cn.itcast.surveypark.domain.statistics.QuestionStatisticsModel;
import cn.itcast.surveypark.service.StatisticsService;

/**
 * 针对非矩阵式题型的设计,可有饼图 柱状图 折线图等
 */
@Controller
@Scope("prototype")
public class ChartOutputAction extends BaseAction<Question> {
    private static final long serialVersionUID = -9021947290801678287L;
    /* 平面饼图 */
    private static final int CHARTTYPE_PIE_2D = 0;
    /* 立体饼图 */
    private static final int CHARTTYPE_PIE_3D = 1;
    /* 水平平面柱状图 */
    private static final int CHARTTYPE_BAR_2D_H = 2;
    /* 竖直平面柱状图 */
    private static final int CHARTTYPE_BAR_2D_V = 3;
    /* 水平立体柱状图 */
    private static final int CHARTTYPE_BAR_3D_H = 4;
    /* 竖直立体柱状图 */
    private static final int CHARTTYPE_BAR_3D_V = 5;
    /* 平面折线图 */
    private static final int CHARTTYPE_LINE_2D = 6;
    /* 立体折线图 */
    private static final int CHARTTYPE_LINE_3D = 7;

    // 图表类型
    private int chartType;
    // 对哪个问题进行统计
    private Integer qid;

    // 注入统计服务
    @Resource
    private StatisticsService ss;

    /**
     * 生成图表并输出到浏览器
     */
    public String execute() {
        return SUCCESS;
    }

    @SuppressWarnings("deprecation")
    public JFreeChart getChart() {
        JFreeChart chart = null ;
        try {
            Font font = new Font("宋体", 0, 20);// 字体
            QuestionStatisticsModel qsm = ss.statistics(qid);
            DefaultPieDataset pieds = null;// 饼图的数据集
            DefaultCategoryDataset cateds = null;// 种类数据集
            //构造数据集
            if(chartType < 2){
                pieds = new DefaultPieDataset();
                for (OptionStatisticsModel om : qsm.getOsms()) {
                    pieds.setValue(om.getOptionLabel(), om.getCount());
                }
            }
            else{
                cateds = new DefaultCategoryDataset();
                for (OptionStatisticsModel osm : qsm.getOsms()) {
                    cateds.addValue(osm.getCount(), osm.getOptionLabel(), "");
                }
            }
            
            // 判断要求的图形
            switch (chartType) {
                case CHARTTYPE_PIE_2D:// 平面饼图
                    chart = ChartFactory.createPieChart(qsm.getQuestion().getTitle(), pieds, true, false, false);
                    break ;
                case CHARTTYPE_PIE_3D:// 立体饼图
                    chart = ChartFactory.createPieChart3D(qsm.getQuestion().getTitle(), pieds, true, true, true);
                    //设置前景色透明度
                    chart.getPlot().setForegroundAlpha(0.6f);
                    break ;
                case CHARTTYPE_BAR_2D_H:// 平面条形图
                    chart = ChartFactory.createBarChart(qsm.getQuestion().getTitle(), "", "", cateds,
                            PlotOrientation.HORIZONTAL, true, true, true);
                    break ;
                case CHARTTYPE_BAR_2D_V:// 平面条形图
                    chart = ChartFactory.createBarChart(qsm.getQuestion().getTitle(), "", "", cateds,
                            PlotOrientation.VERTICAL, true, true, true);
                case CHARTTYPE_BAR_3D_H:// 平面条形图
                    chart = ChartFactory.createBarChart3D(qsm.getQuestion().getTitle(), "", "", cateds,
                            PlotOrientation.HORIZONTAL, true, true, true);
                case CHARTTYPE_BAR_3D_V:// 平面条形图
                    chart = ChartFactory.createBarChart3D(qsm.getQuestion().getTitle(), "", "", cateds,
                            PlotOrientation.VERTICAL, true, true, true);
                    break ;
                //
                case CHARTTYPE_LINE_2D:// 平面条形图
                    chart = ChartFactory.createLineChart(qsm.getQuestion().getTitle(), "", "", cateds,
                            PlotOrientation.VERTICAL, true, true, true);
                    break ;
                case CHARTTYPE_LINE_3D:// 平面条形图
                    chart = ChartFactory.createLineChart3D(qsm.getQuestion().getTitle(), "", "", cateds,
                            PlotOrientation.HORIZONTAL, true, true, true);
                    break ;
            }
            //设置标题和提示条中文
            chart.getTitle().setFont(font);
            chart.getLegend().setItemFont(font);
            //chart.setBackgroundImageAlpha(0.2f);
        
            //设置饼图特效
            if(chart.getPlot() instanceof PiePlot){
                PiePlot pieplot = (PiePlot) chart.getPlot();
                pieplot.setLabelFont(font);
                pieplot.setExplodePercent(0, 0.1);
                pieplot.setStartAngle(-15);
                pieplot.setDirection(Rotation.CLOCKWISE);
                pieplot.setNoDataMessage("No data to display");
                //pieplot.setForegroundAlpha(0.5f);
                //pieplot.setBackgroundImageAlpha(0.3f);
            }
            //设置非饼图效果
            else{
                chart.getCategoryPlot().getRangeAxis().setLabelFont(font);
                chart.getCategoryPlot().getRangeAxis().setTickLabelFont(font);
                chart.getCategoryPlot().getDomainAxis().setLabelFont(font);
                chart.getCategoryPlot().getDomainAxis().setTickLabelFont(font);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return chart ;
    }

    public int getChartType() {
        return chartType;
    }

    public void setChartType(int chartType) {
        this.chartType = chartType;
    }

    public Integer getQid() {
        return qid;
    }

    public void setQid(Integer qid) {
        this.qid = qid;
    }
}
chartOutputAction

 

数据统计方法

package cn.itcast.surveypark.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.itcast.surveypark.dao.BaseDao;
import cn.itcast.surveypark.domain.Answer;
import cn.itcast.surveypark.domain.Question;
import cn.itcast.surveypark.domain.statistics.OptionStatisticsModel;
import cn.itcast.surveypark.domain.statistics.QuestionStatisticsModel;
import cn.itcast.surveypark.service.StatisticsService;

/**
 * 统计服务
 */
@Service("statisticsService")
public class StatisticsServiceImpl implements StatisticsService {

    @Resource(name="questionDao")
    private BaseDao<Question> questionDao ;
    
    @Resource(name="answerDao")
    private BaseDao<Answer> answerDao ;
    
    /**
     * 统计问题,返回问题统计模型
     */
    public QuestionStatisticsModel statistics(Integer qid) {
        QuestionStatisticsModel qsm = new QuestionStatisticsModel();
        Question q = questionDao.getEntity(qid);
        qsm.setQuestion(q);
        
        //统计问题回答人数
        String hql = "select count(*) from Answer a where a.questionId = ?" ;
        int qcount = ((Long)answerDao.uniqueResult(hql,qid)).intValue();
        qsm.setCount(qcount);
        
        //选项统计hql
        String ohql = "select count(*) from Answer a where a.questionId = ? and concat(',',a.answerIds,',') like ?" ;
        //选项人数
        int ocount = 0;
        
        //统计选项
        int qt = q.getQuestionType();
        switch(qt){
            //非矩阵问题
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
                String[] optArr = q.getOptionArr();
                OptionStatisticsModel osm = null ;
                //统计每个选项
                for(int i = 0 ; i < optArr.length ; i ++){
                    osm = new OptionStatisticsModel();
                    osm.setOptionLabel(optArr[i]);
                    osm.setOptionIndex(i);
                    ocount = ((Long)answerDao.uniqueResult(ohql, qid,"%,"+i+",%")).intValue();
                    osm.setCount(ocount);
                    qsm.getOsms().add(osm);
                }
                //其他项统计
                if(q.isOther()){
                    osm = new OptionStatisticsModel();
                    osm.setOptionLabel("其他");
                    ocount = ((Long)answerDao.uniqueResult(ohql, qid,"%other%")).intValue();
                    osm.setCount(ocount);
                    qsm.getOsms().add(osm);
                }
                break ;
                
            //矩阵问题
            case 6:
            case 7:
            case 8:
                String[] rows = q.getMatrixRowTitleArr();
                String[] cols = q.getMatrixColTitleArr();
                String[] opts = q.getMatrixSelectOptionArr();
                for(int i = 0 ; i < rows.length ; i ++){
                    for(int j = 0 ; j < cols.length ; j ++){
                        //radio|checkbox
                        if(qt != 8){
                            osm = new OptionStatisticsModel();
                            osm.setMatrixRowLabel(rows[i]);
                            osm.setMatrixRowIndex(i);
                            osm.setMatrixColLabel(cols[j]);
                            osm.setMatrixColIndex(j);
                            ocount = ((Long)answerDao.uniqueResult(ohql, qid,"%," + i + "_" + j + ",%")).intValue();
                            osm.setCount(ocount);
                            qsm.getOsms().add(osm);
                        }
                        //select
                        else{
                            for(int k = 0 ; k < opts.length ; k ++){
                                osm = new OptionStatisticsModel();
                                osm.setMatrixRowLabel(rows[i]);
                                osm.setMatrixRowIndex(i);
                                osm.setMatrixColLabel(cols[j]);
                                osm.setMatrixColIndex(j);
                                osm.setMatrixSelectLabel(opts[k]);
                                osm.setMatrixSelectIndex(k);
                                ocount = ((Long)answerDao.uniqueResult(ohql, qid,"%," + i + "_" + j + "_" + k + ",%")).intValue();
                                osm.setCount(ocount);
                                qsm.getOsms().add(osm);
                            }
                        }
                    }
                }
                break ;
        }
        return qsm;
    }

}
statisticsServiceImpl

封装统计数据的model

**
 * 问题统计模型
 */
public class QuestionStatisticsModel {
    private Question question;

    private int count;

    private List<OptionStatisticsModel> osms = new ArrayList<OptionStatisticsModel>();

}


===============================


/**
 * 选项统计模型
 */
public class OptionStatisticsModel {
    // 选项标签
    private String optionLabel;
    // 选项索引
    private int optionIndex;

    private String matrixRowLabel;
    private int matrixRowIndex;

    private String matrixColLabel;
    private int matrixColIndex;

    //用于最后题型的矩阵下拉选项
    private String matrixSelectLabel;
    private int matrixSelectIndex;

    // 选项的回答人数
    private int count;


}
View Code

 

查看矩阵式问题统计结果 页面可以调用getScale,getPercent

package cn.itcast.surveypark.struts2.action;

import java.text.DecimalFormat;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.surveypark.domain.Question;
import cn.itcast.surveypark.domain.statistics.OptionStatisticsModel;
import cn.itcast.surveypark.domain.statistics.QuestionStatisticsModel;
import cn.itcast.surveypark.service.StatisticsService;

/**
 * MatrixStatisticsAction
 */
@Controller
@Scope("prototype")
public class MatrixStatisticsAction extends BaseAction<Question> {

    private static final long serialVersionUID = -4731907693006321046L;
    
    private String[] colors = {
        "#ff0000",
        "#00ff00",
        "#0000ff",
        "#ffff00",
        "#ff00ff",
        "#000fff",
    };
    
    public String[] getColors() {
        return colors;
    }

    private Integer qid ;
    
    private QuestionStatisticsModel qsm ;
    
    public QuestionStatisticsModel getQsm() {
        return qsm;
    }

    public void setQsm(QuestionStatisticsModel qsm) {
        this.qsm = qsm;
    }

    @Resource
    private StatisticsService ss ;
    
    public Integer getQid() {
        return qid;
    }

    public void setQid(Integer qid) {
        this.qid = qid;
    }

    public String execute() throws Exception {
        this.qsm = ss.statistics(qid);
        return "" + qsm.getQuestion().getQuestionType() ;
    }
    
    /**
     * 计算比例(从前台把每个小格子的坐标传入,调用数据统计结果计算每个格子的比例)
     */
    public String getScale(int rindex , int cindex){
        int qcount = qsm.getCount();
        int ocount = 0 ;
        for(OptionStatisticsModel osm : qsm.getOsms()){
            if(osm.getMatrixRowIndex() == rindex
                    && osm.getMatrixColIndex() == cindex){
                ocount = osm.getCount();
                break ;
            }
        }
        float scale = 0 ;
        if(qcount != 0){
            scale = (float)ocount / (float)qcount ;
        }
        scale = scale * 100 ;
        DecimalFormat format = new DecimalFormat();//数字的格式转换
        format.applyPattern("#,###.00");
        return "" + ocount + "(" + format.format(scale) + ")";
    }
    
    /**
     * 计算比例
     */
    public String getScale(int rindex , int cindex,int oindex){
        int qcount = qsm.getCount();
        int ocount = 0 ;
        for(OptionStatisticsModel osm : qsm.getOsms()){
            if(osm.getMatrixRowIndex() == rindex
                    && osm.getMatrixColIndex() == cindex
                    && osm.getMatrixSelectIndex() == oindex){
                ocount = osm.getCount();
                break ;
            }
        }
        float scale = 0 ;
        if(qcount != 0){
            scale = (float)ocount / (float)qcount ;
        }
        scale = scale * 100 ;
        DecimalFormat format = new DecimalFormat();
        format.applyPattern("#,###.00");
        return "" + ocount + "(" + format.format(scale) + ")";
    }
    
    /**
     * 计算比例
     */
    public int getPercent(int rindex , int cindex,int oindex){
        int qcount = qsm.getCount();
        int ocount = 0 ;
        for(OptionStatisticsModel osm : qsm.getOsms()){
            if(osm.getMatrixRowIndex() == rindex
                    && osm.getMatrixColIndex() == cindex
                    && osm.getMatrixSelectIndex() == oindex){
                ocount = osm.getCount();
                break ;
            }
        }
        float scale = 0 ;
        if(qcount != 0){
            scale = (float)ocount / (float)qcount ;
        }
        scale = scale * 100 ;
        return (int)scale ;
    }
}
MatrixStatisticsAction
<action name="MatrixStatisticsAction" class="matrixStatisticsAction">
            <result name="6">/matrixNormalStatistics.jsp</result>
            <result name="7">/matrixNormalStatistics.jsp</result>
            <result name="8">/matrixSelectStatistics.jsp</result>
        </action>
View Code
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>矩阵型问题设计</title>
        <link rel="stylesheet" type="text/css" href='<s:url value="/styles.css" />'>
    </head>
    <body>
        <s:include value="/header.jsp" />
        <table>
                <tr>
                    <td colspan="2" class="tdWhiteLine"></td>
                </tr>
                <tr>
                    <td colspan="2" class="tdHeader">矩阵式题型调查结果分析:</td>
                </tr>
                <tr>
                    <td colspan="2" class="tdWhiteLine"></td>
                </tr>
                <tr>
                    <td colspan="2" class="tdHeader"><s:property value="qsm.question.title" /></td>
                </tr>
                <tr>
                    <td colspan="2" class="tdWhiteLine"></td>
                </tr>
                <tr>
                    <td>
                        <table>
                            <!-- 画表头 -->
                            <tr>
                                <td width="30px"></td>
                                <s:iterator value="qsm.question.matrixColTitleArr">
                                    <td><s:property /></td>
                                </s:iterator>
                            </tr>
                            <s:iterator var="row" value="qsm.question.matrixRowTitleArr" status="rst">
                                <tr>
                                    <!-- 行头 -->
                                    <td><s:property/></td>
                                    <s:iterator var="col" value="qsm.question.matrixColTitleArr" status="cst">
                                        <td>
                                            <!-- ognl不仅可以访问属性,还可以直接调方法 -->
                                            <s:property value='getScale(#rst.index,#cst.index)' />
                                        </td>
                                    </s:iterator>
                                </tr>
                            </s:iterator>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align: left;">共有&nbsp;<s:property value="qsm.count" />&nbsp;人参与问卷!</td>
                </tr>
            </table>
    </body>
</html>
matrixNormalStatistics.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>矩阵型问题设计</title>
        <link rel="stylesheet" type="text/css" href='<s:url value="/styles.css" />'>
    </head>
    <body>
        <s:include value="/header.jsp" />
        <s:set var="qt" value="qsm.question.questionType" />
        <table>
                <tr>
                    <td colspan="2" class="tdWhiteLine"></td>
                </tr>
                <tr>
                    <td colspan="2" class="tdHeader">矩阵式下拉列表题型调查结果分析:</td>
                </tr>
                <tr>
                    <td colspan="2" class="tdWhiteLine"></td>
                </tr>
                <tr>
                    <td colspan="2" class="tdHeader"><s:property value="qsm.question.title" /></td>
                </tr>
                <tr>
                    <td colspan="2" class="tdWhiteLine"></td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align: left;display: inline">
                        <!-- 创建左上角颜色块,用不同颜色代表不同下拉列表选项 -->
                        <s:iterator value="qsm.question.matrixSelectOptionArr" status="optst">
                            <!-- 对文本框进行修饰,产生颜色块的特效 -->
                            <input type="text" readonly="readonly" style="border:1px solid blue;background-color: <s:property value='colors[#optst.index]'/>;width: 12px;height: 12px" > <s:property/>
                        </s:iterator>
                    </td>
                </tr>
                <tr>
                    <td>
                        <table>
                            <!-- 输出表头 -->
                            <tr>
                                <td width="30px"></td>
                                <s:iterator value="qsm.question.matrixColTitleArr">
                                    <!-- 集合中每个元素都是字符串,直接通过s:property 方式输出 -->
                                    <td><s:property /></td>
                                </s:iterator>
                            </tr>
    <!-- 遍历行标题数组 -->
    <s:iterator var="row" value="qsm.question.matrixRowTitleArr" status="rst">
        <tr>
            <td><s:property /></td>
            <!--  遍历列表数组 -->
            <s:iterator var="col" value="qsm.question.matrixColTitleArr" status="cst">
                <td style="text-align: left;">
                    <!-- 遍历下拉选项数组 -->
                    <s:iterator var="option" value="qsm.question.matrixSelectOptionArr" status="optst">
                        <input type="text" style="border:1px solid <s:property value='colors[#optst.index]'/>;
                                                  background-color: <s:property value='colors[#optst.index]'/>;
                                                  width: <s:property value='getPercent(#rst.index,#cst.index,#optst.index)'/>px;
                                                  height: 12px" readonly="readonly">
                        <!-- 动态调用action中的方法 -->
                        <s:property value='getScale(#rst.index,#cst.index,#optst.index)'/><br>
                    </s:iterator>
                </td>
            </s:iterator>
        </tr>
    </s:iterator>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align: left;">共有&nbsp;<s:property value="qsm.count" />&nbsp;人参与问卷</td>
                </tr>
            </table>
    </body>
</html>            
matrixSelectStatistics.jsp

 

 

posted @ 2017-04-14 15:00  SKYisLimit  阅读(170)  评论(0)    收藏  举报