Implement Google Chart Types In Real Time Scenarios Using ASP.NET MVC

Introduction

Google chart is a better way to visualize the data in your personal Web site.

There is a way to use Google charts with simple JavaScript that you need to embed in your Web page. You use some Google chart libraries, list the data, which will be charted, select options to customize your chart and finally create a chart object with an Id value that you want to select.

Google chart reference photo


Images of Google chart types used in ASP.NET MVC session

Example Of Bubble chart


Example Of Area chart



Example Of Line chart


Example Of Scatter chart

Today, I will tell you how to implement Google chart types used in ASP.NET MVC, using static data.

Step 1

Create one model class file named “Cricket.cs”.

Code Ref

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace SatyaGoogleChartMvc.Models  
  7. {  
  8.     public class CricketModel  
  9.     {  
  10.         public string CricketYear { get; set; }  
  11.         public string CenturyTitle { get; set; }  
  12.         public string HalfCenturyTitle { get; set; }  
  13.         public Cricket CricketData { get; set; }  
  14.     }  
  15.     public class Cricket  
  16.     {  
  17.         public string Year { get; set; }  
  18.         public string Century { get; set; }  
  19.         public string HalfCentury { get; set; }  
  20.     }  
  21. }  

Code description

Here, I have created two classes CricketModel and Cricket.

In Cricket class, I have put some entities through which I will mention some data, using these entities.

  1. public class CricketModel  
  2.     {  
  3.         public string CricketYear { get; set; }  
  4.         public string CenturyTitle { get; set; }  
  5.         public string HalfCenturyTitle { get; set; }  
  6.         public Cricket CricketData { get; set; }  
  7.     }  

CricketModel class contains some entities, using which we can access the data of Cricket class .

  1. public class Cricket  
  2.     {  
  3.         public string Year { get; set; }  
  4.         public string Century { get; set; }  
  5.         public string HalfCentury { get; set; }  
  6.     }  

I mentioned the Cricket Class Reference in CricketModel Class.

  1. public Cricket CricketData { get; set; }  

Here CricketData is the object of Cricket class file through which we can access the properties and the values of the entities of it and assigned to CricketModel class.

Step 2

Subsequently, create one controller class file named “GoogleController.cs” .

Code Ref

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using SatyaGoogleChartMvc.Models;  
  7.   
  8. namespace SatyaGoogleChartMvc.Controllers  
  9. {  
  10.     public class GoogleController : Controller  
  11.     {  
  12.         public ActionResult Index()  
  13.         {  
  14.             ViewBag.Title = "M.S. DHONI GOOGLE SCORE CHART";  
  15.             CricketModel objCricketModel = new CricketModel();  
  16.             objCricketModel.CricketData = new Cricket();  
  17.             objCricketModel.CricketData = CricketChartData();  
  18.             objCricketModel.CricketYear = "Year";  
  19.             objCricketModel.CenturyTitle = "Century";  
  20.             objCricketModel.HalfCenturyTitle = "HalfCentury";  
  21.             return View(objCricketModel);  
  22.         }  
  23.         public Cricket CricketChartData()  
  24.         {  
  25.             Cricket objcricket = new Cricket();  
  26.             objcricket.Year = "2009,2010,2011,2012,2013,2014,2015,2016";  
  27.             objcricket.Century = "20,28,12,24,14,18,19,27";  
  28.             objcricket.HalfCentury = "42,72,22,12,11,34,13,29";  
  29.             return objcricket;  
  30.         }  
  31.     }  
  32. }  

Code description

Here, I have mentioned the reference of model class file by putting namespace.

  1. using SatyaGoogleChartMvc.Models;  

Here, SatyaGoogleChartMvc is the name of ASP.NET MVC project.

Subsequently, I put some controller action method called Index.

  1. public ActionResult Index()  
  2.         {  
  3.             ViewBag.Title = "M.S. DHONI GOOGLE SCORE CHART";  
  4.             CricketModel objCricketModel = new CricketModel();  
  5.             objCricketModel.CricketData = new Cricket();  
  6.             objCricketModel.CricketData = CricketChartData();  
  7.             objCricketModel.CricketYear = "Year";  
  8.             objCricketModel.CenturyTitle = "Century";  
  9.             objCricketModel.HalfCenturyTitle = "HalfCentury";  
  10.             return View(objCricketModel);  
  11.         }  

Here, I used Browser Title text.

  1. ViewBag.Title = "M.S. DHONI GOOGLE SCORE CHART";  

In CricketModel Class, which is defined in models created an object objCricketModel to access the object properties of Cricket Class i.e. CricketData.

  1. CricketModel objCricketModel = new CricketModel();  
  2. objCricketModel.CricketData = new Cricket();  

Using the object of CricketModel Class, we can access one Method PROPERTIES and some Text Reference, which has already been defined in Cricket Class file as Entities.

  1. objCricketModel.CricketData = CricketChartData();  
  2. objCricketModel.CricketYear = "Year";  
  3. objCricketModel.CenturyTitle = "Century";  
  4. objCricketModel.HalfCenturyTitle = "HalfCentury";  

