JFreeChart - Gantt chart example



JFreeChart - Gantt chart example

A Gantt chart is a type of bar chart that is commonly used for planning and scheduling project tasks and events.

In this post, I will show you how to create a Gantt chart in a java program using the JFreeChart 1.0.19 library.

 

JAR dependencies

Add the following jars to build path of your project.

  • jfreechart-1.0.19.jar
  • jcommon-1.0.23.jar

 

Sample data 

Consider the following sample data for a Gantt chart.

Software Development Phases Estimated Date actual date
Requirement 2017-07-03 2017-07-07 2017-07-03 2017-07-05
Design 2017-07-10 2017-07-14 2017-07-06 2017-07-17
Coding 2017-07-17 2017-07-21 2017-07-18 2017-07-27
Testing 2017-07-24 2017-07-28 2017-07-28 2017-08-01
Deployment 2017-07-31 2017-08-04 2017-08-02 2017-08-04

 

Java program

The following example create a  Gantt chart from the above sample data.

GanttChartExample.java

package com.boraji.tutorial.jfreechart;

import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;

/**
 * @author imssbora
 *
 */
public class GanttChartExample extends JFrame {

   private static final long serialVersionUID = 1L;

   public GanttChartExample(String title) {
      super(title);
      // Create dataset
      IntervalCategoryDataset dataset = getCategoryDataset();
    
      // Create chart
      JFreeChart chart = ChartFactory.createGanttChart(
            "Gantt Chart Example | WWW.BORAJI.COM", // Chart title
            "Software Development Phases", // X-Axis Label
            "Timeline", // Y-Axis Label
            dataset);

      ChartPanel panel = new ChartPanel(chart);
      setContentPane(panel);
   }

   private IntervalCategoryDataset getCategoryDataset() {

      TaskSeries series1 = new TaskSeries("Estimated Date");
      series1.add(new Task("Requirement",
            Date.from(LocalDate.of(2017, 7, 3).atStartOfDay().toInstant(ZoneOffset.UTC)),
            Date.from(LocalDate.of(2017, 7, 7).atStartOfDay().toInstant(ZoneOffset.UTC))
         ));
      
      series1.add(new Task("Design",
            Date.from(LocalDate.of(2017, 7, 10).atStartOfDay().toInstant(ZoneOffset.UTC)),
            Date.from(LocalDate.of(2017, 7, 14).atStartOfDay().toInstant(ZoneOffset.UTC))
         ));
      
      series1.add(new Task("Coding",
            Date.from(LocalDate.of(2017, 7, 17).atStartOfDay().toInstant(ZoneOffset.UTC)),
            Date.from(LocalDate.of(2017, 7, 21).atStartOfDay().toInstant(ZoneOffset.UTC))
         ));
      
      series1.add(new Task("Testing",
            Date.from(LocalDate.of(2017, 7, 24).atStartOfDay().toInstant(ZoneOffset.UTC)),
            Date.from(LocalDate.of(2017, 7, 28).atStartOfDay().toInstant(ZoneOffset.UTC))
         ));
      
      series1.add(new Task("Deployment",
            Date.from(LocalDate.of(2017, 07, 31).atStartOfDay().toInstant(ZoneOffset.UTC)),
            Date.from(LocalDate.of(2017, 8, 4).atStartOfDay().toInstant(ZoneOffset.UTC))
         ));
      
      
      TaskSeries series2 = new TaskSeries("Actual Date");
      series2.add(new Task("Requirement",
            Date.from(LocalDate.of(2017, 7, 3).atStartOfDay().toInstant(ZoneOffset.UTC)),
            Date.from(LocalDate.of(2017, 7, 05).atStartOfDay().toInstant(ZoneOffset.UTC))
         ));
      
      series2.add(new Task("Design",
            Date.from(LocalDate.of(2017, 7, 6).atStartOfDay().toInstant(ZoneOffset.UTC)),
            Date.from(LocalDate.of(2017, 7, 17).atStartOfDay().toInstant(ZoneOffset.UTC))
         ));
      
      series2.add(new Task("Coding",
            Date.from(LocalDate.of(2017, 7, 18).atStartOfDay().toInstant(ZoneOffset.UTC)),
            Date.from(LocalDate.of(2017, 7, 27).atStartOfDay().toInstant(ZoneOffset.UTC))
         ));
      
      series2.add(new Task("Testing",
            Date.from(LocalDate.of(2017, 7, 28).atStartOfDay().toInstant(ZoneOffset.UTC)),
            Date.from(LocalDate.of(2017, 8, 1).atStartOfDay().toInstant(ZoneOffset.UTC))
         ));
      
      series2.add(new Task("Deployment",
            Date.from(LocalDate.of(2017, 8, 2).atStartOfDay().toInstant(ZoneOffset.UTC)),
            Date.from(LocalDate.of(2017, 8, 4).atStartOfDay().toInstant(ZoneOffset.UTC))
         ));

      TaskSeriesCollection dataset = new TaskSeriesCollection();
      dataset.add(series1);
      dataset.add(series2);
      return dataset;
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(() -> {
         GanttChartExample example = new GanttChartExample("Gantt Chart Example");
         example.setSize(800, 400);
         example.setLocationRelativeTo(null);
         example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
         example.setVisible(true);
      });
   }
}

Output

JFreeChart - Gantt chart example | BORAJI.COM