1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
package graphique;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.Locale;
import java.text.NumberFormat;
import javax.servlet.http.HttpSession;
import org.jfree.data.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.renderer.BarRenderer;
import org.jfree.chart.renderer.StandardXYItemRenderer;
import org.jfree.chart.renderer.StackedXYAreaRenderer;
import org.jfree.chart.renderer.XYAreaRenderer;
import org.jfree.chart.plot.*;
import org.jfree.chart.entity.*;
import org.jfree.chart.labels.*;
import org.jfree.chart.urls.*;
import org.jfree.chart.servlet.*;
/**
*
* @author saiyuke
*
* 4 méthodes (importantes) dans cette classe :
* - public static String generateBarChart(Date hitDate, HttpSession session, PrintWriter pw)
* - public static String generatePieChart(Date hitDate, HttpSession session, PrintWriter pw)
* - public static String generateXYChart(String section, HttpSession session, PrintWriter pw)
* - public static String generateXYAreaChart(HttpSession session, PrintWriter pw)
*/
public class WebHitChart {
public static String generateBarChart(Date hitDate, HttpSession session, PrintWriter pw) {
String filename = null;
try {
// Retrieve list of WebHits
WebHitDataSet whDataSet = new WebHitDataSet();
ArrayList list = whDataSet.getDataBySection(hitDate);
// Throw a custom NoDataException if there is no data
if (list.size() == 0) {
System.out.println("No data has been found");
throw new NoDataException();
}
// Create and populate a CategoryDataset
Iterator iter = list.listIterator();
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
while (iter.hasNext()) {
WebHit wh = (WebHit)iter.next();
dataset.addValue(new Long(wh.getHitCount()), "Hits", wh.getSection());
}
// Create the chart object
CategoryAxis categoryAxis = new CategoryAxis("");
ValueAxis valueAxis = new NumberAxis("");
BarRenderer renderer = new BarRenderer();
renderer.setItemURLGenerator(new StandardCategoryURLGenerator("xy_chart.jsp","series","section"));
renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
Plot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer);
JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
chart.setBackgroundPaint(java.awt.Color.white);
// Write the chart image to the temporary directory
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);
// Write the image map to the PrintWriter
ChartUtilities.writeImageMap(pw, filename, info);
pw.flush();
} catch (NoDataException e) {
System.out.println(e.toString());
filename = "public_nodata_500x300.png";
} catch (Exception e) {
System.out.println("Exception - " + e.toString());
e.printStackTrace(System.out);
filename = "public_error_500x300.png";
}
return filename;
}
public static String generatePieChart(Date hitDate, HttpSession session, PrintWriter pw) {
String filename = null;
try {
// Retrieve list of WebHits
WebHitDataSet whDataSet = new WebHitDataSet();
ArrayList list = whDataSet.getDataBySection(hitDate);
// Throw a custom NoDataException if there is no data
if (list.size() == 0) {
System.out.println("No data has been found");
throw new NoDataException();
}
// Create and populate a PieDataSet
DefaultPieDataset data = new DefaultPieDataset();
Iterator iter = list.listIterator();
while (iter.hasNext()) {
WebHit wh = (WebHit)iter.next();
data.setValue(wh.getSection(), new Long(wh.getHitCount()));
}
// Create the chart object
PiePlot plot = new PiePlot(data);
plot.setInsets(new Insets(0, 5, 5, 5));
plot.setURLGenerator(new StandardPieURLGenerator("xy_chart.jsp","section"));
plot.setToolTipGenerator(new StandardPieItemLabelGenerator());
JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
chart.setBackgroundPaint(java.awt.Color.white);
// Write the chart image to the temporary directory
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);
// Write the image map to the PrintWriter
ChartUtilities.writeImageMap(pw, filename, info);
pw.flush();
} catch (NoDataException e) {
System.out.println(e.toString());
filename = "public_nodata_500x300.png";
} catch (Exception e) {
System.out.println("Exception - " + e.toString());
e.printStackTrace(System.out);
filename = "public_error_500x300.png";
}
return filename;
}
public static String generateXYChart(String section, HttpSession session, PrintWriter pw) {
String filename = null;
try {
// Retrieve list of WebHits
WebHitDataSet whDataSet = new WebHitDataSet();
ArrayList list = whDataSet.getDataByHitDate(section);
// Throw a custom NoDataException if there is no data
if (list.size() == 0) {
System.out.println("No data has been found");
throw new NoDataException();
}
// Create and populate an XYSeries Collection
XYSeries dataSeries = new XYSeries("Valeur");
XYSeries dataSeries2 = new XYSeries("Valeur");
Iterator iter = list.listIterator();
while (iter.hasNext()) {
WebHit wh = (WebHit)iter.next();
dataSeries.add(wh.getHitDate().getTime(),wh.getHitCount());
}
// Modification SAIYUKE
// Essai de multi-graphes
XYSeriesCollection xyDataset = new XYSeriesCollection();
xyDataset.addSeries(dataSeries) ;
// Pour rajouter des courbes sur le même graphe, il faut passer par ici!!!!!!!!
xyDataset.addSeries(dataSeries2) ;
// Create tooltip and URL generators
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.UK);
StandardXYToolTipGenerator ttg = new StandardXYToolTipGenerator(
StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
sdf, NumberFormat.getInstance());
TimeSeriesURLGenerator urlg = new TimeSeriesURLGenerator(
sdf, "pie_chart.jsp", "series", "hitDate");
// Create the chart object
DateAxis timeAxis = new DateAxis("");
NumberAxis valueAxis = new NumberAxis("");
valueAxis.setAutoRangeIncludesZero(false); // override default
StandardXYItemRenderer renderer = new StandardXYItemRenderer(
StandardXYItemRenderer.LINES + StandardXYItemRenderer.SHAPES,
ttg, urlg);
renderer.setShapesFilled(true);
XYPlot plot = new XYPlot(xyDataset, timeAxis, valueAxis, renderer);
/** SAIYUKE
* Ajout de la limite des dates */
DateAxis dateAxis = (DateAxis) plot.getDomainAxis() ;
dateAxis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 1)) ;
// 1er jour de la semaine en cours
Calendar minCalendar = new GregorianCalendar(Locale.US) ;
minCalendar.setTime(new Date());
minCalendar.set(Calendar.DAY_OF_WEEK ,minCalendar.getFirstDayOfWeek());
dateAxis.setMinimumDate(minCalendar.getTime());
// Dernier jour de la semaine en cours
minCalendar.set(Calendar.DAY_OF_WEEK ,minCalendar.getFirstDayOfWeek() + 5);
dateAxis.setMaximumDate(minCalendar.getTime());
/** Fin ajout SAIYUKE */
JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
chart.setBackgroundPaint(java.awt.Color.white);
// Write the chart image to the temporary directory
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);
// Write the image map to the PrintWriter
ChartUtilities.writeImageMap(pw, filename, info);
pw.flush();
} catch (NoDataException e) {
System.out.println(e.toString());
filename = "public_nodata_500x300.png";
} catch (Exception e) {
System.out.println("Exception - " + e.toString());
e.printStackTrace(System.out);
filename = "public_error_500x300.png";
}
return filename;
}
public static String generateXYAreaChart(HttpSession session, PrintWriter pw)
{
String filename = null;
try
{
// Récupère la liste de WebHits pour chaque section et remplit un TableXYDataset
WebHitDataSet whDataSet = new WebHitDataSet();
// Récupère la liste des sections
ArrayList sections = whDataSet.getSections();
Iterator sectionIter = sections.iterator();
DefaultTableXYDataset dataset = new DefaultTableXYDataset();
while (sectionIter.hasNext()) {
String section = (String)sectionIter.next();
ArrayList list = whDataSet.getDataByHitDate(section);
XYSeries dataSeries = new XYSeries(section, true, false);
Iterator webHitIter = list.iterator();
while (webHitIter.hasNext()) {
WebHit webHit = (WebHit)webHitIter.next();
dataSeries.add(webHit.getHitDate().getTime(), webHit.getHitCount());
}
dataset.addSeries(dataSeries);
}
// Throw a custom NoDataException if there is no data
if (dataset.getItemCount() == 0) {
System.out.println("No data has been found");
throw new NoDataException();
}
// Create tooltip and URL generators
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.UK);
StandardXYToolTipGenerator ttg = new StandardXYToolTipGenerator(
StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
sdf, NumberFormat.getInstance());
TimeSeriesURLGenerator urlg = new TimeSeriesURLGenerator(
sdf, "bar_chart.jsp", "series", "hitDate");
// Create the X-Axis
DateAxis xAxis = new DateAxis(null);
xAxis.setLowerMargin(0.0);
xAxis.setUpperMargin(0.0);
// Create the X-Axis
NumberAxis yAxis = new NumberAxis(null);
yAxis.setAutoRangeIncludesZero(true);
// Create the renderer
StackedXYAreaRenderer renderer =
new StackedXYAreaRenderer(XYAreaRenderer.AREA_AND_SHAPES, ttg, urlg);
renderer.setSeriesPaint(0, new Color(255, 255, 180));
renderer.setSeriesPaint(1, new Color(206, 230, 255));
renderer.setSeriesPaint(2, new Color(255, 230, 230));
renderer.setSeriesPaint(3, new Color(206, 255, 206));
renderer.setShapePaint(Color.gray);
renderer.setShapeStroke(new BasicStroke(0.5f));
renderer.setShape(new Ellipse2D.Double(-3, -3, 6, 6));
renderer.setOutline(true);
// Create the plot
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
plot.setForegroundAlpha(0.65f);
// Reconfigure Y-Axis so the auto-range knows that the data is stacked
yAxis.configure();
// Create the chart
JFreeChart chart = new JFreeChart(null, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
chart.setBackgroundPaint(java.awt.Color.white);
// Write the chart image to the temporary directory
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);
// Write the image map to the PrintWriter
ChartUtilities.writeImageMap(pw, filename, info);
pw.flush();
} catch (NoDataException e) {
System.out.println(e.toString());
filename = "public_nodata_500x300.png";
} catch (Exception e) {
System.out.println("Exception - " + e.toString());
e.printStackTrace(System.out);
filename = "public_error_500x300.png";
}
return filename;
}
/**
* SAIYUKE
* J'ai mis ce code mis en commaentaire car inutile vu que j'ai tout
* intégré au projet PAM (XAGA).
*/
/*
public static void main(java.lang.String[] args) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.UK);
PrintWriter pw = new PrintWriter(System.out);
String filename = WebHitChart.generateBarChart(sdf.parse("01-Aug-2002"), null, pw);
// String filename = WebHitChart.generatePieChart(sdf.parse("01-Aug-2002"), null, pw);
// String filename = WebHitChart.generateXYChart("service", null, pw);
System.out.println("filename - " + filename);
} catch (Exception e) {
System.out.println("Exception - " + e.toString());
e.printStackTrace();
}
return;
}*/
} |
Partager