Creating Stacked Column Chart With ASP.NET Core MVC Using Google Charts API

In this article, you will learn how to create a stacked column chart with ASP.NET Core MVC using Google Charts API.

Introduction

 
In this article, I will implement a stacked column chart in ASP.NE Core 2.1 MVC application using Google Charts API. I want to create a stacked chart to show the year-wise population data in the city.
 

Google Charts API

 
It is a freely available powerful tool for creating charts and it is very easy to implement. It gives many features with different types of charts.
 
I am using this Google API - <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
 
Prerequisites
  1. Install Visual studio 2017 updated any version
  2. Install .Net core SDK 2.1 or above
Step 1- Create an ASP.NET Core 2.1 MVC Project
 
Open Visual Studio and select File -> New -> Project.
 
After selecting the project, a "New Project" dialog will be opened. Select .NET Core inside the Visual C# menu from the left side panel.
 
Then, select “ASP.NET Core web application“ from the available project types. Then, put the name of the project as StackedChartwithCoreMVCDemo and press OK.
 
Note
StackedChartwithCoreMVCDemo is my project name.
 
Creating Stacked Column Chart With ASP.NET Core MVC Using Google Chart API
 
After clicking on the OK button, a new dialog will open to select the project template. You can see two drop-down menus at the top left of the template window. Select “.NET Core” and “ ASP.NET Core 2.1” from these dropdowns. Then, select “ Web application (Model-View-Controller)” template and press OK to create the ASP.NET Core MVC project.
 
Creating Stacked Column Chart With ASP.NET Core MVC Using Google Chart API
 
Step 2
 
Add a new class to the Models folder as “PopulationModel” and implement properties. 
  1. namespace StackedChartwithCoreMVCDemo.Models  
  2. {  
  3.     public class PopulationModel  
  4.     {  
  5.         public string CityName { getset; }  
  6.         public int PopulationYear2020 { getset; }  
  7.         public int PopulationYear2010 { getset; }  
  8.         public int PopulationYear2000 { getset; }  
  9.         public int PopulationYear1990 { getset; }  
  10.   
  11.     }  
  12. }  
Step 3
 
Add a new class to the Models folder for data access, named as “PopulationDataAccessaLayer” and add the following code. 
  1. namespace StackedChartwithCoreMVCDemo.Models  
  2. {  
  3.     public class PopulationDataAccessaLayer  
  4.     {  
  5.         public static List<PopulationModel> GetCityPopulationList()  
  6.         {  
  7.             var list = new List<PopulationModel>();  
  8.             list.Add(new PopulationModel { CityName = "PURI", PopulationYear2020 = 28000, PopulationYear2010 = 45000, PopulationYear2000 = 22000, PopulationYear1990 = 50000 });  
  9.             list.Add(new PopulationModel { CityName = "Bhubaneswar", PopulationYear2020 = 30000, PopulationYear2010 = 49000, PopulationYear2000 = 24000, PopulationYear1990 = 39000 });  
  10.             list.Add(new PopulationModel { CityName = "Cuttack", PopulationYear2020 = 35000, PopulationYear2010 = 56000, PopulationYear2000 = 26000, PopulationYear1990 = 41000 });  
  11.             list.Add(new PopulationModel { CityName = "Berhampur", PopulationYear2020 = 37000, PopulationYear2010 = 44000, PopulationYear2000 = 28000 , PopulationYear1990 = 48000 });  
  12.             list.Add(new PopulationModel { CityName = "Odisha", PopulationYear2020 = 40000, PopulationYear2010 = 38000, PopulationYear2000 = 30000 , PopulationYear1990 = 54000 });  
  13.   
  14.             return list;  
  15.   
  16.         }  
  17.     }  
  18. }  
Step 4
 
Add a new controller to the Controllers folder and name it as “StackedChart”.
 
Create a new Acton Result to display the data in the View and load the Stacked Column chart. 
  1. namespace StackedChartwithCoreMVCDemo.Controllers  
  2. {  
  3.     public class StackedChartController : Controller  
  4.     {  
  5.         // GET: /<controller>/  
  6.         public IActionResult Index()  
  7.         {  
  8.             return View();  
  9.         }  
  10.         [HttpGet]  
  11.         public JsonResult PopulationChart()  
  12.         {  
  13.             var populationList = PopulationDataAccessaLayer.GetCityPopulationList();  
  14.             return Json(populationList);  
  15.         }  
  16.     }  
  17. }  
Step 5
 
Add a new folder in the Views folder and name that as”StackedChart” and add a new View page in the “StackedChart” folder as ”Index.Cshtml”. Add this below code on the Index.cshtml page 
  1. <title>@ViewData["Title"] - DemoGoogleChart</title>  
  2. <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>  

  3. <div id="chart_div"></div>  
  4. <script type="text/javascript">  
  5.   
  6.     google.charts.load('current', {  
  7.         packages: ['corechart''bar']  
  8.     });  
  9.     google.charts.setOnLoadCallback(LoadData);  
  10.   
  11.     function LoadData() {  
  12.         $.ajax({  
  13.               
  14.             url: 'StackedChart/PopulationChart',  
  15.             dataType: "json",  
  16.             type: "GET",  
  17.             error: function(xhr, status, error) {  
  18.                 var err = eval("(" + xhr.responseText + ")");  
  19.                 toastr.error(err.message);  
  20.             },  
  21.             success: function(data) {  
  22.                 PopulationChart(data);  
  23.                 return false;  
  24.             }  
  25.         });  
  26.         return false;  
  27.     }  
  28.   
  29.     function PopulationChart(data) {  
  30.         var dataArray = [  
  31.             ['City''2020 Population''2010 Population''2000 Population''1990 Population']  
  32.         ];  
  33.         $.each(data, function(i, item) {  
  34.             dataArray.push([item.cityName, item.populationYear2020, item.populationYear2010, item.populationYear2000, item.populationYear1990]);  
  35.         });  
  36.         var data = google.visualization.arrayToDataTable(dataArray);  
  37.         var options = {  
  38.             title: 'Population of Largest Cities of Odisha ',  
  39.   
  40.             is3D: true,  
  41.             isStacked: true,  
  42.   
  43.             chartArea: {  
  44.                 width: '50%'  
  45.             },  
  46.             colors: ['#b0120a''#7b1fa2''#ffab91''#d95f02'],  
  47.             hAxis: {  
  48.                 title: 'City',  
  49.                 minValue: 0  
  50.             },  
  51.             vAxis: {  
  52.                 title: 'Total Population'  
  53.             }  
  54.         };  
  55.         var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));  
  56.         
  57.         chart.draw(data, options);  
  58.         return false;  
  59.     }  
  60. </script>  
Step 6 - Add a new menu
 
Edit the Views/Shared/_Layout page and add a new menu as “stacked Chart”. For that, add the below code.
  1. <div class="navbar-collapse collapse">  
  2.                 <ul class="nav navbar-nav">  
  3.                     <li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>  
  4.                     <li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li>  
  5.                     <li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li>  
  6.   
  7.                     <li><a asp-area="" asp-controller="StackedChart" asp-action="Index">Stacked Chart</a></li>  
  8.                 </ul>  
  9.             </div>  
Step 7
 
Now, run the application. You will successfully see the Stacked Column chart as below.
 
Creating Stacked Column Chart With ASP.NET Core MVC Using Google Chart API