Stacked Column Chart is formed by stacking multiple data-series, one on top of the other. Stacked Column Charts are also referred as Vertical Stacked Bar Chart. Given example shows ASP.NET MVC Stacked Column Chart along with source code that you can try running locally.
@{ Layout = null; } <!DOCTYPE HTML> <html> <head> <script> window.onload = function () { var chart = new CanvasJS.Chart("chartContainer", { animationEnabled: true, title: { text: "Demographics of a High School Marching Band" }, toolTip: { shared: true }, axisY: { title: "No. of Students" }, legend: { cursor: "pointer", verticalAlign: "center", horizontalAlign: "right", itemclick: toggleDataSeries }, data: [{ type: "stackedColumn", name: "Girls", showInLegend: "true", dataPoints: @Html.Raw(ViewBag.DataPoints1) }, { type: "stackedColumn", name: "Boys", showInLegend: "true", dataPoints: @Html.Raw(ViewBag.DataPoints2) }] }); chart.render(); function toggleDataSeries(e) { if (typeof (e.dataSeries.visible) === "undefined" || e.dataSeries.visible) { e.dataSeries.visible = false; } else { e.dataSeries.visible = true; } 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.Collections.Generic; using System.Web.Mvc; namespace ASPNET_MVC_ChartsDemo.Controllers { public class HomeController : Controller { // GET: Home public ActionResult Index() { List<DataPoint> dataPoints1 = new List<DataPoint>(); List<DataPoint> dataPoints2 = new List<DataPoint>(); dataPoints1.Add(new DataPoint("Drums", 2)); dataPoints1.Add(new DataPoint("Trombone", 3)); dataPoints1.Add(new DataPoint("Trumpet", 3)); dataPoints1.Add(new DataPoint("Tuba", 1)); dataPoints2.Add(new DataPoint("Drums", 3)); dataPoints2.Add(new DataPoint("Trombone", 6)); dataPoints2.Add(new DataPoint("Trumpet", 4)); dataPoints2.Add(new DataPoint("Tuba", 3)); ViewBag.DataPoints1 = JsonConvert.SerializeObject(dataPoints1); ViewBag.DataPoints2 = JsonConvert.SerializeObject(dataPoints2); return View(); } } }
using System; using System.Runtime.Serialization; namespace ASPNET_MVC_ChartsDemo.Models { //DataContract for Serializing Data - required to serve in JSON format [DataContract] public class DataPoint { public DataPoint(string label, double y) { this.Label = label; this.Y = y; } //Explicitly setting the name to be used while serializing to JSON. [DataMember(Name = "label")] public string Label = ""; //Explicitly setting the name to be used while serializing to JSON. [DataMember(Name = "y")] public Nullable<double> Y = null; } }
A common toolTip can be shown for all the dataSeries by setting shared property in toolTip object to true. Some other commonly used customization options include indexLabel, dataPointWidth, animationEnabled, etc.
Note For step by step instructions, follow our ASP.NET MVC Integration Tutorial