Render timeline from 1 to 365 days in EJ2 TypeScript Gantt control

2 May 20235 minutes to read

Gantt chart contains different types of in-built timeline view modes and it can be defined by using timelineViewMode property and also we can customize the timescale mode of top tier and bottom tier by using topTier.unit and bottomTier.unit properties. Timeline tier’s unit can be defined by using unit property and format property was used to define date format of timeline cell and formatter property was used to define custom method to format the date value of timeline cell.

In the bottomTier.unit timescale mode, it is possible to display timeline from 1 to 365 days by making use of the formatter in the timelineSettings property. The following example shows how to use the formatter method for timeline cells.

import { Gantt } from '@syncfusion/ej2-gantt';
import { GanttData } from 'datasource.ts';

let gantt: Gantt = new Gantt({
    dataSource: GanttData,
    height:'450px',
    taskFields: {
        id: 'TaskID',
        name: 'TaskName',
        startDate: 'StartDate',
        duration: 'Duration',
        progress: 'Progress',
        child: 'subtasks'
    },
    timelineSettings: {
        timelineUnitSize: 50,
        topTier: {
            unit: "Month",
            format: "MMM dd, y"
        },
        bottomTier: {
            unit: "Day",
            formatter: date => {
                let presentDate = new Date(
                    date.getFullYear(),
                    date.getMonth(),
                    date.getDate()
                );
                var start = new Date(presentDate.getFullYear(), 0, 0);
                var diff = Number(presentDate) - Number(start);
                var oneDay = 1000 * 60 * 60 * 24;
                var day = Math.floor(diff / oneDay);
                return day;
            }
          }
    },
    projectStartDate: new Date("01/01/2019"),
    projectEndDate: new Date("01/01/2020")
    });

    gantt.appendTo('#Gantt');
<!DOCTYPE html>
<html lang="en">

<head>
    <title>EJ2 Gantt</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript Gantt Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="index.css" rel="stylesheet" />
	<link href="https://cdn.syncfusion.com/ej2/material.css" rel="stylesheet" type="text/css"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
       
    <div id='loader'>Loading....</div>
    <div id='container'>
        <div id='Gantt'></div>        
    </div>
</body>

</html>