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.core;
034:
035: import org.jfree.chart.JFreeChart;
036: import org.jfree.chart.labels.StandardPieItemLabelGenerator;
037: import org.jfree.chart.plot.PiePlot;
038: import org.jfree.chart.urls.PieURLGenerator;
039:
040: import org.jfree.data.DefaultPieDataset;
041: import org.jfree.data.PieDataset;
042:
043: import org.libresource.Libresource;
044:
045: import org.libresource.core.CoreConstants;
046: import org.libresource.core.interfaces.LibresourceStatsService;
047:
048: import org.libresource.kernel.KernelConstants;
049: import org.libresource.kernel.interfaces.KernelService;
050:
051: import java.awt.Color;
052: import java.awt.Insets;
053: import java.awt.Paint;
054:
055: import java.net.URI;
056:
057: import java.text.NumberFormat;
058:
059: import java.util.Hashtable;
060:
061: public class DrawPieChartHelper {
062: public static final String MOST_ACTIVE_USERS = "mostActiveUsers";
063: public static final String MOST_ACTIVE_RESOURCES = "mostActiveResources";
064: public static final String PREFERED_RESOURCES = "preferedResources";
065: public static final Paint[] paint = new Paint[] {
066: Color.decode("#c5ceef"), Color.decode("#ffffc5"),
067: Color.decode("#3a63a5"), Color.decode("#dedede"),
068: Color.decode("#9c8cb5") };
069:
070: public static JFreeChart drawChart(URI project, String data)
071: throws Exception {
072: LibresourceStatsService libresourceStatsService = (LibresourceStatsService) Libresource
073: .getService(CoreConstants.SERVICE_STAT);
074: KernelService kernelService = (KernelService) Libresource
075: .getService(KernelConstants.SERVICE);
076:
077: Object[] objects = new Object[0];
078: URI mainResource = null;
079: Hashtable urls = new Hashtable();
080:
081: if (data.equals("mostActiveUsers")) {
082: try {
083: objects = libresourceStatsService
084: .getProjectMostActiveUsers(project);
085: } catch (Exception e) {
086: }
087:
088: mainResource = kernelService.getOwner(project);
089: } else if (data.equals("mostActiveResources")) {
090: try {
091: objects = libresourceStatsService
092: .getProjectMostActiveResources(project);
093: } catch (Exception e) {
094: }
095:
096: mainResource = project;
097: } else if (data.equals("preferedResources")) {
098: try {
099: objects = libresourceStatsService
100: .getProjectPreferedResources(project);
101: } catch (Exception e) {
102: }
103:
104: mainResource = project;
105: }
106:
107: DefaultPieDataset dataset = new DefaultPieDataset();
108: int main = -1;
109:
110: for (int i = 0; i < objects.length; i++) {
111: Object[] obj = (Object[]) objects[i];
112: String shortName = obj[0].toString();
113:
114: try {
115: shortName = Libresource.getShortName(new URI(obj[0]
116: .toString()));
117: } catch (Exception e) {
118: //
119: }
120:
121: urls.put(shortName,
122: (obj[0].toString().length() > 0) ? obj[0]
123: .toString().substring(1) : "");
124: dataset.setValue(shortName, (Float) obj[1]);
125:
126: if (mainResource.getPath().equals(obj[0].toString())) {
127: main = i;
128: }
129: }
130:
131: PiePlot plot = new PiePlot(dataset);
132: plot.setLabelBackgroundPaint(Color.decode("#fafafa"));
133: plot.setSectionOutlinePaint(Color.BLACK);
134: plot.setOutlinePaint(Color.WHITE);
135: plot.setSectionPaint(0, paint[0]);
136: plot.setSectionPaint(1, paint[1]);
137: plot.setSectionPaint(2, paint[2]);
138: plot.setSectionPaint(3, paint[3]);
139: plot.setSectionPaint(4, paint[4]);
140: plot.setInsets(new Insets(0, 5, 5, 5));
141: plot.setCircular(false);
142: plot.setLabelGenerator(new StandardPieItemLabelGenerator(
143: "{0} ({2})", NumberFormat.getNumberInstance(),
144: NumberFormat.getPercentInstance()));
145: plot.setURLGenerator(new MyPieURLGenerator(urls));
146:
147: if (main != -1) {
148: plot.setExplodePercent(main, 0.2);
149: }
150:
151: JFreeChart chart = new JFreeChart("",
152: JFreeChart.DEFAULT_TITLE_FONT, plot, true);
153: chart.setBackgroundPaint(java.awt.Color.white);
154:
155: return chart;
156: }
157: }
158:
159: class MyPieURLGenerator implements PieURLGenerator {
160: private Hashtable urls;
161:
162: public MyPieURLGenerator(Hashtable urls) {
163: this .urls = urls;
164: }
165:
166: public String generateURL(PieDataset a, Comparable b, int c) {
167: return (String) urls.get(b);
168: }
169: }
|