- Enable zooming
- Modes
- Toolbar
- Enable pan
- Enable scrollbar
- Enable animation
- Auto interval on zooming
Contact Support
Zooming in EJ2 JavaScript Chart control
28 Mar 202524 minutes to read
Enable zooming
Chart can be zoomed in three ways.
- Selection - By setting
enableSelectionZooming
property to true inzoomSettings
, you can zoom the chart by using the rubber band selection. - Mousewheel - By setting
enableMouseWheelZooming
property to true inzoomSettings
, you can zoomin and zoomout the chart by scrolling the mouse wheel. - Pinch - By setting
enablePinchZooming
property to true inzoomSettings
, you can zoom the chart through pinch gesture in touch enabled devices.
Pinch zooming is supported only in browsers that support multi-touch gestures. Currently IE11, Chrome and Opera browsers support multi-touch in desktop devices.
var series1 = [];
var point1;
var value = 80;
var i;
for (i = 1; i < 500; i++) {
if (Math.random() > .5) {
value += Math.random();
} else {
value -= Math.random();
}
point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
series1.push(point1);
}
var chart = new ej.charts.Chart({
chartArea : {border : {width : 0}},
primaryXAxis: {
title: 'Years',
valueType: 'DateTime',
labelFormat: 'yMMM',
edgeLabelPlacement: 'Shift',
majorGridLines : { width : 0 }
},
primaryYAxis:
{
title: 'Profit ($)',
rangePadding: 'None',
lineStyle : { width: 0 },
majorTickLines : {width : 0}
},
series: [
{
type: 'Area',
dataSource: series1,
name: 'Product X',
xName: 'x',
yName: 'y',
border: { width: 0.5, color: '#00bdae' },
animation: { enable: false }
},
],
//Zooming for chart
zoomSettings:
{
enableMouseWheelZooming: true,
enablePinchZooming: true,
enableSelectionZooming: true
},
title: 'Sales History of Product X',
legendSettings: { visible: false },
}, '#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/29.1.33/dist/ej2.min.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="element"></div>
</div>
<script id="Unemployment" type="text/x-template">
<div id='templateWrap'>
<table style="width:100%; border: 1px solid black;">
<tr><th colspan="2" bgcolor="#00FFFF">Unemployment</th></tr>
<tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
</table>
</div>
</script>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
After zooming the chart, a zooming toolbar will appear with zoom
,zoomin
, zoomout
, pan
and reset
buttons. Selecting the Pan option will allow to pan the chart and selecting the Reset option will reset the zoomed chart.
Modes
The mode
property in zoomSettings specifies whether the chart is allowed to scale along the horizontal axis or vertical axis. The default value of the mode is XY (both axis).
There are three types of mode.
- X- Allows us to zoom the chart horizontally.
- Y - Allows us to zoom the chart vertically.
- XY – Allows us to zoom the chart both vertically and horizontally.
var series1 = [];
var point1;
var value = 80;
var i;
for (i = 1; i < 500; i++) {
if (Math.random() > .5) {
value += Math.random();
} else {
value -= Math.random();
}
point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
series1.push(point1);
}
var chart = new ej.charts.Chart({
chartArea : {border : {width : 0}},
primaryXAxis: {
title: 'Years',
valueType: 'DateTime',
labelFormat: 'yMMM',
edgeLabelPlacement: 'Shift',
majorGridLines : { width : 0 }
},
primaryYAxis:
{
title: 'Profit ($)',
rangePadding: 'None',
lineStyle : { width: 0 },
majorTickLines : {width : 0}
},
series: [
{
type: 'Area',
dataSource: series1,
name: 'Product X',
xName: 'x',
yName: 'y',
border: { width: 0.5, color: '#00bdae' },
animation: { enable: false }
},
],
zoomSettings:
{
enableSelectionZooming: true,
//zoom mode as x
mode: 'X'
},
title: 'Sales History of Product X',
legendSettings: { visible: false },
}, '#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/29.1.33/dist/ej2.min.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="element"></div>
</div>
<script id="Unemployment" type="text/x-template">
<div id='templateWrap'>
<table style="width:100%; border: 1px solid black;">
<tr><th colspan="2" bgcolor="#00FFFF">Unemployment</th></tr>
<tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
</table>
</div>
</script>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Toolbar
By default, zoomin, zoomout, pan and reset buttons will be displayed for zoomed chart. You can customize to show the desired options in the toolbar using the toolbarItems
property. Also using the showToolbar
property, you can show toolkit for zooming and panning the chart during initial rendering itself.
var series1 = [];
var point1;
var value = 80;
var i;
for (i = 1; i < 500; i++) {
if (Math.random() > .5) {
value += Math.random();
} else {
value -= Math.random();
}
point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
series1.push(point1);
}
var chart = new ej.charts.Chart({
chartArea : {border : {width : 0}},
primaryXAxis: {
title: 'Years',
valueType: 'DateTime',
labelFormat: 'yMMM',
edgeLabelPlacement: 'Shift',
majorGridLines : { width : 0 }
},
primaryYAxis:
{
title: 'Profit ($)',
rangePadding: 'None',
lineStyle : { width: 0 },
majorTickLines : {width : 0}
},
series: [
{
type: 'Area',
dataSource: series1,
name: 'Product X',
xName: 'x',
yName: 'y',
border: { width: 0.5, color: '#00bdae' },
animation: { enable: false }
},
],
zoomSettings:
{
enableSelectionZooming: true,
//toolbar items for zooming toolkit
toolbarItems: ['Zoom', 'Pan', 'Reset'],
showToolbar: true
},
title: 'Sales History of Product X',
legendSettings: { visible: false },
}, '#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/29.1.33/dist/ej2.min.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="element"></div>
</div>
<script id="Unemployment" type="text/x-template">
<div id='templateWrap'>
<table style="width:100%; border: 1px solid black;">
<tr><th colspan="2" bgcolor="#00FFFF">Unemployment</th></tr>
<tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
</table>
</div>
</script>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Toolbar customization
The zoom toolbar in the chart can be repositioned using the toolbarPosition
property, allowing flexible alignment and placement. It supports horizontal alignments (Near, Center, and Far) and vertical alignments (Top, Middle, and Bottom), with default values set to Far and Top, respectively. For precise placement, the x
and y
properties can be used to adjust the toolbar’s position within the chart area. Additionally, enabling the draggable
property allows users to freely move the toolbar within the chart area, ensuring optimal usability.
var series1 = [];
var point1;
var value = 80;
var i;
for (i = 1; i < 500; i++) {
if (Math.random() > .5) {
value += Math.random();
} else {
value -= Math.random();
}
point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
series1.push(point1);
}
var chart = new ej.charts.Chart({
chartArea : {border : {width : 0}},
primaryXAxis: {
title: 'Years',
valueType: 'DateTime',
labelFormat: 'yMMM',
edgeLabelPlacement: 'Shift',
majorGridLines : { width : 0 }
},
primaryYAxis:
{
title: 'Profit ($)',
rangePadding: 'None',
lineStyle : { width: 0 },
majorTickLines : {width : 0}
},
series: [
{
type: 'Area',
dataSource: series1,
name: 'Product X',
xName: 'x',
yName: 'y',
border: { width: 0.5, color: '#00bdae' },
animation: { enable: false }
},
],
zoomSettings:
{
enableSelectionZooming: true,
//toolbar items for zooming toolkit
toolbarItems: ['Zoom', 'Pan', 'Reset'],
showToolbar: true,
toolbarPosition: {
y: -10,
draggable: true,
horizontalAlignment: "Far",
verticalAlignment: "Top"
}
},
title: 'Sales History of Product X',
legendSettings: { visible: false },
}, '#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/29.1.33/dist/ej2.min.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="element"></div>
</div>
<script id="Unemployment" type="text/x-template">
<div id='templateWrap'>
<table style="width:100%; border: 1px solid black;">
<tr><th colspan="2" bgcolor="#00FFFF">Unemployment</th></tr>
<tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
</table>
</div>
</script>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Enable pan
Using enablePan
property you can able to pan the zoomed chart without help of toolbar items.
var series1 = [];
var point1;
var value = 80;
var i;
for (i = 1; i < 500; i++) {
if (Math.random() > .5) {
value += Math.random();
} else {
value -= Math.random();
}
point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
series1.push(point1);
}
var chart = new ej.charts.Chart({
chartArea : {border : {width : 0}},
primaryXAxis: {
title: 'Years',
valueType: 'DateTime',
labelFormat: 'yMMM',
edgeLabelPlacement: 'Shift',
majorGridLines : { width : 0 }
},
primaryYAxis:
{
title: 'Profit ($)',
rangePadding: 'None',
lineStyle : { width: 0 },
majorTickLines : {width : 0}
},
series: [
{
type: 'Area',
dataSource: series1,
name: 'Product X',
xName: 'x',
yName: 'y',
border: { width: 0.5, color: '#00bdae' },
animation: { enable: false }
},
],
zoomSettings:
{
enableSelectionZooming: true,
//toolbar items for zooming toolkit
toolbarItems: ['Zoom', 'Pan', 'Reset'],
showToolbar: true
},
title: 'Sales History of Product X',
legendSettings: { visible: false },
}, '#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/29.1.33/dist/ej2.min.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="element"></div>
</div>
<script id="Unemployment" type="text/x-template">
<div id='templateWrap'>
<table style="width:100%; border: 1px solid black;">
<tr><th colspan="2" bgcolor="#00FFFF">Unemployment</th></tr>
<tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
</table>
</div>
</script>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Enable scrollbar
Using the enableScrollbar
property, you can add a scrollbar to a zoomed chart. This scrollbar allows you to zoom or pan the chart. The appearance of the scrollbar can be customized using properties in scrollbarSettings
. For example, you can use trackColor
and trackRadius
properties to customize the track of the scrollbar, and scrollbarRadius
and scrollbarColor
properties to customize the scroller. The ability to zoom through the scrollbar can be enabled or disabled using the enableZoom
property in scrollbarSettings
. Additionally, you can change the color of the grip and height of the scrollbar using the gripColor
and height
properties.
var series1 = [];
var point1;
var value = 80;
var i;
for (i = 1; i < 500; i++) {
if (Math.random() > .5) {
value += Math.random();
} else {
value -= Math.random();
}
point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
series1.push(point1);
}
var chart = new ej.charts.Chart({
chartArea: { border: { width: 0 } },
primaryXAxis: {
valueType: 'DateTime',
zoomFactor: 0.2,
zoomPosition: 0.6,
scrollbarSettings: {
enable: true,
enableZoom: false,
height: 14,
trackRadius: 8,
scrollbarRadius: 8,
gripColor: 'transparent',
trackColor: 'yellow',
scrollbarColor: 'red'
}
},
series: [
{
type: 'Area',
dataSource: series1,
name: 'Product X',
xName: 'x',
yName: 'y',
border: { width: 0.5, color: '#00bdae' },
animation: { enable: false }
},
],
//Zooming for chart
zoomSettings:
{
enableSelectionZooming: true,
enableScrollbar: true,
mode: 'X'
},
title: 'Sales History of Product X',
legendSettings: { visible: false },
}, '#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/29.1.33/dist/ej2.min.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="element"></div>
</div>
<script id="Unemployment" type="text/x-template">
<div id='templateWrap'>
<table style="width:100%; border: 1px solid black;">
<tr><th colspan="2" bgcolor="#00FFFF">Unemployment</th></tr>
<tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
</table>
</div>
</script>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Position
The position
property allows users to specify their preferred scrollbar location. By default, both vertical and horizontal scrollbars are rendered near their respective axes. Using the customization options below, you can position the scrollbar as desired:
- Default:
placeNextToAxisLine
. - Horizontal scrollbar: Available positions are
Top
andBottom
. - Vertical scrollbar: Available positions are
Left
andRight
.
var series1 = [];
var point1;
var value = 80;
var i;
for (i = 1; i < 500; i++) {
if (Math.random() > .5) {
value += Math.random();
} else {
value -= Math.random();
}
point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
series1.push(point1);
}
var chart = new ej.charts.Chart({
chartArea: { border: { width: 0 } },
primaryXAxis: {
valueType: 'DateTime',
zoomFactor: 0.2,
zoomPosition: 0.6,
scrollbarSettings: {
enable: true,
enableZoom: false,
height: 14,
trackRadius: 8,
scrollbarRadius: 8,
gripColor: 'transparent',
trackColor: 'yellow',
scrollbarColor: 'red',
position:'Bottom',
}
},
primaryYAxis: {
scrollbarSettings: {
enable: true,
enableZoom: false,
height: 14,
trackRadius: 8,
scrollbarRadius: 8,
gripColor: 'transparent',
trackColor: 'yellow',
scrollbarColor: 'red',
position:'Right',
}
},
series: [
{
type: 'Area',
dataSource: series1,
name: 'Product X',
xName: 'x',
yName: 'y',
border: { width: 0.5, color: '#00bdae' },
animation: { enable: false }
},
],
//Zooming for chart
zoomSettings:
{
enableSelectionZooming: true,
enableScrollbar: true,
mode: 'X'
},
title: 'Sales History of Product X',
legendSettings: { visible: false },
}, '#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/29.1.33/dist/ej2.min.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="element"></div>
</div>
<script id="Unemployment" type="text/x-template">
<div id='templateWrap'>
<table style="width:100%; border: 1px solid black;">
<tr><th colspan="2" bgcolor="#00FFFF">Unemployment</th></tr>
<tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
</table>
</div>
</script>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Enable animation
Enable the enableAnimation property to experience smooth transitions when zooming in on the chart.
var series1 = [];
var point1;
var value = 80;
var i;
for (i = 1; i < 500; i++) {
if (Math.random() > .5) {
value += Math.random();
} else {
value -= Math.random();
}
point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
series1.push(point1);
}
var chart = new ej.charts.Chart({
chartArea : {border : {width : 0}},
primaryXAxis: {
title: 'Years',
valueType: 'DateTime',
labelFormat: 'yMMM',
edgeLabelPlacement: 'Shift',
majorGridLines : { width : 0 }
},
primaryYAxis:
{
title: 'Profit ($)',
rangePadding: 'None',
lineStyle : { width: 0 },
majorTickLines : {width : 0}
},
series: [
{
type: 'Area',
dataSource: series1,
name: 'Product X',
xName: 'x',
yName: 'y',
border: { width: 0.5, color: '#00bdae' },
animation: { enable: false }
},
],
zoomSettings:
{
enableSelectionZooming: true,
//toolbar items for zooming toolkit
toolbarItems: ['Zoom', 'Pan', 'Reset'],
showToolbar: true,
enableAnimation: true
},
title: 'Sales History of Product X',
legendSettings: { visible: false },
}, '#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/29.1.33/dist/ej2.min.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="element"></div>
</div>
<script id="Unemployment" type="text/x-template">
<div id='templateWrap'>
<table style="width:100%; border: 1px solid black;">
<tr><th colspan="2" bgcolor="#00FFFF">Unemployment</th></tr>
<tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
</table>
</div>
</script>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Auto interval on zooming
By using enableAutoIntervalOnZooming
property, the axis interval will get calculated automatically with respect to the zoomed range.
var series1 = [];
var point1;
var value = 80;
var i;
for (i = 1; i < 500; i++) {
if (Math.random() > .5) {
value += Math.random();
} else {
value -= Math.random();
}
point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
series1.push(point1);
}
var chart = new ej.charts.Chart({
chartArea : {border : {width : 0}},
primaryXAxis: {
title: 'Years',
valueType: 'DateTime',
labelFormat: 'yMMM',
edgeLabelPlacement: 'Shift',
majorGridLines : { width : 0 },
enableAutoIntervalOnZooming : true
},
primaryYAxis:
{
title: 'Profit ($)',
rangePadding: 'None',
lineStyle : { width: 0 },
majorTickLines : {width : 0},
enableAutoIntervalOnZooming : true
},
series: [
{
type: 'Area',
dataSource: series1,
name: 'Product X',
xName: 'x',
yName: 'y',
border: { width: 0.5, color: '#00bdae' },
animation: { enable: false }
},
],
zoomSettings:
{
enableSelectionZooming: true,
//toolbar items for zooming toolkit
toolbarItems: ['Zoom', 'Pan', 'Reset']
},
title: 'Sales History of Product X',
legendSettings: { visible: false },
}, '#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/29.1.33/dist/ej2.min.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="element"></div>
</div>
<script id="Unemployment" type="text/x-template">
<div id='templateWrap'>
<table style="width:100%; border: 1px solid black;">
<tr><th colspan="2" bgcolor="#00FFFF">Unemployment</th></tr>
<tr><td bgcolor="#00FFFF">${x}:</td><td bgcolor="#00FFFF">${y}</td></tr>
</table>
</div>
</script>
<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 zooming feature, we need to inject
Zoom
moduleChart.Inject(Zoom)
method.