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.axis.DateAxis;
037: import org.jfree.chart.axis.NumberAxis;
038: import org.jfree.chart.axis.ValueAxis;
039: import org.jfree.chart.labels.StandardXYItemLabelGenerator;
040: import org.jfree.chart.plot.XYPlot;
041: import org.jfree.chart.renderer.StandardXYItemRenderer;
042: import org.jfree.chart.urls.TimeSeriesURLGenerator;
043:
044: import org.jfree.data.XYSeries;
045: import org.jfree.data.XYSeriesCollection;
046:
047: import org.libresource.Libresource;
048:
049: import org.libresource.core.CoreConstants;
050: import org.libresource.core.interfaces.LibresourceStatsService;
051:
052: import org.libresource.kernel.KernelConstants;
053: import org.libresource.kernel.interfaces.KernelService;
054:
055: import java.awt.Color;
056:
057: import java.net.URI;
058:
059: import java.text.DecimalFormat;
060: import java.text.SimpleDateFormat;
061:
062: import java.util.Calendar;
063: import java.util.Hashtable;
064: import java.util.Iterator;
065: import java.util.Vector;
066:
067: public class DrawTimeChartHelper {
068: public static final String STATS = "statsHistory";
069: public static final String MOST_ACTIVE_USERS = "mostActiveUsersHistory";
070: public static final String MOST_ACTIVE_RESOURCES = "mostActiveResourcesHistory";
071: public static final String PREFERED_RESOURCES = "preferedResourcesHistory";
072:
073: public static JFreeChart drawChart(URI project, String data)
074: throws Exception {
075: LibresourceStatsService libresourceStatsService = (LibresourceStatsService) Libresource
076: .getService(CoreConstants.SERVICE_STAT);
077: KernelService kernelService = (KernelService) Libresource
078: .getService(KernelConstants.SERVICE);
079:
080: XYSeriesCollection xyDataset = new XYSeriesCollection();
081:
082: if (data.equals(STATS)) {
083: Object[] stats = new Object[0];
084:
085: try {
086: stats = libresourceStatsService
087: .getProjectStatsHistory(project);
088: } catch (Exception e) {
089: }
090:
091: XYSeries activity = new XYSeries("Activity");
092: XYSeries popularity = new XYSeries("Popularity");
093:
094: for (int i = 0; i < stats.length; i++) {
095: Object[] stat = (Object[]) stats[i];
096:
097: if (stat != null) {
098: activity.add(week2time(i), ((Float) stat[0])
099: .doubleValue());
100: popularity.add(week2time(i), ((Float) stat[1])
101: .doubleValue());
102: }
103: }
104:
105: xyDataset.addSeries(popularity);
106: xyDataset.addSeries(activity);
107: } else if (data.equals(MOST_ACTIVE_USERS)) {
108: Object[] users = new Object[0];
109:
110: try {
111: users = libresourceStatsService
112: .getProjectMostActiveUsersHistory(project);
113: } catch (Exception e) {
114: //
115: }
116:
117: buildSeries(xyDataset, users);
118: } else if (data.equals(MOST_ACTIVE_RESOURCES)) {
119: Object[] resources = new Object[0];
120:
121: try {
122: resources = libresourceStatsService
123: .getProjectMostActiveResourcesHistory(project);
124: } catch (Exception e) {
125: }
126:
127: buildSeries(xyDataset, resources);
128: } else if (data.equals(PREFERED_RESOURCES)) {
129: Object[] resources = new Object[0];
130:
131: try {
132: resources = libresourceStatsService
133: .getProjectPreferedResourcesHistory(project);
134: } catch (Exception e) {
135: }
136:
137: buildSeries(xyDataset, resources);
138: }
139:
140: // Create tooltip and URL generators
141: SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
142: StandardXYItemLabelGenerator ttg = new StandardXYItemLabelGenerator(
143: sdf, new DecimalFormat("#0'%'"));
144: TimeSeriesURLGenerator urlg = new TimeSeriesURLGenerator(sdf,
145: "#", "series", "hitDate");
146:
147: ValueAxis timeAxis = new DateAxis("");
148: NumberAxis valueAxis = new NumberAxis("");
149:
150: //timeAxis.setRange(week2time(1), week2time(52));
151: //valueAxis.setRange(0, 100);
152: valueAxis.setAutoRange(true);
153: timeAxis.setAutoRange(true);
154: valueAxis.setNumberFormatOverride(new DecimalFormat("#0'%'"));
155:
156: StandardXYItemRenderer renderer = new StandardXYItemRenderer(
157: StandardXYItemRenderer.LINES
158: + StandardXYItemRenderer.SHAPES, ttg, urlg);
159: renderer.setShapesFilled(true);
160:
161: XYPlot plot = new XYPlot(xyDataset, timeAxis, valueAxis,
162: renderer);
163: plot.setOutlinePaint(Color.WHITE);
164:
165: JFreeChart chart = new JFreeChart("",
166: JFreeChart.DEFAULT_TITLE_FONT, plot, true);
167: chart.setBackgroundPaint(java.awt.Color.white);
168:
169: return chart;
170: }
171:
172: private static long week2time(int week) {
173: Calendar cal = Calendar.getInstance();
174: cal.set(Calendar.WEEK_OF_YEAR, week);
175:
176: return cal.getTime().getTime();
177: }
178:
179: private static void buildSeries(XYSeriesCollection xyDataset,
180: Object[] objects) {
181: Hashtable userNames = new Hashtable();
182: Hashtable series = new Hashtable();
183:
184: for (int i = 1; i <= objects.length; i++) {
185: Vector topUsers = (Vector) objects[i - 1];
186:
187: if (topUsers.size() > 0) {
188: for (Iterator it = topUsers.iterator(); it.hasNext();) {
189: Object[] user = (Object[]) it.next();
190: String uri = (String) user[0];
191: float score = ((Float) user[1]).floatValue();
192:
193: if (userNames.get(uri) == null) {
194: try {
195: userNames.put(uri, Libresource
196: .getShortName(new URI(uri)));
197: } catch (Exception e) {
198: // lazy
199: userNames.put(uri, uri);
200: }
201: }
202:
203: String userName = (String) userNames.get(uri);
204:
205: if (series.get(uri) == null) {
206: XYSeries mySerie = new XYSeries(userName);
207: xyDataset.addSeries(mySerie);
208: series.put(uri, mySerie);
209: }
210:
211: XYSeries theSerie = (XYSeries) series.get(uri);
212: theSerie.add(week2time(i), score);
213: }
214: }
215: }
216: }
217: }
|