Pages Navigation Menu

Coding is much easier than you think

Struts2-Jfreechart integration

Struts2-Jfreechart integration

In this article we will learn to integrate Struts 2 with JFreeChart using struts2-jfreechart-plugin-x.x.x.jar for creating a pie chart in web application using JFreeChart library.
The JFreeChart is easiest and the most widely used library for creating a wide variety of good looking charts.

The Struts-JFreeChart plugin allows Struts 2 Actions to easily return generated charts and graphs.

Instead of streaming a generated chart directly to the HTTP response, this plugin provides a ChartResult, which handles the generation for you. This allows you to generate the chart in one class, and render it out in another class, effectively decoupling the view from the Actions.
** UPDATE: Struts 2 Complete tutorial now available here.

Libraries required :

Jfreechart library can be downloaded here.
Now Create a New Dynamic Web Project in eclipse with following folder structure
Struts2-Jfreechart Folder structure

Struts 2 Action

File :

package com.simplecodestuffs.action;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;

import com.opensymphony.xwork2.Action;

public class JfreeChartAction implements Action {

     private JFreeChart chart;

     public String execute() throws Exception {

	DefaultPieDataset dataSet = new DefaultPieDataset();
	dataSet.setValue("Agriculture", 10);
	dataSet.setValue("Residential heating", 4);
	dataSet.setValue("Commercial products", 15);
	dataSet.setValue("Industry", 42);
	dataSet.setValue("Transportation", 26);
	dataSet.setValue("Others", 3);

	chart = ChartFactory.createPieChart(
		"Source of Air Pollution ", // Title
		dataSet,                    // Data
		true,                       // Display the legend
		true,                       // Display tool tips
		false                       // No URLs


	return SUCCESS;

     // This method will get called if we specify <param name="value">chart</param>
     public JFreeChart getChart() {
	return chart;


Recommended reading:

Here Jfreechart provides DefaultPieDataset method to set static data and createPieChart() method creates the pie chart using the dataset.


To use the plugin, have your Struts configuration package extend the jfreechart-default package, which provides the chart result type.

<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   <!-- package should extend jfreechart-default package -->
   <package name="default" namespace="/" extends="jfreechart-default">
       <action name="displayChart" class="com.simplecodestuffs.action.JfreeChartAction">
            <result name="success" type="chart">
		<param name="value">chart</param>
		<param name="type">jpeg</param>
		<param name="width">600</param>
		<param name="height">400</param>


Jsp page


<title>Struts2-Jfreechart integration</title>
     <h3>Struts2-Jfreechart integration</h3>
     <br />
     <img src="displayChart"/>

In Jsp, servlet is called via img tag’s src attribute. This triggers the Action and render the image on the page.
** UPDATE: To Create Auto-Refreshing Pie Chart dynamically the following tutorial will help here.


Now right click on the project name and select Run As –> Run on Server.
Struts2-Jfreechart integration

About Mohaideen Jamil

Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

  • Geraldine Kyuketsuki

    Thanks Mohaideen Jamil.
    your explainations help me alot. I’m having with this Jfreechart for along time :)

    • Mohaideen Jamil

      Thankyou for the feedback!

  • Manivannan E

    Hi Mohaideen,

    Very nice example. I am trying to use ajax for passing object list to this chart for making this chart dynamic. Any example will be very useful for me. Thank you.