📄 inittestresultschartaction.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.testee.report;import java.io.ByteArrayOutputStream;import javax.servlet.http.*;import org.apache.struts.action.*;import com.cyberdemia.school.*;import com.cyberdemia.cybertester.*;import com.cyberdemia.cybertester.testee.TesteeConstants;import com.cyberdemia.cybertester.common.*;import java.util.logging.*;import java.util.*;import org.jfree.chart.title.*;import org.jfree.chart.*;import org.jfree.data.*;import org.jfree.chart.renderer.*;import org.jfree.chart.axis.*;import org.jfree.chart.imagemap.*;import org.jfree.chart.entity.ChartEntity;import org.jfree.chart.entity.StandardEntityCollection;import org.jfree.chart.plot.PlotOrientation;import java.awt.Color;import java.awt.Font;/** * InitTestResultsChartAction is a Struts action to plot a chart of the user's test results. * * @version $Revision: 1.5 $ at $Date: 2004/06/22 17:06:17 $ by $Author: alexycyap $ * @author Alexander Yap, Nita Ambedkar */public class InitTestResultsChartAction extends AbstractUserAction{ protected ActionForward doPerform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception { Logger logger = CyberTesterUtils.getLogger(); Integer userId = (Integer)session.getAttribute(Constants.USER_ID_KEY); LinkedList stScoreList = new LinkedList(); try { TestManagerHome tMgrHome = CyberTesterUtils.getTestManagerHome(); TestManager testMgr = (TestManager)tMgrHome.create(); TestData[] testList = testMgr.getTestsForUser(null, userId); for (int tidx=0; tidx<testList.length; tidx++) { ScoreData scoreData = testMgr.getTestScoreForUser(testList[tidx].getId(), userId, true); if ((scoreData!=null) && (scoreData.getMaxScore()>0)) { stScoreList.add( scoreData ); } } Collections.sort(stScoreList); } catch (Exception ex) { logger.log(Level.SEVERE, "Error getting scores for user "+userId, ex); throw ex; } DefaultCategoryDataset dataset = new DefaultCategoryDataset(); Iterator stScoreIter = stScoreList.iterator(); ArrayList colorList = new ArrayList(); for (int i=1; stScoreIter.hasNext(); i++) { ScoreData sData = (ScoreData)stScoreIter.next(); double sPercent = (sData.getScore()*100.0)/sData.getMaxScore(); String dataLabel = (sData.getTestName().length()>10) ? sData.getTestName().substring(0,10)+"..." : sData.getTestName(); dataset.addValue( sPercent, userId, dataLabel ); if (sData.isPassed()) { colorList.add(Color.GREEN); } else { colorList.add(Color.RED); } } JFreeChart chart = ChartFactory.createBarChart( "", "Tests", "Score %", dataset, PlotOrientation.VERTICAL, false, true, true); chart.setTitle(new TextTitle("Test results for "+session.getAttribute(Constants.USER_LOGIN_KEY), new Font("SansSerif",Font.BOLD, 12))); /* Change for new version of JFree Chart 9.18 Change begin */ ((CategoryAxis)chart.getCategoryPlot().getDomainAxis()).setCategoryLabelPositions(new CategoryLabelPositions()); /* Change for new version of JFree Chart 9.18 Change End */ ValueAxis rangeAxis = chart.getCategoryPlot().getRangeAxis(); /* Change for new version of JFree Chart 9.18 Change begin */ rangeAxis.setUpperBound(100.0); rangeAxis.setLowerBound(0.0); /* Change for new version of JFree Chart 9.18 Change End */ BarRenderer renderer = new ResultsBarRenderer( (Color[])colorList.toArray(new Color[0])); chart.getCategoryPlot().setRenderer(renderer); renderer.setLabelGenerator( new TestResultsToolTipGenerator(stScoreList) ); renderer.setItemURLGenerator( new TestResultsURLGenerator(stScoreList) ); chart.setAntiAlias(true); int width = Math.max(400, 100+(dataset.getColumnCount()*30)); try { width = Integer.parseInt( request.getParameter("width") ); } catch (NumberFormatException nfex) { // Ignore, width unchanged. } int height = 600; try { height = Integer.parseInt( request.getParameter("height") ); } catch (NumberFormatException nfex) { // Ignore, height unchanged. } final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); ChartUtilities.writeChartAsPNG( baos , chart,width, height, info ); ChartDataCache cache = new ChartDataCache(); cache.data = baos.toByteArray(); cache.info = info; Map chartsCache = (Map)session.getAttribute(Constants.CHARTS_CACHE_KEY); String chartId = String.valueOf(System.currentTimeMillis()); chartsCache.put(chartId, cache); request.setAttribute(TesteeConstants.RESULTS_CHART_INFO_KEY, CyberTesterUtils.getImageMapAreaList(cache.info) ); request.setAttribute(TesteeConstants.RESULTS_CHART_ID_KEY, chartId); return mapping.findForward(Constants.SUCCESS); } class ResultsBarRenderer extends BarRenderer { public ResultsBarRenderer(Color[] barColors) { super(); m_barColors = barColors;// setPaintTableActive(true); } public java.awt.Paint getItemPaint(int row, int column) { if (column<m_barColors.length) { return m_barColors[column]; } return super.getItemPaint(row,column); } private Color[] m_barColors; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -