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 : JfreeChartAction.java

package com.simplecodestuffs.action;

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

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
		);

	chart.setBorderVisible(true);

	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.
 

struts.xml

 
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" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
   <!-- 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>
	    </result>
	</action>
   </package>
</struts>

 

Jsp page

 

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

 
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.
 

Demo

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

About Mohaideen Jamil