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.labels.StandardPieItemLabelGenerator;
038: import org.jfree.chart.plot.PiePlot3D;
039:
040: import org.jfree.data.DefaultPieDataset;
041:
042: import org.libresource.Libresource;
043:
044: import org.libresource.survey.SurveyConstants;
045: import org.libresource.survey.ejb.model.SurveyResourceValue;
046: import org.libresource.survey.interfaces.LibresourceSurveyService;
047:
048: import java.awt.Color;
049: import java.awt.Insets;
050: import java.awt.Paint;
051:
052: import java.net.URI;
053:
054: import java.text.NumberFormat;
055:
056: import java.util.Iterator;
057: import java.util.Vector;
058:
059: public class DrawPieChartHelper {
060: public static final String SURVEY_RESULTS = "surveyPieChartResults";
061: public static final Paint[] paint = new Paint[] {
062: Color.decode("#c5ceef"), Color.decode("#ffffc5"),
063: Color.decode("#3a63a5"), Color.decode("#dedede"),
064: Color.decode("#9c8cb5") };
065:
066: public static JFreeChart drawChart(URI surveyUri, String data)
067: throws Exception {
068: LibresourceSurveyService surveyService = (LibresourceSurveyService) Libresource
069: .getService(SurveyConstants.SERVICE);
070: SurveyResourceValue survey = surveyService.getSurvey(surveyUri);
071: int nbVoters = survey.getVoters().length;
072: Vector results = surveyService.getResults(surveyUri, nbVoters);
073:
074: DefaultPieDataset dataset = new DefaultPieDataset();
075:
076: for (Iterator i = results.iterator(); i.hasNext();) {
077: Object[] obj = (Object[]) i.next();
078: dataset.setValue((String) obj[1], (Integer) obj[3]);
079: }
080:
081: JFreeChart chart = ChartFactory.createPieChart3D(survey
082: .getQuestion(), dataset, true, false, false);
083: PiePlot3D plot = (PiePlot3D) chart.getPlot();
084: plot.setLabelBackgroundPaint(Color.decode("#fafafa"));
085: plot.setSectionOutlinePaint(Color.BLACK);
086: plot.setOutlinePaint(Color.WHITE);
087:
088: int j = 0;
089:
090: for (int i = 0; i < results.size(); i++) {
091: if (i == 5) {
092: j = 0;
093: }
094:
095: plot.setSectionPaint(i, paint[j]);
096: j++;
097: }
098:
099: plot.setInsets(new Insets(0, 5, 5, 5));
100: plot.setCircular(false);
101: plot.setLabelGenerator(new StandardPieItemLabelGenerator(
102: "{0} ({2})", NumberFormat.getNumberInstance(),
103: NumberFormat.getPercentInstance()));
104: chart.setBackgroundPaint(java.awt.Color.white);
105:
106: return chart;
107: }
108: }
|