Pyramid Charts are used to visualize data that are hierarchical in nature, indicating a progressive order. Given example shows ASP.NET MVC Pyramid 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, theme: "light2", title: { text: "Sales via Advertisement" }, legend: { horizontalAlign: "right", verticalAlign: "center" }, data: [{ type: "pyramid", showInLegend: true, legendText: "{label}", indexLabel: "{label} - {y}", toolTipContent: "<b>{label}</b>: {y} <b>({percentage}%)</b>", dataPoints: @Html.Raw(ViewBag.DataPoints) }] }); calculatePercentage(); chart.render(); function calculatePercentage() { var dataPoint = chart.options.data[0].dataPoints; var total = dataPoint[0].y; for (var i = 0; i < dataPoint.length; i++) { if (i == 0) { chart.options.data[0].dataPoints[i].percentage = 100; } else { chart.options.data[0].dataPoints[i].percentage = ((dataPoint[i].y / total) * 100).toFixed(2); } } } } </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> dataPoints = new List<DataPoint>(); dataPoints.Add(new DataPoint("Impressions", 2700)); dataPoints.Add(new DataPoint("Clicked", 1968)); dataPoints.Add(new DataPoint("Free Downloads", 1700)); dataPoints.Add(new DataPoint("Purchase", 450)); dataPoints.Add(new DataPoint("Renewal", 200)); ViewBag.DataPoints = JsonConvert.SerializeObject(dataPoints); 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; } }
You can enable legends for each data point by setting showInLegend to true. The text shown in the legends can also be changed using the legendText property. Some other frequently used customization options are neckHeight, neckWidth, valueRepresents, etc.
Note For step by step instructions, follow our ASP.NET MVC Integration Tutorial