001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web.controllers.survey;
034:
035: import org.jfree.chart.ChartFactory;
036: import org.jfree.chart.JFreeChart;
037: import org.jfree.chart.plot.CategoryPlot;
038: import org.jfree.chart.plot.PlotOrientation;
039:
040: import org.jfree.data.CategoryDataset;
041: import org.jfree.data.DatasetUtilities;
042:
043: import org.libresource.Libresource;
044:
045: import org.libresource.survey.SurveyConstants;
046: import org.libresource.survey.ejb.model.SurveyResourceValue;
047: import org.libresource.survey.interfaces.LibresourceSurveyService;
048:
049: import java.awt.Color;
050: import java.awt.Insets;
051: import java.awt.Paint;
052:
053: import java.net.URI;
054:
055: import java.util.Iterator;
056: import java.util.Vector;
057:
058: public class DrawHistogramHelper {
059: public static final String SURVEY_RESULTS = "surveyHistogramResults";
060: public static final Paint[] paint = new Paint[] {
061: Color.decode("#c5ceef"), Color.decode("#ffffc5"),
062: Color.decode("#3a63a5"), Color.decode("#dedede"),
063: Color.decode("#9c8cb5") };
064:
065: public static JFreeChart drawChart(URI surveyUri, String data)
066: throws Exception {
067: LibresourceSurveyService surveyService = (LibresourceSurveyService) Libresource
068: .getService(SurveyConstants.SERVICE);
069: SurveyResourceValue survey = surveyService.getSurvey(surveyUri);
070: int nbVoters = survey.getVoters().length;
071: Vector results = surveyService.getResults(surveyUri, nbVoters);
072:
073: double[][] tab = new double[results.size()][1];
074: String[] choices = new String[results.size()];
075: int x = 0;
076:
077: for (Iterator i = results.iterator(); i.hasNext();) {
078: Object[] obj = (Object[]) i.next();
079: tab[x][0] = ((Integer) obj[3]).doubleValue();
080: choices[x] = obj[1].toString();
081: x++;
082: }
083:
084: CategoryDataset dataset = DatasetUtilities
085: .createCategoryDataset(choices, new String[] { "" },
086: tab);
087:
088: JFreeChart chart = ChartFactory.createBarChart3D(survey
089: .getQuestion(), null, "Votes", dataset,
090: PlotOrientation.VERTICAL, true, false, false);
091: CategoryPlot plot = chart.getCategoryPlot();
092: int j = 0;
093:
094: for (int i = 0; i < x; i++) {
095: if (i == 5) {
096: j = 0;
097: }
098:
099: plot.getRenderer().setSeriesPaint(i, paint[j]);
100: j++;
101: }
102:
103: plot.setOutlinePaint(Color.WHITE);
104: plot.setInsets(new Insets(0, 5, 5, 5));
105: chart.setBackgroundPaint(java.awt.Color.white);
106:
107: return chart;
108: }
109: }
|