Zooming and Panning in ASP.NET MVC Chart Component

24 Sep 202420 minutes to read

Enable zooming

Chart can be zoomed in three ways.

  • Selection - By setting EnableSelectionZooming property to true in ZoomSettings, you can zoom the chart by using the rubber band selection.
  • Mousewheel - By setting EnableMouseWheelZooming property to true in ZoomSettings, you can zoomin and zoomout the chart by scrolling the mouse wheel.
  • Pinch - By setting EnablePinchZooming property to true in ZoomSettings, 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.

@Html.EJS().Chart("container").
   PrimaryXAxis(px => px.Title("Years").
   MajorGridLines(ViewBag.majorGridLines).
       ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime).
       Skeleton("yMMM")).Series(series =>
       {
           series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Area).
           Border(ViewBag.border).Animation(ViewBag.animation).
           DataSource("series1").
           Name("Product X").
           XName("x").
           YName("y").Add();
       }
       ).ChartArea(area => area.Border(ViewBag.chartBorder)).
       LegendSettings(legend => legend.Visible(false)).
       PrimaryYAxis(py => py.Title("Profit ($)").
       LineStyle(ViewBag.lineStyle).
       MajorTickLines(ViewBag.majorTickLines).
       RangePadding(Syncfusion.EJ2.Charts.ChartRangePadding.None)).
       ZoomSettings(z => z.EnableMouseWheelZooming(true).
       EnablePinchZooming(true).EnableSelectionZooming(true).
       Mode(Syncfusion.EJ2.Charts.ZoomMode.X)).
       Title("Sales History of Product X").Render()

 <script>
        var series1 = [];
        var point1;
        var value = 80;
        var i;
        for (i = 1; i < 500; i++) {
            if (Math.random() > 0.5) {
                value += Math.random();
            }
            else {
                value -= Math.random();
            }
            point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
            series1.push(point1);
        }
    </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.
@Html.EJS().Chart("container").
   PrimaryXAxis(px => px.Title("Years").
   MajorGridLines(ViewBag.majorGridLines).
       ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime).
       Skeleton("yMMM")).Series(series =>
       {
           series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Area).
           Border(ViewBag.border).Animation(ViewBag.animation).
           DataSource("series1").
           Name("Product X").
           XName("x").
           YName("y").Add();
       }
       ).ChartArea(area => area.Border(ViewBag.chartBorder)).
       LegendSettings(legend => legend.Visible(false)).
       PrimaryYAxis(py => py.Title("Profit ($)").
       LineStyle(ViewBag.lineStyle).
       MajorTickLines(ViewBag.majorTickLines).
       RangePadding(Syncfusion.EJ2.Charts.ChartRangePadding.None)).
       ZoomSettings(z => z.EnableMouseWheelZooming(true).
       EnablePinchZooming(true).EnableSelectionZooming(true).
       Mode(Syncfusion.EJ2.Charts.ZoomMode.X)).
      Title("Sales History of Product X").Render()

 <script>
        var series1 = [];
        var point1;
        var value = 80;
        var i;
        for (i = 1; i < 500; i++) {
            if (Math.random() > 0.5) {
                value += Math.random();
            }
            else {
                value -= Math.random();
            }
            point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
            series1.push(point1);
        }
    </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.

@Html.EJS().Chart("container").
   PrimaryXAxis(px => px.Title("Years").
   MajorGridLines(ViewBag.majorGridLines).
       ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime).
       Skeleton("yMMM")).Series(series =>
       {
           series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Area).
           Border(ViewBag.border).Animation(ViewBag.animation).
           DataSource("series1").
           Name("Product X").
           XName("x").
           YName("y").Add();
       }
       ).ChartArea(area => area.Border(ViewBag.chartBorder)).
       LegendSettings(legend => legend.Visible(false)).
       PrimaryYAxis(py => py.Title("Profit ($)").
       LineStyle(ViewBag.lineStyle).
       MajorTickLines(ViewBag.majorTickLines).
       RangePadding(Syncfusion.EJ2.Charts.ChartRangePadding.None)).
       ZoomSettings(z => z.EnableMouseWheelZooming(true).
       ToolbarItems(ViewBag.toolbarItems).
       ShowToolbar(true).
       Mode(Syncfusion.EJ2.Charts.ZoomMode.X)).
       Title("Sales History of Product X").Render()

 <script>
        var series1 = [];
        var point1;
        var value = 80;
        var i;
        for (i = 1; i < 500; i++) {
            if (Math.random() > 0.5) {
                value += Math.random();
            }
            else {
                value -= Math.random();
            }
            point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
            series1.push(point1);
        }
    </script>
public IActionResult Index()
        {
            ViewBag.toolBarItems = new String[] { "Zoom", "Pan", "Reset" };
            return View();
        }

Enable pan

Using EnablePan property you can able to pan the zoomed chart without help of toolbar items.

