📄 gettestanswersdistributionaction.java
字号:
/* * CyberTester - J2EE Web application for creating, delivering and managing tests/exams/surveys. * Copyright (C) 2004 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.chart.title.*;import org.jfree.data.*;import org.jfree.chart.labels.*;import org.jfree.chart.axis.*;import org.jfree.chart.renderer.*;import org.jfree.chart.imagemap.*;import org.jfree.chart.entity.ChartEntity;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. * * @version $Revision: 1.11 $ at $Date: 2004/06/22 17:06:05 $ by $Author: alexycyap $ * @author Alexander Yap, Nita Ambedkar */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(); // If test ID sent by the form is 0, treat it as null. Integer rawTestId = CyberTesterUtils.getIntegerParameterOrRequestAttribute(request, AdminConstants.TEST_ID_KEY); Integer testId = null; if ((rawTestId != null) && (rawTestId.intValue() > 0) ) { testId = rawTestId; } 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, ReportGeneratorBean.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))); /* Change for new version of JFree Chart 9.18 Change begin */ ((CategoryAxis)answerDistChart.getCategoryPlot().getDomainAxis()).setCategoryLabelPositions(new CategoryLabelPositions()); /* Change for new version of JFree Chart 9.18 Change End */ ValueAxis rangeAxis = answerDistChart.getCategoryPlot().getRangeAxis(); /* Change for new version of JFree Chart 9.18 Change begin */ rangeAxis.setUpperBound(maxAnswerCount); rangeAxis.setLowerBound(0.0); /* Change for new version of JFree Chart 9.18 Change begin */ CategoryItemRenderer renderer = answerDistChart.getCategoryPlot().getRenderer(); renderer.setLabelGenerator( new CategoryLabelGenerator() { public String generateLabel(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(); } } ); /* Change for new version of JFree Chart 9.18 Change End */ 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))); /* Change for new version of JFree Chart 9.18 Change begin */ ((CategoryAxis)percentageDistChart.getCategoryPlot().getDomainAxis()).setCategoryLabelPositions(new CategoryLabelPositions()); /* Change for new version of JFree Chart 9.18 Change End */ rangeAxis = percentageDistChart.getCategoryPlot().getRangeAxis(); /* Change for new version of JFree Chart 9.18 Change begin */ rangeAxis.setUpperBound(100.0); rangeAxis.setLowerBound(0.0); renderer = percentageDistChart.getCategoryPlot().getRenderer(); renderer.setLabelGenerator( new CategoryLabelGenerator() { public String generateLabel(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(); } } ); /* Change for new version of JFree Chart 9.18 Change End */ percentageDistChart.setAntiAlias(true); int width = Math.max(300, 100+(answerDataset.getColumnCount()*20)); try { width = Integer.parseInt( request.getParameter("width") ); } catch (NumberFormatException nfex) { // Ignore, width unchanged. } int height = 400; try { height = Integer.parseInt( request.getParameter("height") ); } catch (NumberFormatException nfex) { // Ignore, height unchanged. } 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); /* Change for new version of JFree Chart 9.20 to retrieve the ImageMapAreaTag Begin*/ request.setAttribute(AdminConstants.ANSWER_COUNT_DISTRIBUTION_CHART_INFO_KEY, CyberTesterUtils.getImageMapAreaList(cache.info) ); /* Change for new version of JFree Chart 9.20 to retrieve the ImageMapAreaTag End */ 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_INFO_KEY, CyberTesterUtils.getImageMapAreaList(cache.info) ); 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 + -