ASP.NET MVC Pie Chart Sample using CanvasJS

Here is an example for adding Interactive Pie Charts into your ASP.NET MVC Application using CanvasJS. Pie Chart renders itself with a beautiful animation. It allows you customize various properties like color of individual slices, label content and its position, starting angle, etc.

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: "pie",
    dataPoints: [
        { y: 6, label: "Apple" },
	{ y: 4, label: "Mango" },
	{ y: 5, 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>Pie Chart</title>
</head>
<body>
    <div id="chartContainer"></div>
    <script type="text/javascript">
        window.onload = function () {
            var chart = new CanvasJS.Chart("chartContainer", {
                title: {
                    text: "Desktop Search Engine Market Share, Jul-2016"
                },
                animationEnabled: true,
                legend: {
                    verticalAlign: "center",
                    horizontalAlign: "left",
                    fontSize: 20,
                    fontFamily: "Helvetica"
                },
                theme: "light2",
                data: [
                {
                    type: "pie",
                    indexLabelFontFamily: "Garamond",
                    indexLabelFontSize: 20,
                    indexLabel: "{label} {y}%",
                    startAngle: -20,
                    showInLegend: true,
                    toolTipContent: "{legendText} {y}%",
                    dataPoints: [
                        { y: 72.48, legendText: "Google", label: "Google" },
                        { y: 10.39, legendText: "Bing", label: "Bing" },
                        { y: 7.78, legendText: "Yahoo!", label: "Yahoo!" },
                        { y: 7.14, legendText: "Baidu", label: "Baidu" },
                        { y: 0.22, legendText: "Ask", label: "Ask" },
                        { y: 0.15, legendText: "AOL", label: "AOL" },
                        { y: 1.84, legendText: "Others", label: "Others" }
                    ]
                }
                ]
            });
            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 Pie()
        {
            return View();
        }
    }
}

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