Jasper Reports - Gantt Chart

Posted on June 12, 2009

Jasper Reports framework uses JFreeChart, to dynamically generate charts in a Java application. In this post, I'm gonna use the Java beans as data holders and pass a collection of these beans, to the Jasper reporting engine, to dynamically generate these reports. Below is the code for the Java bean:

package com.chetty.reporting.beans;

import java.util.Date;

public class GanttChartBean {
private String series;
private String task;
private Date startDate;
private Date endDate;

public void setSeries(String series) {
this.series = series;

public String getSeries() {
return series;

public void setTask(String task) {
this.task = task;

public String getTask() {
return task;

public void setStartDate(Date startDate) {
this.startDate = startDate;

public Date getStartDate() {
return startDate;

public void setEndDate(Date endDate) {
this.endDate = endDate;

public Date getEndDate() {
return endDate;

And below is the code for the Java class, that holds the business logic for creating these beans:

package com.chetty.reporting.business;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import com.chetty.reporting.beans.GanttChartBean;

public class GanttChartBeanMaker {
public ArrayList getGanttChartData() {
ArrayList ganttChartDataList = new ArrayList();

Calendar calendar = Calendar.getInstance();

for(int i=1;i<8;i++) {
calendar.set(Calendar.HOUR_OF_DAY, i*1);

Date startDate = calendar.getTime();

calendar.set(Calendar.HOUR_OF_DAY, i*3);

Date endDate = calendar.getTime();

String series = (i % 2 == 0) ? "High Priority" : "Normal";

ganttChartDataList.add(create(series, "Meeting" + i, startDate, endDate));

return ganttChartDataList;

private GanttChartBean create(String series, String task, Date startDate, Date endDate) {
GanttChartBean ganttChartBean = new GanttChartBean();


return ganttChartBean;
And below is the main class, which passes the collection of beans, to the Jasper reporting engine, to dynamically generate the Gantt Chart:
package com.chetty.reporting.engine;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.chetty.reporting.beans.GanttChartBean;
import com.chetty.reporting.beans.PieChartBean;
import com.chetty.reporting.business.GanttChartBeanMaker;
import com.chetty.reporting.business.PieChartBeanMaker;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

public class Reporter {
public static void main(String[] args) throws Exception {
InputStream inputStream = new FileInputStream ("reports/ganttchart.jrxml");

GanttChartBeanMaker ganttChartBeanMaker = new GanttChartBeanMaker();
ArrayList ganttChartBeanList = ganttChartBeanMaker.getGanttChartData();

JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(ganttChartBeanList);

Map parameters = new HashMap();

JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint, "c:/reports/ganttchart.pdf");
When you run the code, the following report is generated: Gantt Chart The above report uses a different theme, which is not present in the jasperreports.jar file. To use that theme, you would have to include the 'jasperreports-chart-themes.jar' file. You can find the report design file, for the above report, here: ganttchart.jrxml

Blog Categories
The views expressed on this blog are my personal views and do not reflect the views of my employer or campaigns I am supporting.

All sample code is provided for illustrative purposes only. These examples have not been thoroughly tested under all conditions. The writer therefore, cannot guarantee or imply reliability, serviceability, or function of these programs.

All programs contained herein are provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.