Zooming and Panning in ASP.NET CORE Chart Component
19 Dec 202421 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 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.
NOTE
Pinch zooming is supported only in browsers that support multi-touch gestures. Currently IE11, Chrome and Opera browsers support multi-touch in desktop devices.
<ejs-chart id="container" load="window.load" width="60%">
<e-chart-primaryxaxis valueType="DateTime"></e-chart-primaryxaxis>
<e-chart-legendsettings visible="false"></e-chart-legendsettings>
<e-chart-zoomsettings enableMouseWheelZooming="true" enablePinchZooming="true" enableSelectionZooming="true"></e-chart-zoomsettings>
<e-series-collection>
<e-series xName="x" width=2 yName="y" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Area"></e-series>
</e-series-collection>
</ejs-chart>
<script>
var series1 = [];
var point1;
var value = 40;
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);
}
window.load = function (args) {
args.chart.series[0].dataSource = series1;
}
</script>
public IActionResult Index()
{
return View();
}
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.
<ejs-chart id="container" load="window.load" width="60%">
<e-chart-primaryxaxis valueType="DateTime"></e-chart-primaryxaxis>
<e-chart-legendsettings visible="false"></e-chart-legendsettings>
<e-chart-zoomsettings enableSelectionZooming="true" mode="X"></e-chart-zoomsettings>
<e-series-collection>
<e-series xName="x" width=2 yName="y" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Area"></e-series>
</e-series-collection>
</ejs-chart>
<script>
var series1 = [];
var point1;
var value = 40;
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);
}
window.load = function (args) {
args.chart.series[0].dataSource = series1;
}
</script>
public IActionResult Index()
{
return View();
}
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.
<ejs-chart id="container" load="window.load" width="60%">
<e-chart-primaryxaxis valueType="DateTime"></e-chart-primaryxaxis>
<e-chart-legendsettings visible="false"></e-chart-legendsettings>
<e-chart-zoomsettings enableSelectionZooming="true" showToolbar= "true" toolbarItems="ViewBag.toolBarItems"></e-chart-zoomsettings>
<e-series-collection>
<e-series xName="x" width=2 yName="y" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Area"></e-series>
</e-series-collection>
</ejs-chart>
<script>
var series1 = [];
var point1;
var value = 40;
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);
}
window.load = function (args) {
args.chart.series[0].dataSource = series1;
}
</script>
public IActionResult Index()
{
ViewBag.toolBarItems = new String[] { "Zoom", "Pan", "Reset" };
return View();
}
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.
<ejs-chart id="container" load="window.load" width="60%">
<e-chart-primaryxaxis valueType="DateTime"></e-chart-primaryxaxis>
<e-chart-legendsettings visible="false"></e-chart-legendsettings>
<e-chart-zoomsettings enableSelectionZooming="true" showToolbar= "true" toolbarItems="ViewBag.toolBarItems">
<e-zommsettings-toolbarPosition y="-10" draggable="true" horizontalAlignment="Far" verticalAlignment="Top"></e-zommsettings-toolbarPosition>
</e-chart-zoomsettings>
<e-series-collection>
<e-series xName="x" width=2 yName="y" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Area"></e-series>
</e-series-collection>
</ejs-chart>
<script>
var series1 = [];
var point1;
var value = 40;
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);
}
window.load = function (args) {
args.chart.series[0].dataSource = series1;
}
</script>
public IActionResult Index()
{
ViewBag.toolBarItems = new String[] { "Zoom", "Pan", "Reset" };
return View();
}
Enable pan
Using enablePan
property, you can pan the zoomed chart without the help of toolbar items.
<ejs-chart id="container" load="window.load" width="60%">
<e-chart-primaryxaxis valueType="DateTime" zoomFactor=0.2 zoomPosition=0.6></e-chart-primaryxaxis>
<e-chart-legendsettings visible="false"></e-chart-legendsettings>
<e-chart-zoomsettings enableSelectionZooming="true" enablePan="true"></e-chart-zoomsettings>
<e-series-collection>
<e-series xName="x" width=2 yName="y" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Area"></e-series>
</e-series-collection>
</ejs-chart>
<script>
var series1 = [];
var point1;
var value = 40;
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);
}
window.load = function (args) {
args.chart.series[0].dataSource = series1;
}
</script>
public IActionResult Index()
{
return View();
}
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.
<ejs-chart id="container" load="window.load" width="60%" title="Sales History of Product X">
<e-chart-primaryxaxis valueType="@Syncfusion.EJ2.Charts.ValueType.DateTime">
<e-scrollbarSettings enable="true" enableZoom="false" height="14" trackRadius="8" scrollbarRadius="8"
gripColor="transparent" trackColor="yellow" scrollbarColor="red"></e-scrollbarSettings>
</e-chart-primaryxaxis>
<e-chart-legendsettings visible="false"></e-chart-legendsettings>
<e-chart-zoomsettings enableSelectionZooming="true" enableScrollbar="true"
mode="@Syncfusion.EJ2.Charts.ZoomMode.X"></e-chart-zoomsettings>
<e-series-collection>
<e-series xName="x" yName="y" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Area" name="Product X">
<e-series-border width="0.5" color="#00bdae"></e-series-border>
<e-series-animation enable="false"></e-series-animation>
</e-series>
</e-series-collection>
</ejs-chart>
<script>
var series1 = [];
var point1;
var value = 40;
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);
}
window.load = function (args) {
args.chart.series[0].dataSource = series1;
}
</script>
public IActionResult Index()
{
return View();
}
Enable animation
Enable the EnableAnimation
property to experience smooth transitions when zooming in on the chart.
<ejs-chart id="container" load="load" title="Sales History of Product X">
<e-chart-primaryxaxis valueType="@Syncfusion.EJ2.Charts.ValueType.DateTime" zoomFactor="0.2" zoomPosition="0.6"></e-chart-primaryxaxis>
<e-chart-legendsettings visible="false"></e-chart-legendsettings>
<e-chart-zoomsettings enableSelectionZooming="true" enablePan="true" enableAnimation="true"></e-chart-zoomsettings>
<e-series-collection>
<e-series xName="x" yName="y" name="Product X" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Area">
<e-series-border width="0.5" color="#00bdae"></e-series-border>
<e-series-animation enable="false"></e-series-animation>
</e-series>
</e-series-collection>
</ejs-chart>
<script>
var series1 = [];
var point1;
var value = 40;
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);
}
function load(args) {
args.chart.series[0].dataSource = series1;
}
</script>
public IActionResult Index()
{
return View();
}
Auto interval on zooming
By using enableAutoIntervalOnZooming
property, the axis interval will get calculated automatically with respect to the zoomed range.
<ejs-chart id="container" load="window.load" width="60%">
<e-chart-primaryxaxis valueType="DateTime" enableAutoIntervalOnZooming="true"></e-chart-primaryxaxis>
<e-chart-legendsettings visible="false"></e-chart-legendsettings>
<e-chart-zoomsettings enableSelectionZooming="true"></e-chart-zoomsettings>
<e-series-collection>
<e-series xName="x" width=2 yName="y" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Area"></e-series>
</e-series-collection>
</ejs-chart>
<script>
var series1 = [];
var point1;
var value = 40;
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);
}
window.load = function (args) {
args.chart.series[0].dataSource = series1;
}
</script>
public IActionResult Index()
{
return View();
}