Zooming in Vue Chart component
25 Sep 202424 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.
<template>
<div id="app">
<ejs-chart id="container" :title='title' :primaryXAxis='primaryXAxis' :zoomSettings='zoom'
:legendSettings='legend'>
<e-series-collection>
<e-series :dataSource='seriesData1' type='Area' xName='x' yName='y' name='Product X' :border='border'
:animation='animation' opacity=0.3> </e-series>
</e-series-collection>
</ejs-chart>
</div>
</template>
<script setup>
import { provide } from "vue";
import { ChartComponent as EjsChart, SeriesCollectionDirective as ESeriesCollection, SeriesDirective as ESeries, AreaSeries, DateTime, Zoom } from "@syncfusion/ej2-vue-charts";
let series1 = [];
let point1;
let value = 40;
let 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);
}
const seriesData1 = series1;
const primaryXAxis = {
valueType: 'DateTime',
labelFormat: 'yMMM'
};
const zoom =
{
enableMouseWheelZooming: true,
enablePinchZooming: true,
enableSelectionZooming: true
};
const title = 'Sales History of Product X';
const legend = { visible: false };
const border = { width: 0.5, color: '#00bdae' };
const animation = { enable: false };
provide('chart', [AreaSeries, DateTime, Zoom]);
</script>
<style>
#container {
height: 350px;
}
</style>
<template>
<div id="app">
<ejs-chart id="container" :title='title' :primaryXAxis='primaryXAxis' :zoomSettings='zoom' :legendSettings='legend'>
<e-series-collection>
<e-series :dataSource='seriesData1' type='Area' xName='x' yName='y' name='Product X' :border='border' :animation='animation'
opacity=0.3> </e-series>
</e-series-collection>
</ejs-chart>
</div>
</template>
<script>
import { ChartComponent, SeriesDirective, SeriesCollectionDirective, AreaSeries, DateTime, Zoom } from "@syncfusion/ej2-vue-charts";
let series1 = [];
let point1;
let value = 40;
let 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);
}
export default {
name: "App",
components: {
"ejs-chart": ChartComponent,
"e-series-collection": SeriesCollectionDirective,
"e-series": SeriesDirective
},
data() {
return {
seriesData1: series1,
primaryXAxis: {
valueType: 'DateTime',
labelFormat: 'yMMM'
},
zoom:
{
enableMouseWheelZooming: true,
enablePinchZooming: true,
enableSelectionZooming: true
},
title: 'Sales History of Product X',
legend: { visible: false },
border: { width: 0.5, color: '#00bdae' },
animation: { enable: false }
};
},
provide: {
chart: [AreaSeries, DateTime, Zoom]
},
};
</script>
<style>
#container {
height: 350px;
}
</style>
Note: To use zooming feature, we need to inject
Zoom
into theprovide
.
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.
<template>
<div id="app">
<ejs-chart id="container" :title='title' :primaryXAxis='primaryXAxis' :zoomSettings='zoom'
:legendSettings='legend'>
<e-series-collection>
<e-series :dataSource='seriesData1' type='Area' xName='x' yName='y' name='Product X' :border='border'
:animation='animation' opacity=0.3> </e-series>
</e-series-collection>
</ejs-chart>
</div>
</template>
<script setup>
import { provide } from "vue";
import { ChartComponent as EjsChart, SeriesCollectionDirective as ESeriesCollection, SeriesDirective as ESeries, AreaSeries, DateTime, Zoom } from "@syncfusion/ej2-vue-charts";
let series1 = [];
let point1;
let value = 40;
let 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);
}
const seriesData1 = series1;
const primaryXAxis = {
valueType: 'DateTime',
labelFormat: 'yMMM'
};
const zoom =
{
enableSelectionZooming: true,
mode: 'X'
};
const title = 'Sales History of Product X';
const legend = { visible: false };
const border = { width: 0.5, color: '#00bdae' };
const animation = { enable: false };
provide('chart', [AreaSeries, DateTime, Zoom]);
</script>
<style>
#container {
height: 350px;
}
</style>
<template>
<div id="app">
<ejs-chart id="container" :title='title' :primaryXAxis='primaryXAxis' :zoomSettings='zoom' :legendSettings='legend'>
<e-series-collection>
<e-series :dataSource='seriesData1' type='Area' xName='x' yName='y' name='Product X' :border='border'
:animation='animation' opacity=0.3> </e-series>
</e-series-collection>
</ejs-chart>
</div>
</template>
<script>
import { ChartComponent, SeriesDirective, SeriesCollectionDirective, AreaSeries, DateTime, Zoom } from "@syncfusion/ej2-vue-charts";
let series1 = [];
let point1;
let value = 40;
let 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);
}
export default {
name: "App",
components: {
"ejs-chart": ChartComponent,
"e-series-collection": SeriesCollectionDirective,
"e-series": SeriesDirective
},
data() {
return {
seriesData1: series1,
primaryXAxis: {
valueType: 'DateTime',
labelFormat: 'yMMM'
},
zoom:
{
enableSelectionZooming: true,
mode: 'X'
},
title: 'Sales History of Product X',
legend: { visible: false },
border: { width: 0.5, color: '#00bdae' },
animation: { enable: false }
};
},
provide: {
chart: [AreaSeries, DateTime, Zoom]
},
};
</script>
<style>
#container {
height: 350px;
}
</style>
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.
<template>
<div id="app">
<ejs-chart id="container" :title='title' :primaryXAxis='primaryXAxis' :zoomSettings='zoom'>
<e-series-collection>
<e-series :dataSource='seriesData1' type='Area' xName='x' yName='y' name='Product X' opacity=0.3>
</e-series>
</e-series-collection>
</ejs-chart>
</div>
</template>
<script setup>
import { provide } from "vue";
import { ChartComponent as EjsChart, SeriesCollectionDirective as ESeriesCollection, SeriesDirective as ESeries, AreaSeries, DateTime, Zoom } from "@syncfusion/ej2-vue-charts";
let series1 = [];
let point1;
let value = 40;
let 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);
}
const seriesData1 = series1;
const primaryXAxis = {
valueType: 'DateTime',
labelFormat: 'yMMM'
};
const zoom =
{
enableSelectionZooming: true,
toolbarItems: ['Zoom', 'Pan', 'Reset'],
showToolbar: true
};
const title = 'Sales History of Product X';
provide('chart', [AreaSeries, DateTime, Zoom]);
</script>
<style>
#container {
height: 350px;
}
</style>
<template>
<div id="app">
<ejs-chart id="container" :title='title' :primaryXAxis='primaryXAxis' :zoomSettings='zoom'>
<e-series-collection>
<e-series :dataSource='seriesData1' type='Area' xName='x' yName='y' name='Product X' opacity=0.3>
</e-series>
</e-series-collection>
</ejs-chart>
</div>
</template>
<script>
import { ChartComponent, SeriesDirective, SeriesCollectionDirective, AreaSeries, DateTime, Zoom } from "@syncfusion/ej2-vue-charts";
let series1 = [];
let point1;
let value = 40;
let 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);
}
export default {
name: "App",
components: {
"ejs-chart": ChartComponent,
"e-series-collection": SeriesCollectionDirective,
"e-series": SeriesDirective
},
data() {
return {
seriesData1: series1,
primaryXAxis: {
valueType: 'DateTime',
labelFormat: 'yMMM'
},
zoom:
{
enableSelectionZooming: true,
toolbarItems: ['Zoom', 'Pan', 'Reset'],
showToolbar: true
},
title: 'Sales History of Product X'
};
},
provide: {
chart: [AreaSeries, DateTime, Zoom]
},
};
</script>
<style>
#container {
height: 350px;
}
</style>
Enable pan
Using enablePan
property you can able to pan the zoomed chart without help of toolbar items.
<template>
<div id="app">
<ejs-chart id="container" :title='title' :primaryXAxis='primaryXAxis' :zoomSettings='zoom'>
<e-series-collection>
<e-series :dataSource='seriesData1' type='Area' xName='x' yName='y' name='Product X' opacity=0.3>
</e-series>
</e-series-collection>
</ejs-chart>
</div>
</template>
<script setup>
import { provide } from "vue";
import { ChartComponent as EjsChart, SeriesCollectionDirective as ESeriesCollection, SeriesDirective as ESeries, AreaSeries, DateTime, Zoom } from "@syncfusion/ej2-vue-charts";
let series1 = [];
let point1;
let value = 40;
let 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);
}
const seriesData1 = series1;
const primaryXAxis = {
valueType: 'DateTime',
labelFormat: 'yMMM',
zoomFactor: 0.2, zoomPosition: 0.6
};
const zoom =
{
enableSelectionZooming: true,
enablePan: true
};
const title = 'Sales History of Product X';
provide('chart', [AreaSeries, DateTime, Zoom]);
</script>
<style>
#container {
height: 350px;
}
</style>
<template>
<div id="app">
<ejs-chart id="container" :title='title' :primaryXAxis='primaryXAxis' :zoomSettings='zoom'>
<e-series-collection>
<e-series :dataSource='seriesData1' type='Area' xName='x' yName='y' name='Product X' opacity=0.3>
</e-series>
</e-series-collection>
</ejs-chart>
</div>
</template>
<script>
import { ChartComponent, SeriesDirective, SeriesCollectionDirective, AreaSeries, DateTime, Zoom } from "@syncfusion/ej2-vue-charts";
let series1 = [];
let point1;
let value = 40;
let 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);
}
export default {
name: "App",
components: {
"ejs-chart": ChartComponent,
"e-series-collection": SeriesCollectionDirective,
"e-series": SeriesDirective
},
data() {
return {
seriesData1: series1,
primaryXAxis: {
valueType: 'DateTime',
labelFormat: 'yMMM',
zoomFactor: 0.2, zoomPosition: 0.6
},
zoom:
{
enableSelectionZooming: true,
enablePan: true
},
title: 'Sales History of Product X'
};
},
provide: {
chart: [AreaSeries, DateTime, Zoom]
},
};
</script>
<style>
#container {
height: 350px;
}
</style>
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.
<template>
<div id="app">
<ejs-chart id="container" :title='title' :primaryXAxis='primaryXAxis' :zoomSettings='zoom'>
<e-series-collection>
<e-series :dataSource='seriesData1' type='Area' xName='x' yName='y' name='Product X' opacity=0.3>
</e-series>
</e-series-collection>
</ejs-chart>
</div>
</template>
<script setup>
import { provide } from "vue";
import { ChartComponent as EjsChart, SeriesCollectionDirective as ESeriesCollection, SeriesDirective as ESeries, AreaSeries, DateTime, Zoom, ScrollBar } from "@syncfusion/ej2-vue-charts";
let series1 = [];
let point1;
let value = 40;
let 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);
}
const seriesData1 = series1;
const primaryXAxis = {
valueType: 'DateTime',
labelFormat: 'yMMM',
zoomFactor: 0.2, zoomPosition: 0.6,
scrollbarSettings: {
enable: true,
enableZoom: false,
height: 14,
trackRadius: 8,
scrollbarRadius: 8,
gripColor: 'transparent',
trackColor: 'yellow',
scrollbarColor: 'red'
}
};
const zoom =
{
enableSelectionZooming: true,
enableScrollbar: true
}
const title = 'Sales History of Product X';
provide('chart', [AreaSeries, DateTime, Zoom, ScrollBar]);
</script>
<style>
#container {
height: 350px;
}
</style>
<template>
<div id="app">
<ejs-chart id="container" :title='title' :primaryXAxis='primaryXAxis' :zoomSettings='zoom'>
<e-series-collection>
<e-series :dataSource='seriesData1' type='Area' xName='x' yName='y' name='Product X' opacity=0.3>
</e-series>
</e-series-collection>
</ejs-chart>
</div>
</template>
<script>
import { ChartComponent, SeriesDirective, SeriesCollectionDirective, AreaSeries, DateTime, Zoom, ScrollBar } from "@syncfusion/ej2-vue-charts";
let series1 = [];
let point1;
let value = 40;
let 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);
}
export default {
name: "App",
components: {
"ejs-chart": ChartComponent,
"e-series-collection": SeriesCollectionDirective,
"e-series": SeriesDirective
},
data() {
return {
seriesData1: series1,
primaryXAxis: {
valueType: 'DateTime',
labelFormat: 'yMMM',
zoomFactor: 0.2, zoomPosition: 0.6,
scrollbarSettings: {
enable: true,
enableZoom: false,
height: 14,
trackRadius: 8,
scrollbarRadius: 8,
gripColor: 'transparent',
trackColor: 'yellow',
scrollbarColor: 'red'
}
},
zoom:
{
enableSelectionZooming: true,
enableScrollbar: true
},
title: 'Sales History of Product X'
};
},
provide: {
chart: [AreaSeries, DateTime, Zoom, ScrollBar]
},
};
</script>
<style>
#container {
height: 350px;
}
</style>
Enable animation
Use the enableAnimation
property to create smooth transitions when zooming in on the chart.
<template>
<div id="app">
<ejs-chart id="container" :title='title' :primaryXAxis='primaryXAxis' :zoomSettings='zoom'
:legendSettings='legend'>
<e-series-collection>
<e-series :dataSource='seriesData1' type='Area' xName='x' yName='y' name='Product X' :border='border'
:animation='animation' opacity=0.3> </e-series>
</e-series-collection>
</ejs-chart>
</div>
</template>
<script setup>
import { provide } from "vue";
import { ChartComponent as EjsChart, SeriesCollectionDirective as ESeriesCollection, SeriesDirective as ESeries, AreaSeries, DateTime, Zoom } from "@syncfusion/ej2-vue-charts";
let series1 = [];
let point1;
let value = 40;
let 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);
}
const seriesData1 = series1;
const primaryXAxis = {
valueType: 'DateTime',
labelFormat: 'yMMM',
zoomFactor: 0.2, zoomPosition: 0.6,
};
const zoom =
{
enableSelectionZooming: true,
enablePan: true,
enableAnimation: true
};
const title = 'Sales History of Product X';
const legend = { visible: false };
const border = { width: 0.5, color: '#00bdae' };
const animation = { enable: false };
provide('chart', [AreaSeries, DateTime, Zoom]);
</script>
<style>
#container {
height: 350px;
}
</style>
<template>
<div id="app">
<ejs-chart id="container" :title='title' :primaryXAxis='primaryXAxis' :zoomSettings='zoom' :legendSettings='legend'>
<e-series-collection>
<e-series :dataSource='seriesData1' type='Area' xName='x' yName='y' name='Product X' :border='border' :animation='animation'
opacity=0.3> </e-series>
</e-series-collection>
</ejs-chart>
</div>
</template>
<script>
import { ChartComponent, SeriesDirective, SeriesCollectionDirective, AreaSeries, DateTime, Zoom } from "@syncfusion/ej2-vue-charts";
let series1 = [];
let point1;
let value = 40;
let 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);
}
export default {
name: "App",
components: {
"ejs-chart": ChartComponent,
"e-series-collection": SeriesCollectionDirective,
"e-series": SeriesDirective
},
data() {
return {
seriesData1: series1,
primaryXAxis: {
valueType: 'DateTime',
labelFormat: 'yMMM',
zoomFactor: 0.2, zoomPosition: 0.6
},
zoom:
{
enableSelectionZooming: true,
enablePan: true
},
title: 'Sales History of Product X',
legend: { visible: false },
border: { width: 0.5, color: '#00bdae' },
animation: { enable: false }
};
},
provide: {
chart: [AreaSeries, DateTime, Zoom]
},
};
</script>
<style>
#container {
height: 350px;
}
</style>
Auto interval on zooming
By using enableAutoIntervalOnZooming
property,
the axis interval will get calculated automatically with respect to the zoomed range.
<template>
<div id="app">
<ejs-chart id="container" :title='title' :primaryXAxis='primaryXAxis' :zoomSettings='zoom'>
<e-series-collection>
<e-series :dataSource='seriesData1' type='Area' xName='x' yName='y' name='Product X' opacity=0.3>
</e-series>
</e-series-collection>
</ejs-chart>
</div>
</template>
<script setup>
import { provide } from "vue";
import { ChartComponent as EjsChart, SeriesCollectionDirective as ESeriesCollection, SeriesDirective as ESeries, AreaSeries, DateTime, Zoom } from "@syncfusion/ej2-vue-charts";
let series1 = [];
let point1;
let value = 40;
let 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);
}
const seriesData1 = series1;
const primaryXAxis = {
valueType: 'DateTime',
labelFormat: 'yMMM',
enableAutoIntervalOnZooming: true
};
const zoom =
{
enableSelectionZooming: true,
};
const title = 'Sales History of Product X';
provide('chart', [AreaSeries, DateTime, Zoom]);
</script>
<style>
#container {
height: 350px;
}
</style>
<template>
<div id="app">
<ejs-chart id="container" :title='title' :primaryXAxis='primaryXAxis' :zoomSettings='zoom'>
<e-series-collection>
<e-series :dataSource='seriesData1' type='Area' xName='x' yName='y' name='Product X' opacity=0.3> </e-series>
</e-series-collection>
</ejs-chart>
</div>
</template>
<script>
import { ChartComponent, SeriesDirective, SeriesCollectionDirective, AreaSeries, DateTime, Zoom } from "@syncfusion/ej2-vue-charts";
let series1 = [];
let point1;
let value = 40;
let 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);
}
export default {
name: "App",
components: {
"ejs-chart": ChartComponent,
"e-series-collection": SeriesCollectionDirective,
"e-series": SeriesDirective
},
data() {
return {
seriesData1: series1,
primaryXAxis: {
valueType: 'DateTime',
labelFormat: 'yMMM',
enableAutoIntervalOnZooming: true
},
zoom:
{
enableSelectionZooming: true,
},
title: 'Sales History of Product X'
};
},
provide: {
chart: [AreaSeries, DateTime, Zoom]
},
};
</script>
<style>
#container {
height: 350px;
}
</style>