Code: Select all
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);
[b] // Write the image map to the PrintWriter
ChartUtilities.writeImageMap(pw, filename, info);
pw.flush();[/b]
} 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;
}