Subsequently, pass the object of CricketModel class inside Index view.

  1. return View(objCricketModel);  

Build a function, using Cricket class.

  1. public Cricket CricketChartData()  
  2.         {  
  3.             Cricket objcricket = new Cricket();  
  4.             objcricket.Year = "2009,2010,2011,2012,2013,2014,2015,2016";  
  5.             objcricket.Century = "20,28,12,24,14,18,19,27";  
  6.             objcricket.HalfCentury = "42,72,22,12,11,34,13,29";  
  7.             return objcricket;  
  8.         }  

Here, I create an object of Cricket class i.e. objcricket.

Using this, we can assign some static data to the entities defined in Cricket class in Models folder.

  1. Cricket objcricket = new Cricket();  
  2. objcricket.Year = "2009,2010,2011,2012,2013,2014,2015,2016";  
  3. objcricket.Century = "20,28,12,24,14,18,19,27";  
  4. objcricket.HalfCentury = "42,72,22,12,11,34,13,29";  
  5. return objcricket;  

Step 3

Create one CSHTML file In Google of Views folder named “Index.cshtml”.

Code Ref

  1. @model SatyaGoogleChartMvc.Models.CricketModel  
  2.   
  3. <title>  
  4.     @ViewBag.Title  
  5. </title>  
  6. <fieldset>  
  7.     <legend style="font-family:Arial;color:blue;font-size:large">M.S. DHONI's 100s and 50s GOOGLE CHART</legend>  
  8.     <script type="text/javascript" src="https://www.google.com/jsapi"></script>  
  9.     <script type="text/javascript">  
  10.   
  11.   
  12.         google.load("visualization""1", { packages: ["corechart"] });  
  13.         google.setOnLoadCallback(drawChart);  
  14.   
  15.         function drawChart() {  
  16.             var Year1 = [@Model.CricketData.Year];  
  17.             var Century1 = [@Model.CricketData.Century];  
  18.             var HalfCentury1 = [@Model.CricketData.HalfCentury];  
  19.   
  20.             var data = new google.visualization.DataTable();  
  21.             data.addColumn('string''@Model.CricketYear');  
  22.             data.addColumn('number''@Model.CenturyTitle');  
  23.             data.addColumn('number''@Model.HalfCenturyTitle');  
  24.             for (i = 0; i < Year1.length; i++) {  
  25.                 data.addRow([Year1[i].toString(), Century1[i], HalfCentury1[i]]);  
  26.             }  
  27.             var options = {  
  28.                 title: 'M.S. DHONI CENTURY AND HALF CENTURY',  
  29.                 hAxis: {  
  30.                     title: '@Model.CricketYear', titleTextStyle: { color: 'blue' }  
  31.                        }  
  32.             };  
  33.   
  34.             //Different chart types >> AreaChart , BubbleChart ,LineChart ,ScatterChart  
  35.             var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));  
  36.             chart.draw(data, options);  
  37.         }       
  38.     </script>  
  39.     <div id="chart_div" style="width: 1100px; height: 700px;">  
  40.     </div>  
  41. </fieldset>  

Code description

Here, I used the reference of CricketModel Class in View CSHTML file.

  1. @model SatyaGoogleChartMvc.Models.CricketModel  

The Browser title text is assigned. It is already mentioned in Controller Class.

  1. <title>  
  2.     @ViewBag.Title  
  3. </title>  

I used one script file that should be responsible to show your data in form of Google chart View.

  1. <script type="text/javascript" src="https://www.google.com/jsapi"></script>  

Again, I have mentioned all class reference entities and style of axis and related properties inside JavaScript tags.

  1. <script type="text/javascript">  
  2.   
  3.   
  4.         google.load("visualization""1", { packages: ["corechart"] });  
  5.         google.setOnLoadCallback(drawChart);  
  6.   
  7.         function drawChart() {  
  8.             var Year1 = [@Model.CricketData.Year];  
  9.             var Century1 = [@Model.CricketData.Century];  
  10.             var HalfCentury1 = [@Model.CricketData.HalfCentury];  
  11.   
  12.             var data = new google.visualization.DataTable();  
  13.             data.addColumn('string''@Model.CricketYear');  
  14.             data.addColumn('number''@Model.CenturyTitle');  
  15.             data.addColumn('number''@Model.HalfCenturyTitle');  
  16.             for (i = 0; i < Year1.length; i++) {  
  17.                 data.addRow([Year1[i].toString(), Century1[i], HalfCentury1[i]]);  
  18.             }  
  19.             var options = {  
  20.                 title: 'M.S. DHONI CENTURY AND HALF CENTURY',  
  21.                 hAxis: {  
  22.                     title: '@Model.CricketYear', titleTextStyle: { color: 'blue' }  
  23.                        }  
  24.             };  
  25.   
  26.             //Different chart types >> AreaChart , BubbleChart ,LineChart ,ScatterChart  
  27.             var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));  
  28.             chart.draw(data, options);  
  29.         }       
  30.     </script>  

Now, load the Visualization API and the corechart package. 

  1. google.load("visualization""1", { packages: ["corechart"] });  

Subsequently, set a callback to run when Google Visualization API is loaded. 

  1. google.setOnLoadCallback(drawChart);  
