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

2 May 202311 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.

var GanttData = [
    {
        TaskID: 1,
        TaskName: 'Product concept',
        StartDate: new Date('01/02/2019'),
        EndDate: new Date('01/21/2019'),
        subtasks: [
            { TaskID: 2, TaskName: 'Defining the product and its usage', StartDate: new Date('01/02/2019'), Duration: 3, Progress: 30 },
            { TaskID: 3, TaskName: 'Defining target audience', StartDate: new Date('01/02/2019'), Duration: 3 },
            {
                TaskID: 4, TaskName: 'Prepare product sketch and notes', StartDate: new Date('01/02/2019'), Duration: 2,
                Predecessor: '2', Progress: 30
            },
        ]
    },
    {
        TaskID: 5,
        TaskName: 'Market research',
        StartDate: new Date('01/02/2019'),
        EndDate: new Date('01/21/2019'),
        subtasks: [
            {
                TaskID: 6,
                TaskName: 'Demand analysis',
                StartDate: new Date('01/04/2019'),
                EndDate: new Date('01/21/2019'),
                subtasks: [
                    {
                        TaskID: 7, TaskName: 'Customer strength', StartDate: new Date('01/04/2019'), Duration: 4,
                        Predecessor: '5', Progress: 30
                    },
                    { TaskID: 8, TaskName: 'Market opportunity analysis', StartDate: new Date('01/04/2019'), Duration: 4, Predecessor: '5' }
                ]
            },
            {
                TaskID: 9, TaskName: 'Competitor analysis', StartDate: new Date('01/04/2019'), Duration: 4,
                Predecessor: '7, 8', Progress: 30
            },
            { TaskID: 10, TaskName: 'Product strength analsysis', StartDate: new Date('01/04/2019'), Duration: 4, Predecessor: '9' },
            {
                TaskID: 11, TaskName: 'Research complete', StartDate: new Date('01/04/2019'), Duration: 0, Predecessor: '10',
                Indicators: [
                    {
                        'date': '01/20/2019',
                        'name': 'Research completed',
                        'tooltip': 'Research completed',
                        'iconClass': 'description e-icons'
                    }
                ]
            }
        ]
    },
];

var gantt = new ej.gantt.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://cdn.syncfusion.com/ej2/23.1.36/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
       
    
    <div id="container">
        <div id="Gantt"></div>        
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>