If you haven’t already, please refer to this tutorial for step by step instruction of adding charts to your ASP.Net MVC Application. We recommend that you download the Sample Visual Studio Project and try it on your own to understand the API better.
We will be using following JSON format in this example:
[ {"x":0.0,"y":11.0}, {"x":1.0,"y":17.0}, {"x":2.0,"y":8.0}, {"x":3.0,"y":15.0}, {"x":4.0,"y":19.0}, {"x":5.0,"y":19.0}, {"x":6.0,"y":24.0}, {"x":7.0,"y":19.0}, . . . ]
Here is the code for creating Charts using JSON Data. We will get and parse the JSON Data using jQuery and convert the data to CanvasJS supported format if not already formatted (Please refer to Working with Data). Then we will initialize a chart and pass the dataPoints to the chart. In this example, JSON Data is already formatted according to CanvasJS, please refer to JavaScript Charts Using JSON & AJAX for example with different JSON format.
$.get("/home/json/", function (data) { var chart = new CanvasJS.Chart("chartContainer", { animationEnabled: true, theme: "light2",//light1 title: { text: "CanvasJS Charts in ASP.Net MVC using JSON & AJAX" }, data: [ { // Change type to "bar", "splineArea", "area", "spline", "pie",etc. type: "line", dataPoints: data } ] }); chart.render(); });
To summarize, in order to create charts using JSON Data, we just need to fetch and parse the data to CanvasJS supported format and then pass the dataPoints to the chart.
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <script type="text/javascript" src="https://canvasjs.com/assets/script/jquery-1.11.1.min.js"></script> <script src="https://cdn.canvasjs.com/canvasjs.min.js"></script> </head> <body> <h1>Charts using JSON & AJAX</h1> <div id="chartContainer" style="height: 300px; width: 100%;"></div> <script type="text/javascript"> $(document).ready(function () { $.get("/home/json/", function (data) { var chart = new CanvasJS.Chart("chartContainer", { animationEnabled: true, theme: "light2",//light1 title: { text: "CanvasJS Charts in ASP.Net MVC using JSON & AJAX" }, data: [ { // Change type to "bar", "splineArea", "area", "spline", "pie",etc. type: "line", dataPoints: data } ] }); chart.render(); }); }); </script> </body> </html>
using ASPNET_MVC_Samples.Models; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace ASPNET_MVC_Samples.Controllers { public class HomeController : Controller { // GET: Home public ActionResult Index() { return View(); } public ContentResult JSON() { double count = 50, y = 10; Random random = new Random(DateTime.Now.Millisecond); List<DataPoint> dataPoints; dataPoints = new List<DataPoint>(); for (int i = 0; i < count; i++) { y = y + (random.Next(0, 20) - 10); dataPoints.Add(new DataPoint(i, y)); } JsonSerializerSettings _jsonSetting = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }; return Content(JsonConvert.SerializeObject(dataPoints, _jsonSetting), "application/json"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Web; namespace ASPNET_MVC_Samples.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; } }