Callback is the one that creates and populates a data table, instantiates Google chart type, passes the data and draws it.

  1. function
     drawChart() {  
  2.             var Year1 = [@Model.CricketData.Year];  
  3.             var Century1 = [@Model.CricketData.Century];  
  4.             var HalfCentury1 = [@Model.CricketData.HalfCentury];  
  5.   
  6.             var data = new google.visualization.DataTable();  
  7.             data.addColumn('string''@Model.CricketYear');  
  8.             data.addColumn('number''@Model.CenturyTitle');  
  9.             data.addColumn('number''@Model.HalfCenturyTitle');  
  10.             for (i = 0; i < Year1.length; i++) {  
  11.                 data.addRow([Year1[i].toString(), Century1[i], HalfCentury1[i]]);  
  12.             }  

Create the data table. 

  1. var data = new google.visualization.DataTable();  
  2.           data.addColumn('string''@Model.CricketYear');  
  3.           data.addColumn('number''@Model.CenturyTitle');  
  4.           data.addColumn('number''@Model.HalfCenturyTitle');  
  5.           for (i = 0; i < Year1.length; i++) {  
  6.               data.addRow([Year1[i].toString(), Century1[i], HalfCentury1[i]]);  
  7.           }  

Here, I used some CricketModel and Cricket class entities, using @Model to access its properties and values. 

  1. vayr Year1 = [@Model.CricketData.Year];  
  2. data.addColumn('string''@Model.CricketYear');  

Year is the entity of Cricket Class object and CricketYear is the entit of CricketModel class.

Like this, others are defined.

  1. var Year1 = [@Model.CricketData.Year];  
  2.             var Century1 = [@Model.CricketData.Century];  
  3.             var HalfCentury1 = [@Model.CricketData.HalfCentury];  
  4.   
  5.             var data = new google.visualization.DataTable();  
  6.             data.addColumn('string''@Model.CricketYear');  
  7.             data.addColumn('number''@Model.CenturyTitle');  
  8.             data.addColumn('number''@Model.HalfCenturyTitle');  

Here, I have added the data rows in the data table, which is based on the variables assigned in the function defined in script tag.

  1. var Year1 = [@Model.CricketData.Year];  
  2.             var Century1 = [@Model.CricketData.Century];  
  3.             var HalfCentury1 = [@Model.CricketData.HalfCentury];  
  4.   
  5. for (i = 0; i < Year1.length; i++) {  
  6.                 data.addRow([Year1[i].toString(), Century1[i], HalfCentury1[i]]);  
  7.             }  
  8.   
  9.   
  10. Set chart options  
  11.   
  12. var options = {  
  13.                 title: 'M.S. DHONI CENTURY AND HALF CENTURY',  
  14.                 hAxis: {  
  15.                     title: '@Model.CricketYear', titleTextStyle: { color: 'blue' }  
  16.                        }  
  17.             };  

Here, I have put some title of Google chart and Axis text information as year.

  1. title: 'M.S. DHONI CENTURY AND HALF CENTURY',  
  2. hAxis: {  
  3. title: '@Model.CricketYear', titleTextStyle: { color: 'blue' }  

Instantiate and draw our chart, passing in some options.

AreaChart, BubbleChart ,LineChart ,ScatterChart are all Google chart types.

 

  1. var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));  
  2. chart.draw(data, options);  

Div is the one that will hold Google chart type.

  1. <div id="chart_div" style="width: 1100px; height: 700px;">  
  2. </div>  

Step 4

Make Start page at time of First Load.

Code Ref

  1. routes.MapRoute(  
  2.                 name: "Default",  
  3.                 url: "{controller}/{action}/{id}",  
  4.                 defaults: new { controller = "Google", action = "Index", id = UrlParameter.Optional }  
  5.             );  

Code Description

It is Google name of the Controller.

There is Index name of Controller Action Method name.


Output

The URL Is,

http://localhost:61715/Google/Index

For Area chart

It displays the data when hovering over the points.


Shows data


For Bubble chart

It displays the data when hovering over bubbles.


Shows data


For Line chart

It displays the data tooltips when hovering over points.


Shows data


For Scatter chart

Scatter charts points on a graph. When the user hovers over the points, DATA tooltips are displayed with more information.


Shows data


See the Browser Title Bar Text, as mentioned in the code.

  1. ViewBag.Title = "M.S. DHONI GOOGLE SCORE CHART";  


Different types of Google chart are shown earlier, which can be mentioned and changed, which is based on the requirement.

In Index.Cshtml, the code is given below.

  1. var chart = new google.visualization.AreaChart (document.getElementById('chart_div'));  
  2. var chart = new google.visualization.BubbleChart (document.getElementById('chart_div'));  
  3. var chart = new google.visualization.LineChart (document.getElementById('chart_div'));  
  4. var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));  

AreaChart, BubbleChart ,LineChart ,ScatterChart are all Google chart types.

Summary

What is Google Chart?

How To Add Google chart In real time ASP.NET MVC Project.

Different types Of Google charts.

Packages used in Index.Cshtml to access Google charts.


Similar Articles