How to show an exact date range in JavaScript Charts
Use the DateTime axis zoomFactor and zoomPosition properties to display a specific portion of the complete date range. In this example, buttons switch the visible range between the last month and the last day of the dataset.
Understand zoom factor and zoom position
-
zoomFactordefines the fraction of the complete axis range that remains visible. Valid values are from0to1;1displays the complete range. -
zoomPositiondefines where the visible range begins within the complete axis range. Valid values are from0to1 - zoomFactor.
Calculate these values from the required start and end dates instead of relying on fixed constants.
var totalDuration = dataEnd.getTime() - dataStart.getTime();
var visibleDuration = visibleEnd.getTime() - visibleStart.getTime();
var zoomFactor = visibleDuration / totalDuration;
var zoomPosition =
(visibleStart.getTime() - dataStart.getTime()) / totalDuration;Apply an exact visible range
Clamp the calculated values to the valid range, assign the values to primaryXAxis, and call refresh().
function showDateRange(startDate, endDate) {
var minimum = chartData[0].date.getTime();
var maximum = chartData[chartData.length - 1].date.getTime();
var total = maximum - minimum;
var start = Math.max(minimum, startDate.getTime());
var end = Math.min(maximum, endDate.getTime());
var factor;
var position;
if (total <= 0 || end <= start) {
return;
}
factor = Math.min(1, Math.max(0.0001, (end - start) / total));
position = Math.min(1 - factor, Math.max(0, (start - minimum) / total));
chart.primaryXAxis.zoomFactor = factor;
chart.primaryXAxis.zoomPosition = position;
chart.refresh();
}Complete example
var chartData = [];
var startDate = new Date(2026, 0, 1, 0, 0, 0);
var pointCount = 90 * 24;
var i;
for (i = 0; i < pointCount; i++) {
chartData.push({
date: new Date(startDate.getTime() + (i * 60 * 60 * 1000)),
value: 40 + (Math.sin(i / 24) * 10) + ((i % 12) * 0.5)
});
}
var chart = new ej.charts.Chart({
primaryXAxis: {
valueType: 'DateTime',
title: 'Date and time',
labelFormat: 'MMM d',
intervalType: 'Days',
edgeLabelPlacement: 'Shift'
},
primaryYAxis: {
title: 'Value',
minimum: 20,
maximum: 70,
interval: 10
},
series: [{
type: 'Line',
dataSource: chartData,
xName: 'date',
yName: 'value',
name: 'Reading',
animation: {
enable: false
}
}],
zoomSettings: {
enableMouseWheelZooming: true,
enablePinchZooming: true,
enableSelectionZooming: true,
enableScrollbar: true,
mode: 'X'
},
legendSettings: {
visible: false
},
title: 'Hourly Readings'
}, '#element');
function showDateRange(visibleStart, visibleEnd, labelFormat, intervalType) {
var minimum = chartData[0].date.getTime();
var maximum = chartData[chartData.length - 1].date.getTime();
var totalDuration = maximum - minimum;
var start = Math.max(minimum, visibleStart.getTime());
var end = Math.min(maximum, visibleEnd.getTime());
var zoomFactor;
var zoomPosition;
if (totalDuration <= 0 || end <= start) {
return;
}
zoomFactor = Math.min(
1,
Math.max(0.0001, (end - start) / totalDuration)
);
zoomPosition = Math.min(
1 - zoomFactor,
Math.max(0, (start - minimum) / totalDuration)
);
chart.primaryXAxis.zoomFactor = zoomFactor;
chart.primaryXAxis.zoomPosition = zoomPosition;
chart.primaryXAxis.labelFormat = labelFormat;
chart.primaryXAxis.intervalType = intervalType;
chart.refresh();
}
function getLastDataDate() {
return chartData[chartData.length - 1].date;
}
document.getElementById('showLastMonth').onclick = function () {
var end = getLastDataDate();
var start = new Date(end.getTime());
start.setMonth(start.getMonth() - 1);
showDateRange(start, end, 'MMM d', 'Days');
};
document.getElementById('showLastDay').onclick = function () {
var end = getLastDataDate();
var start = new Date(end.getTime() - (24 * 60 * 60 * 1000));
showDateRange(start, end, 'MMM d, HH:mm', 'Hours');
};
document.getElementById('resetRange').onclick = function () {
chart.primaryXAxis.zoomFactor = 1;
chart.primaryXAxis.zoomPosition = 0;
chart.primaryXAxis.labelFormat = 'MMM d';
chart.primaryXAxis.intervalType = 'Days';
chart.refresh();
};<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Exact Date Range in Chart</title>
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js"></script>
<style>
.chart-actions {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 12px;
}
#element {
height: 360px;
}
</style>
</head>
<body>
<div class="chart-actions">
<button id="showLastMonth" type="button">Show Last Month</button>
<button id="showLastDay" type="button">Show Last Day</button>
<button id="resetRange" type="button">Reset Range</button>
</div>
<div id="element"></div>
<script src="index.js"></script>
</body>
</html>Troubleshooting
- If the wrong range appears, verify that the source data is sorted by date and that the first and last points represent the complete range.
- If
zoomFactororzoomPositionis invalid, clamp both values to their supported ranges. - If labels are too dense, update
labelFormatandintervalTypefor the selected range. - If dates shift by several hours, verify whether the source uses local time or UTC and apply one convention consistently.
- If the scrollbar does not appear, verify that
enableScrollbaristrueand thatzoomFactoris below1.