Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
  • Register
  • Login
  1. Home
  2. Blogpost

Implementing Pie Chart in Asp.Net MVC

Date- Jul 09,2023

4741

Free Download Pay & Download
Am Charts Pie Chart

Pie Chart in Asp.Net MVC using AM Chart 4

So for implementing charts in asp.net mvc you have to add following library in your view

  <script src="//cdn.amcharts.com/lib/4/core.js"></script>
        <script src="//cdn.amcharts.com/lib/4/charts.js"></script>

Here , you can copy following code to the view for creating charts, here first code we are using to create chart from static  javascript array and the second one will be from server side data. You can copy the following code on the view.

@using Newtonsoft.Json
@model List<PieChart.Controllers.FuelData>
@{
    ViewBag.Title = "Home Page";
}

<main>
    <section class="row" aria-labelledby="aspnetTitle">
        <h1 id="title">ASP.NET AM Charts</h1>



        <script src="//cdn.amcharts.com/lib/4/core.js"></script>
        <script src="//cdn.amcharts.com/lib/4/charts.js"></script>
        <h1 id="title">Pie Chart using Client Side data source</h1>
        <div id="chartdiv" style="width: 100%; height: 400px;"></div>

        <h1 id="title">Pie Chart using Server Side data source</h1>
        <div id="chartdiv2" style="width: 100%; height: 400px;"></div>
        <script>
        // Create chart instance
        var chart = am4core.create("chartdiv", am4charts.PieChart);
         var allTypesData = JSON.parse('@Html.Raw(JsonConvert.SerializeObject(Model))');
        // Add data

        chart.data = [{
            "country": "Lithuania",
            "litres": 501.9
        }, {
            "country": "Czech Republic",
            "litres": 301.9
        }, {
            "country": "Ireland",
            "litres": 201.1
        }, {
            "country": "Germany",
            "litres": 165.8
        }, {
            "country": "Australia",
            "litres": 139.9
        }, {
            "country": "Austria",
            "litres": 128.3
        }, {
            "country": "UK",
            "litres": 99
        }, {
            "country": "Belgium",
            "litres": 60
        }, {
            "country": "The Netherlands",
            "litres": 50
        }];

        // Add and configure Series
        var pieSeries = chart.series.push(new am4charts.PieSeries());
        pieSeries.dataFields.value = "litres";
        pieSeries.dataFields.category = "country";



         var chart2 = am4core.create("chartdiv2", am4charts.PieChart);
         var allTypesData = JSON.parse('@Html.Raw(JsonConvert.SerializeObject(Model))');
        // Add data
        chart2.data = allTypesData;


        // Add and configure Series
        var pieSeries2 = chart2.series.push(new am4charts.PieSeries());
        pieSeries2.dataFields.value = "litres";
        pieSeries2.dataFields.category = "country";
        </script>
    </section>
</main>

Now , if you just want the client side data chart  then you can simply use

  <script>
        // Create chart instance
        var chart = am4core.create("chartdiv", am4charts.PieChart);
       

        chart.data = [{
            "country": "Lithuania",
            "litres": 501.9
        }, {
            "country": "Czech Republic",
            "litres": 301.9
        }, {
            "country": "Ireland",
            "litres": 201.1
        }, {
            "country": "Germany",
            "litres": 165.8
        }];

        // Add and configure Series
        var pieSeries = chart.series.push(new am4charts.PieSeries());
        pieSeries.dataFields.value = "litres";
        pieSeries.dataFields.category = "country";
	</script>	

So , Now for server side you can use following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PieChart.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            List<FuelData> fuelDataList = new List<FuelData>
        {
            new FuelData { country = "USA", litres = 50.5m },
            new FuelData { country = "Canada", litres = 32.2m },
            new FuelData { country = "Germany", litres = 75.9m },
            new FuelData { country = "Australia", litres = 62.4m }
        };
            return View(fuelDataList);
        }

       
    }

    public class FuelData
    {
        public string country { get; set; }
        public decimal litres { get; set; }
    }
}

Now , run the application and you will see pie charts created on your browser

So , this is how we can implement pie chart in Asp.net mvc using AM Charts.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Comments

Tags

Swagger UI
Swashbuckle
SwashbuckleAspNetCore
Rest API
Postman
Api Testing
ITextSharp
Export to Pdf
AspNet Core
AspNet
C#
View to Pdf in Aspnet
Scheduler
Fibonacci series in Java
Display Fibonacci Series
First C# Program
What is C?
C
C Programming
CodeLobster
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, India   info@code2night.com

Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • XML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • PDF Editor
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Built with for developers