DEV Community

Ananya Deka
Ananya Deka

Posted on

Creating a Gantt Chart with CanvasJS

Gantt charts are a fantastic way to visualize project timelines by illustrating tasks against time. They help in tracking progress, scheduling, and resource allocation in project management. In this article, we’ll walk through the steps to create a Gantt chart using CanvasJS, a user-friendly JavaScript charting library.

Gantt Chart implemented using CanvasJS

The Benefits of Gantt Charts

Gantt charts are a staple of project management, and for good reason. They offer a range of benefits, including:

  • Clear visualization: Gantt charts provide a clear and concise visual representation of the project timeline, making it easy to understand task dependencies and relationships.
  • Improved scheduling: With a Gantt chart, you can schedule resources and allocate tasks to team members with ease.
  • Enhanced planning: Gantt charts allow you to plan and visualize the timeline of events and activities, making it easier to identify potential bottlenecks.

Steps to Create Gantt Chart

Setting Up Your Environment

To get started, you need to include the CanvasJS library in your HTML file by linking it via CDN. Here's the initial HTML structure you can work with:



<!DOCTYPE HTML>
<html>
<head>
    <script src="https://cdn.canvasjs.com/canvasjs.min.js"></script>
    <style>
        #chartContainer {
            height: 400px; 
            width: 100%; 
        }
    </style>
</head>
<body>
    <div id="chartContainer"></div>
    <script>
        // Your Gantt chart code will go here
    </script>
</body>
</html>


Enter fullscreen mode Exit fullscreen mode

Creating the Gantt Chart

Creating a Gantt chart in CanvasJS is a straightforward process that requires just a few simple steps:

  1. Choose the rangeBar chart type: The rangeBar chart type is a versatile chart type that can be used to create a Gantt chart-like visualization.
  2. Provide timestamp values as y-values: To create a Gantt chart, you'll need to provide timestamp values as y-values. These values will represent the start and end times of each task.
  3. Use the CanvasJS.formatDate() function: The CanvasJS.formatDate() function is used to convert the timestamp values to display date and time values in the labels and tooltip content.

Gantt Chart Configuration

Next, we’ll set up the chart configuration.



const options = {
  ...
  axisY: {
    labelFormatter: function(e){
      return CanvasJS.formatDate(e.value, "MMM D, YYYY");
    },
    gridThickness: 1
  },
  toolTip:{
    contentFormatter: function ( e ) {
      return "<strong>" + e.entries[0].dataPoint.label + "</strong></br> Start: " +  CanvasJS.formatDate(e.entries[0].dataPoint.y[0], "MMM D - h:mm TT") + "</br>End : " +  CanvasJS.formatDate(e.entries[0].dataPoint.y[1], "MMM D - h:mm TT");
    },
    backgroundColor: "#f7f7f7",
    color: "#333",
    borderThickness: 1,
    borderColor: "#ddd"
  },
  data: [
    {
      type: "rangeBar",
      dataPoints: [
        { label: "Research", y: [(new Date(2024, 9, 4, 9, 0)).getTime(), (new Date(2024, 9, 7, 17, 0)).getTime()] },
        { label: "Design", y: [(new Date(2024, 9, 7, 9, 0)).getTime(), (new Date(2024, 9, 10, 17, 0)).getTime()] },
        { label: "Development", y: [(new Date(2024, 9, 10, 9, 0)).getTime(), (new Date(2024, 9, 20, 17, 0)).getTime()] },
        { label: "Testing", y: [(new Date(2024, 9, 20, 9, 0)).getTime(), (new Date(2024, 9, 25, 17, 0)).getTime()] },
        { label: "Documentation", y: [(new Date(2024, 9, 25, 9, 0)).getTime(), (new Date(2024, 9, 27, 17, 0)).getTime()] },
        { label: "Release & Verify", y: [(new Date(2024, 9, 28, 9, 0)).getTime(), (new Date(2024, 9, 28, 17, 0)).getTime()] }
      ]
    }
  ]
}


Enter fullscreen mode Exit fullscreen mode

This code creates a rangeBar chart with six tasks, each with a start and end time. The CanvasJS.formatDate() function is used to convert the timestamp values to display date and time values in the labels and tooltip content.

Check out this JSFiddle for the complete code of this example.

Tips and Variations

Here are a few tips and variations to help you customize your Gantt chart:

  • Use different colors and styles: Use different colors and styles to differentiate between tasks and resources.
  • Add more data points: Add more data points to create a more detailed Gantt chart.
  • Customize the date and time format: Use the CanvasJS.formatDate() function to customize the date and time format.
  • Experiment with different chart options: Experiment with different chart options and settings to customize the appearance and behavior of the Gantt chart.

Conclusion

And there you have it! You’ve successfully created a Gantt chart using CanvasJS to visualize how long different activities will take over the course of a project. Feel free to adjust the activities, durations, and other chart configurations to suit your project’s needs.

Top comments (0)