@Html.EJS().Chart("container").
   PrimaryXAxis(px => px.Title("Years").
   MajorGridLines(ViewBag.majorGridLines).
       ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime).
       Skeleton("yMMM").ZoomFactor(0.2). ZoomPosition(0.6)).Series(series =>
       {
           series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Area).
           Border(ViewBag.border).Animation(ViewBag.animation).
           DataSource("series1").
           Name("Product X").
           XName("x").
           YName("y").Add();
       }
       ).ChartArea(area => area.Border(ViewBag.chartBorder)).
       LegendSettings(legend => legend.Visible(false)).
       PrimaryYAxis(py => py.Title("Profit ($)").
       LineStyle(ViewBag.lineStyle).
       MajorTickLines(ViewBag.majorTickLines).
       RangePadding(Syncfusion.EJ2.Charts.ChartRangePadding.None)).
       ZoomSettings(z => z.EnableMouseWheelZooming(true).
       EnablePinchZooming(true).EnableSelectionZooming(true).
       Mode(Syncfusion.EJ2.Charts.ZoomMode.X).EnablePan(true)).
       Title("Sales History of Product X").Render()

 <script>
        var series1 = [];
        var point1;
        var value = 80;
        var i;
        for (i = 1; i < 500; i++) {
            if (Math.random() > 0.5) {
                value += Math.random();
            }
            else {
                value -= Math.random();
            }
            point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
            series1.push(point1);
        }
    </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.

@Html.EJS().Chart("container").
   PrimaryXAxis(px => px.ScrollbarSettings(scroll => scroll.Enable(true).Height(14).EnableZoom(false).TrackRadius(8).ScrollbarRadius(8).GripColor("transparent").TrackColor("yellow").ScrollbarColor("red")).
       ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime)).Series(series =>
       {
           series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Area).
           Border(br => br.Width(0.5).Color("#00bdae")).Animation(an => an.Enable(false)).
           DataSource("series1").
           Name("Product X").
           XName("x").
           YName("y").Add();
       }
       ).
       LegendSettings(legend => legend.Visible(false)).
       ZoomSettings(z => z.EnableScrollbar(true)
       EnableSelectionZooming(true).
       Mode(Syncfusion.EJ2.Charts.ZoomMode.X)).
       Title("Sales History of Product X").Render()

 <script>
        var series1 = [];
        var point1;
        var value = 80;
        var i;
        for (i = 1; i < 500; i++) {
            if (Math.random() > 0.5) {
                value += Math.random();
            }
            else {
                value -= Math.random();
            }
            point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
            series1.push(point1);
        }
    </script>
public IActionResult Index()
        {
            return View();
        }

Enable animation

Enable the EnableAnimation property to experience smooth transitions when zooming in on the chart.

@Html.EJS().Chart("container").Series(series =>
{
   series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Area)
        .XName("x")
        .YName("y")
        .Name("Product X")
        .Border(br => br.Width(0.5).Color("#00bdae"))
        .Animation(an => an.Enable(false))
        .Add();
}).PrimaryXAxis(px => px.ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime).ZoomFactor(0.2).ZoomPosition(0.6)
 ).ZoomSettings(z => z.EnableSelectionZooming(true).EnablePan(true).EnableAnimation(true)
 ).Title("Sales History of Product X").LegendSettings(leg => leg.Visible(false)).Load("load").Render()

<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.

@Html.EJS().Chart("container").
   PrimaryXAxis(px => px.Title("Years").
   MajorGridLines(ViewBag.majorGridLines).
       ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime).
       Skeleton("yMMM")).Series(series =>
       {
           series.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Area).
           Border(ViewBag.border).Animation(ViewBag.animation).
           DataSource("series1").
           Name("Product X").
           XName("x").
           YName("y").Add();
       }
       ).ChartArea(area => area.Border(ViewBag.chartBorder)).
       LegendSettings(legend => legend.Visible(false)).
       enableAutoIntervalOnZooming(true).
       PrimaryYAxis(py => py.Title("Profit ($)").
       LineStyle(ViewBag.lineStyle).
       MajorTickLines(ViewBag.majorTickLines).
       RangePadding(Syncfusion.EJ2.Charts.ChartRangePadding.None)).
       ZoomSettings(z =>z.EnableSelectionZooming(true).
       Mode(Syncfusion.EJ2.Charts.ZoomMode.X)).
       Title("Sales History of Product X").Render()

 <script>
        var series1 = [];
        var point1;
        var value = 80;
        var i;
        for (i = 1; i < 500; i++) {
            if (Math.random() > 0.5) {
                value += Math.random();
            }
            else {
                value -= Math.random();
            }
            point1 = { x: new Date(1950, i + 2, i), y: value.toFixed(1) };
            series1.push(point1);
        }
    </script>
public IActionResult Index()
        {
            return View();
        }