Bar Chart Creation Code

From org.jfree.chart.demo.servlet.WebHitChart (See also bar_chart.jsp) 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; }
Back to the chart Back to the home page