How do I return a dynamically generated pie chart from a servlet?
Author: Deron Eriksson
Description: This tutorial describes how the JFreeChart library can be used to dynamically generate a pie chart that can then be returned via a servlet's output stream.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0) || Tomcat 5.5.20


The JFreeChart library is a great JavaSW tool for creating a wide variety of good looking charts. In this short tutorial, we'll look at how we can use JFreeChart to generate a pie chart that can be returned as an image from a servletW.

Our demonstration project consists of a single servlet named ChartServlet. The project references the jcommon-1.0.12.jar and the jfreechart-1.0.9.jar libraries. The jcommon jarW file contains some classes that you might find useful when working with jfreechart. The demonstration project is shown here in Eclipse:

project structure

Our deployment descriptor maps /chart to ChartServlet.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="tomcat-demo" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<servlet>
		<servlet-name>ChartServlet</servlet-name>
		<servlet-class>chart.ChartServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>ChartServlet</servlet-name>
		<url-pattern>/chart</url-pattern>
	</servlet-mapping>

</web-app>

The ChartServlet creates a JFreeChart object in its getChart() method. First we populate a DefaultPieDataset object with some values. Then, we call one of the ChartFactory.createPieChart() methods with the dataset and some other values and get back a JFreeChart object. For fun, I added a 5 pixel green border around the chart.

In doGet(), we set the response content type to be "image/png". The servlet takes the chart object and writes it to the output stream of the response using one of the ChartUtilities.writeChartAsPNG() methods. This method also allows us to set the width and height of the chart.

ChartServlet.java

package chart;

import java.awt.BasicStroke;
import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

public class ChartServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

		response.setContentType("image/png");

		OutputStream outputStream = response.getOutputStream();

		JFreeChart chart = getChart();
		int width = 500;
		int height = 350;
		ChartUtilities.writeChartAsPNG(outputStream, chart, width, height);

	}

	public JFreeChart getChart() {
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("Ford", 23.3);
		dataset.setValue("Chevy", 32.4);
		dataset.setValue("Yugo", 44.2);

		boolean legend = true;
		boolean tooltips = false;
		boolean urls = false;

		JFreeChart chart = ChartFactory.createPieChart("Cars", dataset, legend, tooltips, urls);

		chart.setBorderPaint(Color.GREEN);
		chart.setBorderStroke(new BasicStroke(5.0f));
		chart.setBorderVisible(true);

		return chart;
	}

}

If we fire up our web application and hit the chart servlet in a browser, we can see that our chart has been generated and has been successfully returned from the servlet.

Pie chart returned from servlet

As you can see, generating a chart and returning it on a servlet output stream is very easy with JFreeChart. Another technique that you can use is to write the chart to a temporary file. JFreeChart comes with a servlet that can do this, and it also has a cleanup servlet for cleaning up old files.