Wednesday, April 8, 2009 0 comments

JFreeChart For The Web Cont.....

Ok my last post showed you guys as to how to display JFreeCharts in your web application. One issue with that code that i later realized that was it does not support concurrency due to the fact that it creates a temporary file with the same name and if two pepole requests for the same chart at the same time there might be unpredictable outputs and hence i went throught their API again and found a way to resolve this issue. Use the following code if you need to support concurrency when genearating reports and this method anyway is much better performance vice as it does not include any I/O operations which is always an overhead to the application.

JFreeChart chart = ChartFactory.createBarChart(chartTitle,

xAxisName, yAxisName, dataSet, PlotOrientation.VERTICAL,

false, true, false);


OutputStream outStream = response.getOutputStream();

ByteArrayOutputStream byteArray = new ByteArrayOutputStream();

ChartUtilities.writeChartAsPNG(byteArray, chart, 800, 500);

byte[] byteStream = null;

byteStream = byteArray.toByteArray();


response.setContentLength((int) byteStream.length);



"no-store, no-cache, must-revalidate, post-check=0, pre-check=0");

response.setHeader("Pragma", "no-cache");