Baseline in EJ2 JavaScript Gantt control
4 Jul 202617 minutes to read
The baseline feature in the Gantt Chart control enables comparison between original planned schedules and actual task execution timelines. This visualization provides clear insights into schedule deviations, helping assess project performance and identify areas requiring attention. Baseline functionality displays both the original planned timeline and current progress side-by-side for comprehensive project tracking.
Before implementing baseline functionality, ensure the data source includes baseline date fields and configure the taskFields object with appropriate field mappings. The baseline feature requires proper field mapping to display planned versus actual timelines effectively.
Baseline fields:
- baselineStartDate: Represents the originally planned start date of a task. This value is used to compare against the actual start date to identify schedule deviations.
- baselineEndDate: Represents the originally planned end date of a task. It is used to compare against the actual end date.
-
baselineDuration: Represents the total planned duration of the task. This value is critical for baseline visualization. To represent a baseline milestone, this property must be explicitly set to
0. SettingbaselineStartDateandbaselineEndDateto the same value without settingbaselineDurationto0will result in a one-day baseline task, not a milestone.
Implement baseline
To enable baseline, configure the Gantt control by setting renderBaseline to true, mapping baselineStartDate, baselineEndDate, and optionally baselineDuration in taskFields. To customize appearance set the baselineColor property or the .e-baseline-bar CSS class for advanced styling.
export let projectData = [
{
TaskID: 1,
TaskName: 'Project Planning',
StartDate: new Date('02/04/2019'),
EndDate: new Date('02/08/2019'),
baselineStartDate: new Date('02/02/2019'),
baselineEndDate: new Date('02/06/2019'),
baselineDuration: '5' // Regular baseline
},
{
TaskID: 2,
TaskName: 'Milestone Review',
StartDate: new Date('02/10/2019'),
EndDate: new Date('02/10/2019'),
baselineStartDate: new Date('02/09/2019'),
baselineEndDate: new Date('02/09/2019'),
baselineDuration: '0' // Milestone baseline
}
];
var baselineColor = 'rgba(255, 107, 107, 0.8)';.e-gantt .e-gantt-chart .e-baseline-bar {
height: 4px;
border-radius: 2px;
opacity: 0.9;
background-color: #4CAF50;
}The following example demonstrates complete baseline configuration with proper field mapping:
var ganttChart = new ej.gantt.Gantt({
dataSource: GanttData,
height:'450px',
taskFields: {
id: 'TaskID',
name: 'TaskName',
startDate: 'StartDate',
duration: 'Duration',
progress: 'Progress',
baselineStartDate:"BaselineStartDate",
baselineEndDate:"BaselineEndDate",
baselineDuration: "BaselineDuration",
parentID: "ParentID"
},
renderBaseline:true,
baselineColor:'red'
});
ganttChart.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/34.1.29/tailwind3.css" rel="stylesheet" type="text/css">
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.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>For a comprehensive demonstration of baseline functionality, explore the interactive sample.
Customize baseline using event
You can customize the baseline bar in the Gantt chart using the queryTaskbarInfo event.
queryTaskbarInfo: function (args) {
var element = args.rowElement.querySelector('.e-baseline-bar');
if (element) {
element.style.background = 'linear-gradient(red, yellow)';
}
}Customize baseline templates
The baselineTemplate property allows customization of baseline rendering by replacing the default baseline UI with a custom HTML structure. This enables advanced scenarios such as rendering additional baseline elements, visual indicators, or multiple baselines using task-specific data.
Set the baselineTemplate property with a template string or function. The template receives the task data object, which can be used to dynamically generate baseline elements.
Multiple baseline rendering using template
By default, the Gantt component supports a single baseline per task. However, using the baselineTemplate, you can extend this behavior to render multiple baselines by maintaining additional baseline data within a custom field in your data source.
This enables rich visualization scenarios such as:
- Comparing original vs revised schedules.
- Visualizing multiple planning phases.
- Highlighting deviations across timeline checkpoints.
The following example demonstrates how to render multiple baselines using baselineTemplate.
ej.gantt.Gantt.Inject(ej.gantt.DayMarkers, ej.gantt.Selection);
var ganttChart = new ej.gantt.Gantt({
dataSource: baselineTemplateData,
taskFields: {
id: 'TaskID',
name: 'TaskName',
startDate: 'StartDate',
endDate: 'EndDate',
duration: 'Duration',
progress: 'Progress',
baselineStartDate: 'BaselineStartDate',
baselineEndDate: 'BaselineEndDate',
parentID: 'ParentID',
},
baselineTemplate: baselineTemplate,
splitterSettings: {
columnIndex: 3,
},
tooltipSettings: {
showTooltip: false,
},
allowSelection: true,
renderBaseline: true,
rowHeight: 60,
taskbarHeight: 20,
gridLines: "Both",
highlightWeekends: true,
columns: [
{ field: 'TaskID', headerText: 'ID', textAlign: 'Left' },
{ field: 'TaskName', width: '270px', headerText: 'Name' },
{ field: 'BaselineStartDate', headerText: 'Baseline Start Date', width: '180px' },
{ field: 'BaselineDuration', headerText: 'Baseline Duration', width: '180px' },
{ field: 'BaselineStartDate1', format: { skeleton: 'yMd', type: 'date' }, headerText: 'Baseline1 Start Date', width: '180px' },
{ field: 'BaselineDuration1', headerText: 'Baseline1 Duration', width: '180px' },
{ field: 'BaselineStartDate2', format: { skeleton: 'yMd', type: 'date' }, headerText: 'Baseline2 Start Date', width: '180px' },
{ field: 'BaselineDuration2', headerText: 'Baseline2 Duration', width: '180px' }
],
labelSettings: {
taskLabel: 'TaskName'
},
height: '450px',
baselineColor: 'red'
});
ganttChart.appendTo('#Gantt');
function baselineTemplate(props) {
if (props.hasChildRecords || (props.data && props.data.hasChildRecords)) {
return '';
}
var taskRecord = props.taskData;
var ganttProperties = taskRecord.ganttProperties;
var chartRowsModule = gantt.chartRowsModule;
var baselineTop = chartRowsModule.baselineTop;
var baselineHeight = chartRowsModule.baselineHeight;
var taskBarHeight = chartRowsModule.taskBarHeight;
var milestoneHeight = chartRowsModule.milestoneHeight;
var milestoneMarginTop = chartRowsModule.milestoneMarginTop;
var rowHeight = gantt.rowHeight;
var renderBaseline = gantt.renderBaseline;
var enableRtl = gantt.enableRtl;
var taskSpacing = 9;
var baselineSpacing = 4;
function getLeft(date) {
return gantt.dataOperation.getTaskLeft(new Date(date), false, ganttProperties.calendarContext);
}
function getWidth(start, duration) {
if (!start || duration == null || duration === 0)
return 0;
// Calculate end date based on start date and duration
var end = new Date(start);
end.setDate(end.getDate() + duration);
var leftStart = gantt.dataOperation.getTaskLeft(new Date(start), false, ganttProperties.calendarContext);
var leftEnd = gantt.dataOperation.getTaskLeft(end, false, ganttProperties.calendarContext);
return leftEnd - leftStart;
}
function render(start, duration, index) {
if (!start)
return '';
var leftPosition = getLeft(start);
var width = getWidth(start, duration);
// Milestone baseline (duration = 0)
if (duration === 0) {
var milestoneSize = renderBaseline ? taskBarHeight : (taskBarHeight - 10);
var baselineMilestoneHeight = renderBaseline ? 5 : 2;
var leftPosition_ms = enableRtl
? (leftPosition - (milestoneHeight / 2) + 3)
: (leftPosition - (milestoneHeight / 2) + 1);
var marginTop = (-Math.floor(rowHeight - milestoneMarginTop) + baselineMilestoneHeight) +
2 +
(index * baselineSpacing);
// shift per baseline
return '<div class="e-baseline-gantt-milestone-container" style="position:absolute;' +
'width:' + milestoneSize + 'px;' +
'height:' + milestoneSize + 'px;' +
'transform:rotate(45deg);' +
(enableRtl ? 'right:' : 'left:') + leftPosition_ms + 'px;' +
'margin-top:' + marginTop + 'px;">' +
'</div>';
}
// Normal baseline bar
return '<div class="e-baseline-bar" role="term" style="position:absolute;' +
(enableRtl ? 'right:' : 'left:') + leftPosition + 'px;' +
'margin-top:' + (baselineTop + (index * taskSpacing)) + 'px;' +
'width:' + width + 'px;' +
'height:' + baselineHeight + 'px;"></div>';
}
return ('<div class="custom-multi-baseline">' +
render(taskRecord.taskData.BaselineStartDate, taskRecord.taskData.BaselineDuration, 0) +
render(taskRecord.taskData.BaselineStartDate1, taskRecord.taskData.BaselineDuration1, 1) +
render(taskRecord.taskData.BaselineStartDate2, taskRecord.taskData.BaselineDuration2, 2) +
'</div>');
}<!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/34.1.29/tailwind3.css" rel="stylesheet" type="text/css">
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.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>