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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace SatyaGoogleChartMvc.Models
- {
- public class CricketModel
- {
- public string CricketYear { get; set; }
- public string CenturyTitle { get; set; }
- public string HalfCenturyTitle { get; set; }
- public Cricket CricketData { get; set; }
- }
- public class Cricket
- {
- public string Year { get; set; }
- public string Century { get; set; }
- public string HalfCentury { get; set; }
- }
- }
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.
- public class CricketModel
- {
- public string CricketYear { get; set; }
- public string CenturyTitle { get; set; }
- public string HalfCenturyTitle { get; set; }
- public Cricket CricketData { get; set; }
- }
CricketModel class contains some entities, using which we can access the data of Cricket class .
- public class Cricket
- {
- public string Year { get; set; }
- public string Century { get; set; }
- public string HalfCentury { get; set; }
- }
I mentioned the Cricket Class Reference in CricketModel Class.
- 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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using SatyaGoogleChartMvc.Models;
- namespace SatyaGoogleChartMvc.Controllers
- {
- public class GoogleController : Controller
- {
- public ActionResult Index()
- {
- ViewBag.Title = "M.S. DHONI GOOGLE SCORE CHART";
- CricketModel objCricketModel = new CricketModel();
- objCricketModel.CricketData = new Cricket();
- objCricketModel.CricketData = CricketChartData();
- objCricketModel.CricketYear = "Year";
- objCricketModel.CenturyTitle = "Century";
- objCricketModel.HalfCenturyTitle = "HalfCentury";
- return View(objCricketModel);
- }
- public Cricket CricketChartData()
- {
- Cricket objcricket = new Cricket();
- objcricket.Year = "2009,2010,2011,2012,2013,2014,2015,2016";
- objcricket.Century = "20,28,12,24,14,18,19,27";
- objcricket.HalfCentury = "42,72,22,12,11,34,13,29";
- return objcricket;
- }
- }
- }
Code description
Here, I have mentioned the reference of model class file by putting namespace.
- using SatyaGoogleChartMvc.Models;
Here, SatyaGoogleChartMvc is the name of ASP.NET MVC project.
Subsequently, I put some controller action method called Index.
- public ActionResult Index()
- {
- ViewBag.Title = "M.S. DHONI GOOGLE SCORE CHART";
- CricketModel objCricketModel = new CricketModel();
- objCricketModel.CricketData = new Cricket();
- objCricketModel.CricketData = CricketChartData();
- objCricketModel.CricketYear = "Year";
- objCricketModel.CenturyTitle = "Century";
- objCricketModel.HalfCenturyTitle = "HalfCentury";
- return View(objCricketModel);
- }
Here, I used Browser Title text.
- 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.
- CricketModel objCricketModel = new CricketModel();
- 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.
- objCricketModel.CricketData = CricketChartData();
- objCricketModel.CricketYear = "Year";
- objCricketModel.CenturyTitle = "Century";
- objCricketModel.HalfCenturyTitle = "HalfCentury";
Subsequently, pass the object of CricketModel class inside Index view.
- return View(objCricketModel);
Build a function, using Cricket class.
- public Cricket CricketChartData()
- {
- Cricket objcricket = new Cricket();
- objcricket.Year = "2009,2010,2011,2012,2013,2014,2015,2016";
- objcricket.Century = "20,28,12,24,14,18,19,27";
- objcricket.HalfCentury = "42,72,22,12,11,34,13,29";
- return objcricket;
- }
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.
- Cricket objcricket = new Cricket();
- objcricket.Year = "2009,2010,2011,2012,2013,2014,2015,2016";
- objcricket.Century = "20,28,12,24,14,18,19,27";
- objcricket.HalfCentury = "42,72,22,12,11,34,13,29";
- return objcricket;
Step 3
Create one CSHTML file In Google of Views folder named “Index.cshtml”.
Code Ref
- @model SatyaGoogleChartMvc.Models.CricketModel
- <title>
- @ViewBag.Title
- </title>
- <fieldset>
- <legend style="font-family:Arial;color:blue;font-size:large">M.S. DHONI's 100s and 50s GOOGLE CHART</legend>
- <script type="text/javascript" src="https://www.google.com/jsapi"></script>
- <script type="text/javascript">
- google.load("visualization", "1", { packages: ["corechart"] });
- google.setOnLoadCallback(drawChart);
- function drawChart() {
- var Year1 = [@Model.CricketData.Year];
- var Century1 = [@Model.CricketData.Century];
- var HalfCentury1 = [@Model.CricketData.HalfCentury];
- var data = new google.visualization.DataTable();
- data.addColumn('string', '@Model.CricketYear');
- data.addColumn('number', '@Model.CenturyTitle');
- data.addColumn('number', '@Model.HalfCenturyTitle');
- for (i = 0; i < Year1.length; i++) {
- data.addRow([Year1[i].toString(), Century1[i], HalfCentury1[i]]);
- }
- var options = {
- title: 'M.S. DHONI CENTURY AND HALF CENTURY',
- hAxis: {
- title: '@Model.CricketYear', titleTextStyle: { color: 'blue' }
- }
- };
- //Different chart types >> AreaChart , BubbleChart ,LineChart ,ScatterChart
- var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
- chart.draw(data, options);
- }
- </script>
- <div id="chart_div" style="width: 1100px; height: 700px;">
- </div>
- </fieldset>











Raees KhanPosted Feb 15, 2017, 11:54 PM
Good show, is your application show all chart without internet ?