Creating a Pie Chart using JFreeChart

A collegue of mine was working on a project requiring a lot of charting displays in Java EE applications. He requested my inputs around how he should go about getting this requirement done. I did a quick google search and found a number of viable options.

Option I] JFreeChart
JFreeChart can be integrated with Java EE applications using another open source project called cewolf. I also came across a useful tutorial for using cewolf and JFreeChart. The nicest thing about the cewolf distribution is that it comes built-in with JFreeChart distribution. No two open source project integration horror stories here.

Option II] A lot of open source projects

Option III] Big Faceless Java Graph Library

A quick evaluation and I decided to go for JFreeChart. Reason 1: It appeared to be a very popular open source project. Reason II: Option III was not open source and came with a price tag. In these days of open source, not a very nice thing. :-((

The reason I need to write this tutorial is because I felt the tutorial mentioned above though useful is not complete. This blog is my attempt at getting the missing points in.


SysDeo Tomcat Plugin
There some plugins available for HTML/JSP editing like Eclipse HTML Editor plugin
If you are through with the initial setup, here’s how we proceed. Create a new Tomcat project “JavaCharting”.

Tomcat settings are as follows:
ContextName: JavaCharting
subDirectory set as application root: webApplication

The Eclipe folder structure should be something like this:

   |---------- src
   |---------- webApplication
		|--------- charting
		|--------- etc
		|          |
		|          |---- overlib.js
		|--------- WEB-INF

The overlib.js should be available from cewolf distribution. Add cewolf.tld from the distribution in the WEB-INF folder and also add web.xml if it is not available.

The structure of the web.xml is as below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "">
<web-app id="WebApp">
		<!-- sets storage implementation -->
		<!-- sets overlib.js location relative to webapp -->


No special change is needed in the cewolf.tld.

For our tutorial purpose let us take up the challenge of developing a pie chart. The first step for charting is to provide a container holding the pie chart data. In JFreeChart the container is developed using Java class com.charting.PieChartDS

Below is the sample code contained in PieChartDS

package com.charting;

import java.util.Map;


import de.laures.cewolf.DatasetProducer;

public class PieChartDS implements DatasetProducer, Serializable {

    public PieChartDS() {

    public Object produceDataset(Map params) {
        DefaultPieDataset ds = null;

        ds = new DefaultPieDataset();
        for (int i=0; i&lt;=10; i++) {
            ds.setValue("DEPT-"+i,new Integer(i));
        return ds;

    public String getProducerId() {
        return "Sample Test";

    public boolean hasExpired(Map params, java.util.Date since) {
        return false;


The container class in JFreeChart needs to implement the DatasetProducer and Serializable interfaces. Additionally the class needs to provide concrete implementations for the methods:


In addition for tooltips on the Pie Chart create a new Class named CustomPieToolTipGenerator. The sample code for the class is as below:

package com.charting;


public class CustomPieToolTipGenerator implements
	de.laures.cewolf.tooltips.PieToolTipGenerator { 

    public String generateToolTip(PieDataset dataset,
	Comparable section, int index) {
      return String.valueOf(dataset.getValue(index));


Now let’s move on to display in the JSP. Add the following index.jsp in the charting folder.

<%@page contentType="text/html"%>
<%@taglib uri='/WEB-INF/cewolf.tld' prefix='cewolf' %>
<H1>Pie Chart view</H1>
<jsp:useBean id="pieData" class="com.charting.PieChartDS"/>
<jsp:useBean id="pieToolTip"
<% pageContext.setAttribute("pieChartViewToolTips",
	pieToolTip); %>
<cewolf:chart id="pieChart" title="Pie" type="pie">
        <cewolf:point x="0" y="0" color="#FFFFFF" />
        <cewolf:point x="300" y="0" color="#DDDDFF" />
        <cewolf:producer id="pieData" />
<cewolf:img chartid="pieChart" renderer="/cewolf"
	width="300" height="300">
    <cewolf:map tooltipgeneratorid="pieChartViewToolTips"/>


Note the cewolf specific changes done at lines 2, 7, 8, 9, 10-21.

Line 2 we have added the cewolf specific tag library. Line 7 and 8 we have added the Pie Chart container classs and the tool tip generator. Line 9 sets the tool tip generator in the page context for future references within the JSP. Line 10-18 provides the definition of the pie chart. Line 19: renders the Pie Chart and line 20 helps add the tool tips on the rendered graph. Run the http://localhost/JavaCharting/charting/index.jsp page in the browser and presto the Pie Chart is rendered.


4 thoughts on “Creating a Pie Chart using JFreeChart

  1. Very good article.
    The PieChartDS java class had the method produceDataset, how to set this method even before this method is been pulled from the jsp?

    Basically i have a class which sets the bean’s values, i then create an object of PieChartDS and added a constructor where i set the bean into the PieChartDS. Then the method produceDataset culls the value from the bean and sets the DefaultPieDataset Object.

    When using in jsp it throws a null pointer exception.

  2. I have not done it myself. My recommendation is use cewolf:param to pass the relevant param information and add appropriate processing logic in the produceDataset method implementation.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s