Stacked Bar 100% Chart is similar to Stacked Bar Chart except that their height is rendered as a percentage of total value at any given point. Given example shows ASP.NET MVC Stacked Bar 100% 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: "Product Market Share", fontColor: "#322D31" }, axisY: { suffix: "%", interval: 20 }, toolTip: { shared: true }, data: [{ type: "stackedBar100", name: "Product 1", showInLegend: true, color: "#7E7D9C", yValueFormatString: "#,##0\"%\"", indexLabel: "{y}", indexLabelFontSize: 17, indexLabelFontColor: "#F4F6F5", dataPoints: @Html.Raw(ViewBag.DataPoints1) }, { type: "stackedBar100", name: "Product 2", showInLegend: true, color: "#ADADC7", yValueFormatString: "#,##0\"%\"", indexLabel: "{y}", indexLabelFontSize: 17, indexLabelFontColor: "#F4F6F5", dataPoints: @Html.Raw(ViewBag.DataPoints2) }, { type: "stackedBar100", name: "Product 3", showInLegend: true, color: "#9897A9", yValueFormatString: "#,##0\"%\"", indexLabel: "{y}", indexLabelFontSize: 17, indexLabelFontColor: "#F4F6F5", dataPoints: @Html.Raw(ViewBag.DataPoints3) }, { type: "stackedBar100", name: "Others", showInLegend: true, color: "#6A6880", yValueFormatString: "#,##0\"%\"", indexLabel: "{y}", indexLabelFontSize: 17, indexLabelFontColor: "#F4F6F5", dataPoints: @Html.Raw(ViewBag.DataPoints4) }] }); chart.render(); } </script> </head> <body> <div id="chartContainer" style="height: 370px; width: 100%;"></div> <script src="https://canvasjs.com/assets/script/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>(); List<DataPoint> dataPoints3 = new List<DataPoint>(); List<DataPoint> dataPoints4 = new List<DataPoint>(); dataPoints1.Add(new DataPoint("2015", 41)); dataPoints1.Add(new DataPoint("2016", 25)); dataPoints1.Add(new DataPoint("2017", 20)); dataPoints2.Add(new DataPoint("2015", 29)); dataPoints2.Add(new DataPoint("2016", 17)); dataPoints2.Add(new DataPoint("2017", 33)); dataPoints3.Add(new DataPoint("2015", 11)); dataPoints3.Add(new DataPoint("2016", 19)); dataPoints3.Add(new DataPoint("2017", 21)); dataPoints4.Add(new DataPoint("2015", 19)); dataPoints4.Add(new DataPoint("2016", 39)); dataPoints4.Add(new DataPoint("2017", 26)); ViewBag.DataPoints1 = JsonConvert.SerializeObject(dataPoints1); ViewBag.DataPoints2 = JsonConvert.SerializeObject(dataPoints2); ViewBag.DataPoints3 = JsonConvert.SerializeObject(dataPoints3); ViewBag.DataPoints4 = JsonConvert.SerializeObject(dataPoints4); 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; } }
toolTipContent property can be used to customize the info shown in the toolTip. Some other commonly used customization options include shared(toolTip), indexLabel, showInLegend, etc.