ASP.NET MVC Stacked Column Chart Sample using CanvasJS

Here is an example for adding Interactive Stacked Column Charts into your ASP.NET MVC Application using CanvasJS. Stacked Column Charts show relation between individual values to the total sum. Here the data points from different series are stacked one on top of the other instead of placing them side-by-side like in standard multi-series chart.

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.


Download ASP.Net MVC Chart Samples
var chart = new CanvasJS.Chart("chartContainer", {
.
.
data: [{
    type: "stackedColumn",
    dataPoints: [
        { y: 6, label: "Apple" },
	{ y: 4, label: "Mango" },
	{ y: 5, label: "Orange" },
    ]
},
{
    type: "stackedColumn",
    dataPoints: [
        { y: 2, label: "Apple" },
	{ y: 6, label: "Mango" },
	{ y: 4, label: "Orange" },
    ]
}]
.
.
}

You can further customize these charts to enable features like Zooming, Panning, Exporting, etc. To know more about the available features, please refer to our getting started section.


@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <script type="text/javascript" src="https://cdn.canvasjs.com/canvasjs.min.js"></script>
    <title>Stacked Column Chart</title>
</head>
<body>
    <div id="chartContainer"></div>

    <script type="text/javascript">
        window.onload = function () {
            var chart = new CanvasJS.Chart("chartContainer", {
                animationEnabled: true,
                title: {
                    text: "Coal Reserves of Countries"
                },
                axisY: {
                    title: "Coal (bn tonnes)",
                    valueFormatString: "#0.#,.",
                },
                data: [
                {
                    type: "stackedColumn",
                    legendText: "Anthracite & Bituminous",
                    yValueFormatString: "#0.#,.",
                    showInLegend: "true",
                    dataPoints: [
                        { y: 111338, label: "USA" },
                        { y: 49088, label: "Russia" },
                        { y: 62200, label: "China" },
                        { y: 90085, label: "India" },
                        { y: 38600, label: "Australia" },
                        { y: 48750, label: "SA" }
                    ],

                    //dataPoints: @Html.Raw(ViewBag.DataPoints),
                }, {
                    type: "stackedColumn",
                    legendText: "SubBituminous & Lignite",
                    showInLegend: "true",
                    indexLabel: "#total bn",
                    yValueFormatString: "#0.#,.",
                    indexLabelFormatString: "#0.#,.",
                    indexLabelPlacement: "outside",
                    dataPoints: [
                        { y: 135305, label: "USA" },
                        { y: 107922, label: "Russia" },
                        { y: 52300, label: "China" },
                        { y: 3360, label: "India" },
                        { y: 39900, label: "Australia" },
                        { y: 0, label: "SA" }
                    ],

                    //dataPoints: @Html.Raw(ViewBag.DataPoints),
                }
                ]
            });
            chart.render();
        };
    </script>
</body>
</html>
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
    {
        public ActionResult StackedColumn()
        {
            return View();
        }
    }
}

If you have any questions, please feel free to ask in our forums.Ask Question