Zooming and Panning allows analysis of data at microscopic level and is very useful while plotting graphs with large data sets. All Charts with axis support Zooming & Panning along both X & Y axes in CanvasJS. Given example demonstrates a Line Chart with Zoom / Pan feature. It also includes source code that you can try running locally.
@{ Layout = null; } <!DOCTYPE HTML> <html> <head> <script> window.onload = function () { var chart = new CanvasJS.Chart("chartContainer", { theme: "light2", // "light1", "light2", "dark1", "dark2" animationEnabled: true, zoomEnabled: true, title: { text: "Try Zooming and Panning" }, data: [{ type: "line", dataPoints: @Html.Raw(ViewBag.DataPoints) }] }); chart.render(); } </script> </head> <body> <div id="chartContainer" style="height: 370px; width: 100%;"></div> <script src="https://cdn.canvasjs.com/canvasjs.min.js"></script> </body> </html>
using ASPNET_MVC_ChartsDemo.Models; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Web.Mvc; namespace ASPNET_MVC_ChartsDemo.Controllers { public class HomeController : Controller { // GET: Home public ActionResult Index() { double count = 1000, y = 100; Random random = new Random(); List<DataPoint> dataPoints = new List<DataPoint>(); for (int i = 0; i < count; i++) { y += random.Next(-10, 11); dataPoints.Add(new DataPoint(i, y)); } ViewBag.DataPoints = JsonConvert.SerializeObject(dataPoints); return View(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Web; namespace ASPNET_MVC_ChartsDemo.Models { //DataContract for Serializing Data - required to serve in JSON format [DataContract] public class DataPoint { public DataPoint(double x, double y) { this.X = x; this.Y = y; } //Explicitly setting the name to be used while serializing to JSON. [DataMember(Name = "x")] public Nullable<double> X = null; //Explicitly setting the name to be used while serializing to JSON. [DataMember(Name = "y")] public Nullable<double> Y = null; } }
You can customize along which axis zooming / panning should occur using zoomType property. Some other frequently used customization options include animationEnabled, animationDuration, theme, etc.
Note For step by step instructions, follow our ASP.NET MVC Integration Tutorial