📄 gettestanswersdistributionaction.java
字号:
/* * CyberTester - J2EE Web application for creating, delivering and managing tests/exams/surveys. * Copyright (C) 2003 CyberDemia Research and Services Pty Ltd * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program (see the file COPYING); if not, write to the * Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * See the COPYING file located in the top-level-directory of * the archive of this program for complete text of license. */package com.cyberdemia.cybertester.admin.report;import java.awt.Font;import java.io.ByteArrayOutputStream;import java.util.*;import javax.servlet.http.*;import org.apache.struts.action.*;import com.cyberdemia.school.*;import com.cyberdemia.cybertester.*;import java.util.logging.*;import com.cyberdemia.cybertester.admin.*;import com.cyberdemia.cybertester.common.*;import org.jfree.chart.*;import org.jfree.data.*;import org.jfree.chart.labels.*;import org.jfree.chart.axis.*;import org.jfree.chart.renderer.*;import org.jfree.chart.entity.StandardEntityCollection;import org.jfree.chart.plot.PlotOrientation;/** * * GetTestAnswersDistributionAction is a Struts action to retrieve distribution of * all users' answers for a test. * * @author Alexander Yap */public final class GetTestAnswersDistributionAction extends AbstractAdminAction { protected ActionForward doPerform( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception { String ownerHierId = (String)session.getAttribute(AdminConstants.SELECTED_HIERARCHY_ID_KEY); if (ownerHierId==null) { ActionErrors errors = new ActionErrors(); errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.hierarchyNode.unselected")); saveErrors(request, errors); return mapping.findForward(Constants.ERROR); } Logger logger = CyberTesterUtils.getLogger(); String testId = CyberTesterUtils.getParameterOrRequestAttribute(request,AdminConstants.TEST_ID_KEY); // Some forms will send in "NONE", convert it to null if ("NONE".equals(testId)) { testId = null; } try { TestManagerHome tMgrHome = CyberTesterUtils.getTestManagerHome(); TestManager testMgr = (TestManager)tMgrHome.create(); TestData[] testList = testMgr.getTests(ownerHierId, true); request.setAttribute( AdminConstants.TEST_LIST_KEY, testList ); if (testId!=null) { logger.fine("Retrieving answers distribution assigned to Test ID "+testId); request.setAttribute(AdminConstants.TEST_ID_KEY, testId); // Calculation various distributions by questions ReportGeneratorHome reportGenHome = CyberTesterUtils.getReportGeneratorHome(); ReportGenerator reportGen = (ReportGenerator)reportGenHome.create(); DefaultCategoryDataset answerDataset = new DefaultCategoryDataset(); DefaultCategoryDataset percentageDataset = new DefaultCategoryDataset(); Map answersDistMap = reportGen.getAnswersDistributionForTest(testId); Map percentageDistMap = reportGen.getScorePercentageDistributionForTest(testId, ReportGenerator.PERCENTAGE_BY_QUESTION); Iterator quesIndexIter = answersDistMap.keySet().iterator(); int maxAnswerCount = 20; while( quesIndexIter.hasNext()) { Integer quesIndex = (Integer)quesIndexIter.next(); Integer answerDistVal = (Integer)answersDistMap.get(quesIndex); if (answerDistVal.intValue() > maxAnswerCount ) { maxAnswerCount = answerDistVal.intValue(); } Double percentageDistVal = (Double)percentageDistMap.get(quesIndex); answerDataset.addValue(answerDistVal, "0", quesIndex); percentageDataset.addValue(percentageDistVal, "0", quesIndex); } JFreeChart answerDistChart = ChartFactory.createBarChart( "", "Question index", "Number of correct answers", answerDataset, PlotOrientation.VERTICAL, false, true, false); answerDistChart.setTitle(new TextTitle("Number of Correct Answers vs Questions", new Font("SansSerif",Font.BOLD, 12))); ((CategoryAxis)answerDistChart.getCategoryPlot().getDomainAxis()).setVerticalCategoryLabels(true); ValueAxis rangeAxis = answerDistChart.getCategoryPlot().getRangeAxis(); rangeAxis.setMaximumAxisValue(maxAnswerCount); rangeAxis.setMinimumAxisValue(0.0); CategoryItemRenderer renderer = answerDistChart.getCategoryPlot().getRenderer(); renderer.setItemLabelGenerator( new CategoryItemLabelGenerator() { public String generateItemLabel(CategoryDataset data, int series, int category) { return data.getValue(series, category).toString(); } public String generateToolTip(CategoryDataset data, int series, int category) { return "Number of correct answers for question "+data.getColumnKey(category)+" : "+data.getValue(series, category).toString(); } } ); answerDistChart.setAntiAlias(true); JFreeChart percentageDistChart = ChartFactory.createBarChart( "", "Question index", "Percentage of correct answers", percentageDataset, PlotOrientation.VERTICAL, false, true, false); percentageDistChart.setTitle(new TextTitle("Percentage of Correct Answers vs Questions", new Font("SansSerif",Font.BOLD, 12))); ((CategoryAxis)percentageDistChart.getCategoryPlot().getDomainAxis()).setVerticalCategoryLabels(true); rangeAxis = percentageDistChart.getCategoryPlot().getRangeAxis(); rangeAxis.setMaximumAxisValue(100.0); rangeAxis.setMinimumAxisValue(0.0); renderer = percentageDistChart.getCategoryPlot().getRenderer(); renderer.setItemLabelGenerator( new CategoryItemLabelGenerator() { public String generateItemLabel(CategoryDataset data, int series, int category) { return data.getValue(series, category).toString(); } public String generateToolTip(CategoryDataset data, int series, int category) { return "Percentage of correct answers for question "+data.getColumnKey(category)+" : "+data.getValue(series, category).toString(); } } ); percentageDistChart.setAntiAlias(true); int width = Math.max(300, 100+(answerDataset.getColumnCount()*20)); int height = 400; try { width = Integer.parseInt( request.getParameter("width") ); } catch (NumberFormatException nfex) { } try { height = Integer.parseInt( request.getParameter("height") ); } catch (NumberFormatException nfex) { } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); ChartUtilities.writeChartAsPNG( baos , answerDistChart,width, height, info ); ChartDataCache cache = new ChartDataCache(); cache.data = baos.toByteArray(); cache.info = info; Map chartsCache = (Map)session.getAttribute(Constants.CHARTS_CACHE_KEY); String answerDistChartId = "C"+String.valueOf(System.currentTimeMillis()); chartsCache.put(answerDistChartId, cache); request.setAttribute(AdminConstants.ANSWER_COUNT_DISTRIBUTION_CHART_ID_KEY, answerDistChartId ); baos = new ByteArrayOutputStream(); info = new ChartRenderingInfo(new StandardEntityCollection()); ChartUtilities.writeChartAsPNG( baos , percentageDistChart,width, height, info ); cache = new ChartDataCache(); cache.data = baos.toByteArray(); cache.info = info; String percentageDistChartId = "P"+String.valueOf(System.currentTimeMillis()); chartsCache.put(percentageDistChartId, cache); request.setAttribute(AdminConstants.ANSWER_PERCENTAGE_DISTRIBUTION_CHART_ID_KEY, percentageDistChartId ); } } catch (Exception ex) { logger.log( Level.SEVERE, "Error getting answers distribution", ex); throw ex; } return mapping.findForward(Constants.SUCCESS); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -