Date time axis in JavaScript Chart control
18 Nov 201824 minutes to read
DateTime Axis
The DateTime axis uses a date-time scale and displays date-time values as axis labels based on the specified format. This axis type is ideal for visualizing time-based data such as trends, timelines, and time-series data.
var chartData = [
{ x: new Date(2000, 6, 11), y: 10 }, { x: new Date(2002, 3, 7), y: 30 },
{ x: new Date(2004, 3, 6), y: 15 }, { x: new Date(2006, 3, 30), y: 65 },
{ x: new Date(2008, 3, 8), y: 90 }, { x: new Date(2010, 3, 8), y: 85 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
// Date time scale in primary X Axis
valueType: 'DateTime',
title: 'Sales Across Years',
labelFormat: 'yMMM'
},
primaryYAxis: {
title: 'Sales Amount in millions(USD)'
},
series:[{
dataSource: chartData,
xName: 'x', yName: 'y',
name: 'Sales', type: 'Line'
}],
title: 'Average Sales Comparison'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Note: To use the Datetime axis, inject
DateTimeusingChart.Inject(DateTime)method and set thevalueTypeproperty of the axis toDateTime.
DateTimeCategory Axis
The DateTimeCategory axis is used to render date-time values with non-linear intervals. This axis type is especially useful when certain time ranges, such as weekends or holidays, need to be excluded. In the following example, only business days are displayed within a week.
var chartData = [
{ x: new Date(2000, 6, 11), y: 10 }, { x: new Date(2002, 3, 7), y: 30 },
{ x: new Date(2004, 3, 6), y: 15 }, { x: new Date(2006, 3, 30), y: 65 },
{ x: new Date(2008, 3, 8), y: 90 }, { x: new Date(2010, 3, 8), y: 85 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'DateTimeCategory',
title: 'Sales Across Years',
labelFormat: 'yMMM'
},
primaryYAxis: {
title: 'Sales Amount in millions(USD)'
},
series:[{
dataSource: chartData,
xName: 'x', yName: 'y',
name: 'Sales', type: 'Line'
}],
title: 'Average Sales Comparison'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Note: To use the DateTimeCategory axis, inject
DateTimeCategoryusingChart.Inject(DateTimeCategory)method and set thevalueTypeproperty of the axis toDateTimeCategory. The axis range can be controlled using theminimum,maximum, andintervalproperties.
Range
Range of an axis will be calculated automatically based on the provided data, you can also customize the range of the axis using minimum, maximum and interval property of the axis.
var chartData = [
{ x: new Date(2000, 6, 11), y: 10 }, { x: new Date(2002, 3, 7), y: 30 },
{ x: new Date(2004, 3, 6), y: 15 }, { x: new Date(2006, 3, 30), y: 65 },
{ x: new Date(2008, 3, 8), y: 90 }, { x: new Date(2010, 3, 8), y: 85 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
// Date time range in primary X Axis
valueType: 'DateTimeCategory',
title: 'Sales Across Years',
labelFormat: 'yMMM',
},
primaryYAxis: {
title: 'Sales Amount in millions(USD)'
},
series:[{
dataSource: chartData,
xName: 'x', yName: 'y',
name: 'Sales', type: 'Line'
}],
title: 'Average Sales Comparison'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Interval Customization
Date-time intervals can be customized using the interval and intervalType properties of the axis. For example, when the interval is set to 2 and the interval type is set to Years, the axis displays labels at two-year intervals.
The DateTime axis supports the following interval types:
- Auto
- Years
- Months
- Days
- Hours
- Minutes
- Seconds
var chartData = [
{ x: new Date(2000, 6, 11), y: 10 }, { x: new Date(2002, 3, 7), y: 30 },
{ x: new Date(2004, 3, 6), y: 15 }, { x: new Date(2006, 3, 30), y: 65 },
{ x: new Date(2008, 3, 8), y: 90 }, { x: new Date(2010, 3, 8), y: 85 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'DateTimeCategory',
title: 'Sales Across Years',
labelFormat: 'yMMM',
intervalType: 'Years'
},
primaryYAxis: {
title: 'Sales Amount in millions(USD)'
},
series:[{
dataSource: chartData,
xName: 'x', yName: 'y',
name: 'Sales', type: 'Line'
}],
title: 'Average Sales Comparison'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Applying Padding to the Range
Padding can be applied to the minimum and maximum values of the axis range using the rangePadding property. The DateTime axis supports the following range padding types:
- None
- Round
- Additional
Datetime - None
When the rangePadding property is set to None, the minimum and maximum values of the axis are calculated directly from the data values.
var chartData = [
{ x: new Date(2000, 6, 11), y: 10 }, { x: new Date(2002, 3, 7), y: 30 },
{ x: new Date(2004, 3, 6), y: 15 }, { x: new Date(2006, 3, 30), y: 65 },
{ x: new Date(2008, 3, 8), y: 90 }, { x: new Date(2010, 3, 8), y: 85 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'DateTime',
title: 'Sales Across Years',
labelFormat: 'yMMM',
//Range padding as none
rangePadding: 'None'
},
primaryYAxis: {
title: 'Sales Amount in millions(USD)'
},
series:[{
dataSource: chartData,
xName: 'x', yName: 'y',
name: 'Sales', type: 'Line'
}],
title: 'Average Sales Comparison'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Datetime - Round
When the rangePadding property is set to Round, the minimum and maximum values are rounded to the nearest interval boundary. For example, if the minimum value is January 15 and the interval type is set to Months with an interval of 1, the axis minimum is adjusted to January 1.
var chartData = [
{ x: new Date(2000, 6, 11), y: 10 }, { x: new Date(2002, 3, 7), y: 30 },
{ x: new Date(2004, 3, 6), y: 15 }, { x: new Date(2006, 3, 30), y: 65 },
{ x: new Date(2008, 3, 8), y: 90 }, { x: new Date(2010, 3, 8), y: 85 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'DateTime',
title: 'Sales Across Years',
labelFormat: 'yMMM',
//Range padding as round
rangePadding: 'Round'
},
primaryYAxis: {
title: 'Sales Amount in millions(USD)'
},
series:[{
dataSource: chartData,
xName: 'x', yName: 'y',
name: 'Sales', type: 'Line'
}],
title: 'Average Sales Comparison'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Datetime - Additional
When the rangePadding property is set to Additional, an additional interval is added before the minimum and after the maximum values of the axis range to provide extra spacing.
var chartData = [
{ x: new Date(2000, 6, 11), y: 10 }, { x: new Date(2002, 3, 7), y: 30 },
{ x: new Date(2004, 3, 6), y: 15 }, { x: new Date(2006, 3, 30), y: 65 },
{ x: new Date(2008, 3, 8), y: 90 }, { x: new Date(2010, 3, 8), y: 85 }
];
var chart= new ej.charts.Chart({
primaryXAxis: {
valueType: 'DateTime',
title: 'Sales Across Years',
labelFormat: 'yMMM',
//Range padding as additional
rangePadding: 'Additional'
},
primaryYAxis: {
title: 'Sales Amount in millions(USD)'
},
series:[{
dataSource: chartData,
xName: 'x', yName: 'y',
name: 'Sales', type: 'Line'
}],
title: 'Average Sales Comparison'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>Label Format
Date values displayed on the DateTime axis can be formatted using the labelFormat property. This property supports globalized date and time formats for clear and localized label rendering.
var chartData = [
{ x: new Date(2000, 6, 11), y: 10 }, { x: new Date(2002, 3, 7), y: 30 },
{ x: new Date(2004, 3, 6), y: 15 }, { x: new Date(2006, 3, 30), y: 65 },
{ x: new Date(2008, 3, 8), y: 90 }, { x: new Date(2010, 3, 8), y: 85 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'DateTime',
title: 'Sales Across Years',
//Label format as yMd
labelFormat: 'yMd'
},
primaryYAxis: {
title: 'Sales Amount in millions(USD)'
},
series:[{
dataSource: chartData,
xName: 'x', yName: 'y',
name: 'Sales', type: 'Line'
}],
title: 'Average Sales Comparison'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></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>The following table illustrates the output produced by applying commonly used date-time format strings to the labelFormat property.
| Label Value | Label Format Property Value | Result | Description | |||
| new Date(2000, 03, 10) | EEEE | Monday | Displays the full name of the day | |||
| new Date(2000, 03, 10) | yMd | 04/10/2000 | Displays the date in month/day/year format | |||
| new Date(2000, 03, 10) | MMM | Apr | Displays the abbreviated month name | |||
| new Date(2000, 03, 10) | hm | Displays the time in hours and minutes | </tr>||||
| new Date(2000, 03, 10) | hms | 12:00:00 AM | Displays the time in hours, minutes, and seconds |