How to show an exact date range in TypeScript 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
import {
Chart,
LineSeries,
DateTime,
Zoom
} from '@syncfusion/ej2-charts';
Chart.Inject(LineSeries, DateTime, Zoom);
const chartData: { date: Date; value: number }[] = [];
const startDate: Date = new Date(2026, 0, 1, 0, 0, 0);
const pointCount: number = 90 * 24;
for (let i: number = 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)
});
}
const chart: Chart = new 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'
});
chart.appendTo('#element');
function showDateRange(
visibleStart: Date,
visibleEnd: Date,
labelFormat: string,
intervalType: string
): void {
const minimum: number = chartData[0].date.getTime();
const maximum: number = chartData[chartData.length - 1].date.getTime();
const totalDuration: number = maximum - minimum;
const start: number = Math.max(minimum, visibleStart.getTime());
const end: number = Math.min(maximum, visibleEnd.getTime());
if (totalDuration <= 0 || end <= start) {
return;
}
const zoomFactor: number = Math.min(
1,
Math.max(0.0001, (end - start) / totalDuration)
);
const zoomPosition: number = 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 as any;
chart.refresh();
}
function getLastDataDate(): Date {
return chartData[chartData.length - 1].date;
}
(document.getElementById('showLastMonth') as HTMLButtonElement).onclick = (): void => {
const end: Date = getLastDataDate();
const start: Date = new Date(end.getTime());
start.setMonth(start.getMonth() - 1);
showDateRange(start, end, 'MMM d', 'Days');
};
(document.getElementById('showLastDay') as HTMLButtonElement).onclick = (): void => {
const end: Date = getLastDataDate();
const start: Date = new Date(end.getTime() - (24 * 60 * 60 * 1000));
showDateRange(start, end, 'MMM d, HH:mm', 'Hours');
};
(document.getElementById('resetRange') as HTMLButtonElement).onclick = (): void => {
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>
<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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
</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>
</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.