Top 10 Most Popular Charts In Angular With .NET Core API

This article explains how to create charts in an Angular applications, e.g., line chart, area chart, bar chart, pie chart, doughnut chart, polar area chart, scatter chart, bubble char, gauge chart, radar chart, mixed charts and other types of charts with .NET Core API and using Visual Studio 2017 IDE, ng2-chart and ng2-google-chart as npm packages.

Angular

This article is my fourth article on Angular; following is the link to my previous articles

Always Use Angular Latest Version with Visual Studio 2017

There are more than a dozen charts available which can be created very quickly in Angular. However, I am going to explain only those charts which are very popular and more commonly used in day to day life. Following is the list of charts which I am going to cover in this article.

  1. Line Chart
  2. Area Chart
  3. Bar Chart
  4. Pie Chart
  5. Doughnut Chart
  6. Polar Area Chart
  7. Scatter Chart
  8. Bubble Chart
  9. Gauge Chart
  10. Radar Chart

Step 1 Create a new Angular app using the inbuilt template of Visual Studio 2017.

Open Visual Studio 2017 >> File >> New >> Project…

Navigate to installed template >> Installed >> Visual C# >> Web >> ASP.NET Core Web Application >> OK >> Select Angular Template >>OK

If you are creating Angular App with Visual Studio 2017 for the first time, then please have a look at the below article which will provide you a more descriptive and pictorial guide to creating your first Angular App with VS 2017.

 

 

I have created it using the default template as described in the preceding link so it will look like,

Angular

Step 2 - Update Angular Version

Right now, I am using the Angular version “5.2.0-rc.0”. If you are not aware of how to upgrade your project to use the latest version of Angular, then you can go to the below link

Always Use Angular Latest Version with Visual Studio 2017.

Following is the complete code for “package.json” file

  1. {  
  2.   "name""AngularCharts",  
  3.   "private"true,  
  4.   "version""0.0.0",  
  5.   "scripts": {  
  6.     "test""karma start ClientApp/test/karma.conf.js"  
  7.   },  
  8.   "devDependencies": {  
  9.     "@angular/animations""^5.2.0-rc.0",  
  10.     "@angular/common""^5.2.0-rc.0",  
  11.     "@angular/compiler""^5.2.0-rc.0",  
  12.     "@angular/compiler-cli""^5.2.0-rc.0",  
  13.     "@angular/core""^5.2.0-rc.0",  
  14.     "@angular/forms""^5.2.0-rc.0",  
  15.     "@angular/http""^5.2.0-rc.0",  
  16.     "@angular/platform-browser""^5.2.0-rc.0",  
  17.     "@angular/platform-browser-dynamic""^5.2.0-rc.0",  
  18.     "@angular/platform-server""^5.2.0-rc.0",  
  19.     "@angular/router""^5.2.0-rc.0",  
  20.     "@ngtools/webpack""^1.9.3",  
  21.     "@types/chai""^4.1.0",  
  22.     "@types/jasmine""^2.8.3",  
  23.     "@types/webpack-env""^1.13.3",  
  24.     "angular2-router-loader""^0.3.5",  
  25.     "angular2-template-loader""^0.6.2",  
  26.     "aspnet-prerendering""^3.0.1",  
  27.     "aspnet-webpack""^2.0.1",  
  28.     "awesome-typescript-loader""^3.4.1",  
  29.     "bootstrap""^3.3.7",  
  30.     "chai""^4.1.2",  
  31.     "css""^2.2.1",  
  32.     "css-loader""^0.28.8",  
  33.     "es6-shim""^0.35.3",  
  34.     "event-source-polyfill""^0.0.12",  
  35.     "expose-loader""^0.7.4",  
  36.     "extract-text-webpack-plugin""^3.0.2",  
  37.     "file-loader""^1.1.6",  
  38.     "html-loader""^0.5.4",  
  39.     "isomorphic-fetch""^2.2.1",  
  40.     "jasmine-core""^2.8.0",  
  41.     "jquery""^3.2.1",  
  42.     "json-loader""^0.5.7",  
  43.     "karma""^2.0.0",  
  44.     "karma-chai""^0.1.0",  
  45.     "karma-chrome-launcher""^2.2.0",  
  46.     "karma-cli""^1.0.1",  
  47.     "karma-jasmine""^1.1.1",  
  48.     "karma-webpack""^2.0.9",  
  49.     "preboot""^6.0.0-beta.1",  
  50.     "raw-loader""^0.5.1",  
  51.     "reflect-metadata""^0.1.10",  
  52.     "rxjs""^5.5.6",  
  53.     "style-loader""^0.19.1",  
  54.     "to-string-loader""^1.1.5",  
  55.     "typescript""^2.6.2",  
  56.     "url-loader""^0.6.2",  
  57.     "webpack""^3.10.0",  
  58.     "webpack-hot-middleware""^2.21.0",  
  59.     "webpack-merge""^4.1.1",  
  60.     "zone.js""^0.8.19",  
  61.     "ng2-charts""^1.6.0",  
  62.     "chart.js""^2.7.1"  
  63.   },  
  64.   "dependencies": {  
  65.     "ng2-google-charts""^3.3.0"  
  66.   }  
  67. }  

Step 3  Install the latest versions of “ng2-charts” and “chart.js”.

Angular

Angular

Alternatively, you can add those packages name manually in “package.json” file, i.e., add following lines in "devDependencies" section.

  • "ng2-charts": "^1.6.0",
  • "chart.js": "^2.7.1",

Now start creating different types of charts one by one.

Line Chart

“A line chart or line graph is a type of chart which displays information as a series of data points called 'markers' connected by straight line segments. It is a basic type of chart common in many fields.”  -- Source Wikipedia

Angular

Area
Chart

“An area chart or area graph displays graphically quantitative data. It is based on the line chart. The area between axis and line are commonly emphasized with colors, textures, and hatchings. Commonly one compares with an area chart two or more quantities.” -- Source Wikipedia.

Angular

As per the definition mentioned above, we can say that line chart and area chart is very similar, and area chart is based on an online chart emphasized with colors, textures, and hatchings. So I will be creating both types of chart on the same component.

Step 1 - Add a new component for Line Chart using the Angular Item templates option available in Visual Studio 2017.

Right click on “component” folder >> Add >> New Item…

or you can use the keyboard shortcut “Ctrl + Shift + A”.

Angular

Check if “Angular Item Templates” is already installed or not.  If not, then please install it and re-open this window after installation. Navigate to Installed >> Visual C# >> ASP.NET Core >> Web >> Angular >> Select Angular Component. Provide the component name and click on Add button.

Angular

Uncheck the options which are not required, as you can see in the following screenshot that I have unchecked the options for test file name and styles file name. So, test file and styles file will not be created.

Angular

Step 2 - Adding .NET Core Web API Controller

As we are doing this with .NET Core, rather than getting sample data from JSON, I will be fetching data from API Service.

Go to API Controller Folder right click on it >> Add >> Controller… >> select “API Controller Empty” >> Provide controller name as “LineChartController” >> Click on Add.

Step 3 - Write a method in .NET Core API Controller to get sample data for a line chart.

If you open the default application, you will notice that it is showing a table for a weather forecast, so let’s use similar type of data for our line and area chart as well. Following is the complete code for “LineChartController.cs”.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using Microsoft.AspNetCore.Mvc;  
  4.   
  5. namespace AngularChartsWithCoreAPI.Controllers  
  6. {  
  7.     [Produces("application/json")]  
  8.     [Route("api/LineChart")]  
  9.     public class LineChartController : Controller  
  10.     {  
  11.         [HttpGet("[action]")]  
  12.         public WeatherForecast GetWeatherForecast()  
  13.         {  
  14.             var weatherList = new List<Weather>();  
  15.             weatherList.Add(new Weather { Label = "New Delhi", Data = new int[] { 6, 19, 6, 21, 7, 15 } });  
  16.             weatherList.Add(new Weather { Label = "New York", Data = new int[] { -8, -6, -1, 2, -7, 6 } });  
  17.             weatherList.Add(new Weather { Label = "Moscow", Data = new int[] { -4, 3, -5, -1, -6, -3 } });  
  18.             weatherList.Add(new Weather { Label = "London", Data = new int[] { 6, 2, 4, 6, 7, 7 } });  
  19.   
  20.             List<string> lineChartLabelsList = new List<string>();  
  21.   
  22.             for (int i = 0; i < 3; i++)  
  23.             {  
  24.                 lineChartLabelsList.AddRange(new string[] { $"{(DayOfWeek)i} High", $"{(DayOfWeek)i} Low" });  
  25.             }  
  26.             return new WeatherForecast { WeatherList = weatherList, ChartLabels = lineChartLabelsList.ToArray() };  
  27.         }  
  28.   
  29.         public class Weather  
  30.         {  
  31.             public int[] Data { get; set; }  
  32.             public string Label { get; set; }  
  33.         }  
  34.   
  35.         public class WeatherForecast  
  36.         {  
  37.             public List<Weather> WeatherList { get; set; }  
  38.             public string[] ChartLabels { get; set; }  
  39.         }  
  40.     }  
  41. }  

Following is the sample JSON data returned from GetWeatherForecast() API method.

  1. {  
  2.     "weatherList": [  
  3.         {  
  4.             "data": [6, 19, 6, 21, 7, 15],  
  5.             "label""New Delhi"  
  6.         },  
  7.         {  
  8.             "data": [-8, -6, -1, 2, -7, 6],  
  9.             "label""New York"  
  10.         },  
  11.         {  
  12.             "data": [-4, 3, -5, -1, -6, -3],  
  13.             "label""Moscow"  
  14.         },  
  15.         {  
  16.             "data": [6, 2, 4, 6, 7, 7],  
  17.             "label""London"  
  18.         }  
  19.     ],  
  20.     "chartLabels": [  
  21.         "Sunday High",  
  22.         "Sunday Low",  
  23.         "Monday High",  
  24.         "Monday Low",  
  25.         "Tuesday High",  
  26.         "Tuesday Low"  
  27.     ]  
  28. }  

Step 4 - Make Changes in line-chart.component.ts

Now go to “line-chart.component.ts” and add following lines

  1. interface Weather {  
  2.     data: Array<number>;  
  3.     label: string;  
  4. }  
  5.   
  6. interface WeatherForecast {  
  7.     weatherList: Weather[];  
  8.     chartLabels: string[];  
  9. }  

And inside the constructor call the API Service. Following is the complete code of “line-chart.component.ts” file.

  1. import { Component, Inject } from '@angular/core';  
  2. import { Http } from '@angular/http';  
  3.   
  4. @Component({  
  5.     selector: 'app-line-chart',  
  6.     templateUrl: './line-chart.component.html',      
  7. })  
  8.   
  9.   
  10. export class LineChartComponent {  
  11.     public weatherForecast: WeatherForecast;  
  12.     public chartLegend: boolean = true;  
  13.     public chartType: string = 'line';  
  14.       
  15.   
  16.     public chartOptions: any = {  
  17.         responsive: true,  
  18.         legend: {  
  19.             position: 'bottom'  
  20.         }  
  21.     };  
  22.   
  23.     constructor(http: Http, @Inject('BASE_URL') baseUrl: string) {  
  24.         http.get(baseUrl + 'api/LineChart/GetWeatherForecast').subscribe(result => {  
  25.             this.weatherForecast = result.json() as WeatherForecast;  
  26.         }, error => console.error(error));  
  27.     }  
  28. }  
  29.   
  30.   
  31. interface Weather {  
  32.     data: Array<number>;  
  33.     label: string;  
  34. }  
  35.   
  36. interface WeatherForecast {  
  37.     weatherList: Weather[];  
  38.     chartLabels: string[];  
  39. }  

line-chart.component.html

  1. <h1>Highest & Lowest Temprature for Next 3 Days</h1>  
  2.   
  3. <div class="row" *ngIf="weatherForecast">  
  4.     <div style="display: block">  
  5.         <canvas baseChart  
  6.                 [datasets]="weatherForecast.weatherList"  
  7.                 [labels]="weatherForecast.chartLabels"  
  8.                 [options]="chartOptions"  
  9.                 [legend]="chartLegend"  
  10.                 [chartType]="chartType"></canvas>  
  11.     </div>  
  12. </div>  

Step 5 - Make changes in app.shared.module.ts file

Go to the file the file “app.shared.module.ts” and do the following highlighted changes.

Angular

Write code for routing and navigation then re-run the application.

Following is the screenshot of the running application.

Angular

As you can see that I have created an area chart (line chart with colors). In case of “ng2-charts”, it is not mandatory to provide custom color. It creates line chart by default filled with color.

Using Custom Color with line chart

Go to the file “line-chart.component.ts” and add following code inside LineChartComponent class.

  1. public chartColors: Array<any> = [  
  2.         {  
  3.   
  4.             backgroundColor: 'rgba(255, 0, 0, 0.2)',  
  5.             borderColor: 'rgba(255,153,51,1)',  
  6.             pointBackgroundColor: 'rgba(0,128,0,1)',  
  7.             pointBorderColor: '#fff',  
  8.             pointHoverBackgroundColor: '#fff',  
  9.             pointHoverBorderColor: 'rgba(148,159,177,0.8)',  
  10.             pointRadius: 8,  
  11.         },  
  12.         {  
  13.   
  14.             backgroundColor: 'rgba(0, 0, 255, 0.4)',  
  15.             borderColor: 'rgba(77,83,96,1)',  
  16.             pointBackgroundColor: 'rgba(77,83,96,1)',  
  17.             pointBorderColor: '#fff',  
  18.             pointHoverBackgroundColor: '#fff',  
  19.             pointHoverBorderColor: 'rgba(77,83,96,1)',  
  20.             lineTension: 0,  
  21.             pointRadius: 10,  
  22.             pointStyle: 'rectRounded'  
  23.         },  
  24.         {  
  25.             backgroundColor: 'green',  
  26.             borderColor: 'rgba(0,128,0,1)',  
  27.             pointBackgroundColor: 'rgba(148,159,177,1)',  
  28.             pointBorderColor: '#fff',  
  29.             pointHoverBackgroundColor: '#fff',  
  30.             pointHoverBorderColor: 'rgba(148,159,177,0.8)',  
  31.             pointStyle: 'rect',  
  32.             pointRadius: 6,  
  33.   
  34.         },  
  35.   
  36.         {  
  37.   
  38.             backgroundColor: 'rgba(255, 255, 0, 0.2)',  
  39.             borderColor: 'rgba(0,0,255,1)',  
  40.             pointBackgroundColor: 'rgba(0,0,255,1)',  
  41.             pointBorderColor: '#fff',  
  42.             pointHoverBackgroundColor: '#fff',  
  43.             pointHoverBorderColor: 'rgba(148,159,177,0.8)',  
  44.             pointStyle: 'triangle',  
  45.             pointRadius: 10,  
  46.         }  
  47.     ];  
Angular

If you do not provide a value for “backgroundColor”, then it will take gray as the default color.

Comment all “backgroundColor” values for each element of “chartColors”. after that it will look like the following screenshot.

Angular

Remove the color from area chat and make it like line chart.

Add following line to each element of “chartColors” to remove background color.

fill: false,

Angular

Bar Chart

“A bar chart or bar graph is a chart or graph that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent. The bars can be plotted vertically or horizontally. A vertical bar chart is sometimes called a line graph.” -- Source Wikipedia.

Angular

We have seen multiple examples of Line Chart and Area Chart in the same way we can create bar chart as well. I am not going to add a new component for bar chart we can use the same data line chart to create a bar chart.

Go to line chart HTML file “\line-chart.component.html” and remove following line from <canvas> tag.

[colors]="chartColors"

Following is the complete HTML code

  1. <div class="row" *ngIf="weatherForecast">  
  2.     <div style="display: block">  
  3.         <canvas baseChart  
  4.                 [datasets]="weatherForecast.weatherList"  
  5.                 [labels]="weatherForecast.chartLabels"  
  6.                 [options]="chartOptions"  
  7.                 [legend]="chartLegend"  
  8.                 [chartType]="chartType"></canvas>  
  9.     </div>  
  10. </div>  

In “\line-chart.component.ts”  replace

public chartType: string = 'line';

With

public chartType: string = 'bar';

Angular

You can see that it is creating bar above the line and below the line as well. If we remove the negative values, then it will create bar only above the line. Let’s change the data from Web API Controller. 

  1. public WeatherForecast GetWeatherForecast()  
  2.         {  
  3.             var weatherList = new List<Weather>();  
  4.             weatherList.Add(new Weather { Label = "New Delhi", Data = new int[] { 6, 19, 6, 21, 7, 15 } });  
  5.             //weatherList.Add(new Weather { Label = "New York", Data = new int[] { -8, -6, -1, 2, -7, 6 } });  
  6.             //weatherList.Add(new Weather { Label = "Moscow", Data = new int[] { -4, 3, -5, -1, -6, -3 } });  
  7.             weatherList.Add(new Weather { Label = "London", Data = new int[] { 6, 2, 4, 6, 7, 7 } });  
  8.   
  9.             List<string> lineChartLabelsList = new List<string>();  
  10.   
  11.             for (int i = 0; i < 3; i++)  
  12.             {  
  13.                 lineChartLabelsList.AddRange(new string[] { $"{(DayOfWeek)i} High", $"{(DayOfWeek)i} Low" });  
  14.             }  
  15.   
  16.             return new WeatherForecast { WeatherList = weatherList, ChartLabels = lineChartLabelsList.ToArray() };  
  17.   
  18.         }  
Angular

Pie Chart

“A pie chart (or a circle chart) is a circular statistical graphic which is divided into slices to illustrate numerical proportion. In a pie chart, the arc length of each slice (and consequently its central angle and area), is proportional to the quantity it represents. While it is named for its resemblance to a pie which has been sliced, there are variations on the way it can be presented.” -- Source Wikipedia.

Angular

Step 1 - Add an API Controller “PieChartController”.

  1. using Microsoft.AspNetCore.Mvc;  
  2. namespace AngularCharts.Controllers  
  3. {  
  4.     [Produces("application/json")]  
  5.     [Route("api/PieChart")]  
  6.     public class PieChartController : Controller  
  7.     {  
  8.         [HttpGet("[action]")]  
  9.         public WorldPopulation GetPopulationData()  
  10.         {  
  11.             WorldPopulation population = new WorldPopulation {  
  12.                 Countries = new string[] { "China""India""USA""Indonesia""Brazil""Other" },  
  13.                 Population = new long[] { 1_376_745_728, 1_296_834_048, 331_026_400, 262_936_096, 218_622_304, 3_997_059_073 } };  
  14.             return population;  
  15.         }  
  16.     }  
  17.   
  18.     public class WorldPopulation  
  19.     {  
  20.         public string[] Countries { get; set; }  
  21.         public long[] Population { get; set; }  
  22.     }  
  23. }  

Sample JSON returned from “~/api/PieChart/GetPopulationData”

  1. {  
  2.     "countries": [  
  3.         "China",  
  4.         "India",  
  5.         "USA",  
  6.         "Indonesia",  
  7.         "Brazil",  
  8.         "Other"  
  9.     ],  
  10.         "population": [  
  11.             1376745728,  
  12.             1296834048,  
  13.             331026400,  
  14.             262936096,  
  15.             218622304,  
  16.             3997059073  
  17.         ]  
  18. }  

Step 2 - Add Angular Component “PieChartComponent”.

pie-chart.component.ts 

  1. import { Component, Inject } from '@angular/core';  
  2. import { Http } from '@angular/http';  
  3.   
  4. @Component({  
  5.     selector: 'app-pie-chart',  
  6.     templateUrl: './pie-chart.component.html',  
  7.     styleUrls: ['./pie-chart.component.css']  
  8. })  
  9.   
  10.   
  11. export class PieChartComponent {  
  12.   
  13.     public worldPopulation: WorldPopulation;  
  14.     public chartType: string = 'pie';  
  15.     public chartName: string = 'Pie Chart';  
  16.   
  17.     constructor(http: Http, @Inject('BASE_URL') baseUrl: string) {  
  18.         http.get(baseUrl + 'api/PieChart/GetPopulationData').subscribe(result => {  
  19.             this.worldPopulation = result.json() as WorldPopulation;  
  20.         }, error => console.error(error));  
  21.     }  
  22.   
  23.      
  24.    public showPieChart() {  
  25.         this.chartType = 'pie';  
  26.  this.chartName = 'Pie Chart';  
  27.     }  
  28.   
  29. }  
  30.   
  31. interface WorldPopulation {  
  32.     countries: Array<string>;  
  33.     population: Array<number>;  
  34. }  

pie-chart.component.html

  1. <h1>World Population Country Wise : {{chartName}}</h1>  
  2. <div class="row" *ngIf="worldPopulation">  
  3.     <div style="display: block">  
  4.         <canvas baseChart  
  5.                 [data]="worldPopulation.population"  
  6.                 [labels]="worldPopulation.countries"  
  7.                 [chartType]="chartType"></canvas>  
  8.     </div>  
  9. </div>  
  10.   
  11. <button (click)="showPieChart()" style="font-size:24px">Pie Chart</button>  

Step 3 - Changes for Routing and Navigation

Make required changes in “app.shared.module.ts” file for routing.

Add code in “navmenu.component.html” for navigation.

Angular

Doughnut Chart

“A doughnut chart (also spelled donut) is a variant of the pie chart, with a blank center allowing for additional information about the data as a whole to be included. Doughnut charts are similar to pie charts in that their aim is to illustrate proportions. This type of circular graph can support multiple statistics at once and it provides a better data intensity ratio to standard pie charts.” -- Source Wikipedia.

Angular

In the “pie-chart.component.ts” add following code

  1. public showDoughnutChart() {  
  2.         this.chartType = 'doughnut';  
  3.         this.chartName = 'Doughnut Chart';  
  4.     }   

In the file “pie-chart.component.html” below line

  1. <button (click)="showDoughnutChart()" style="font-size:24px">Doughnut Chart</button>     

Apart from this no other change is required. It will automatically render a doughnut chart. Following is the screenshot for the same.

Angular

Polar Area Chart

“Polar area charts are similar to pie charts, but each segment has the same angle - the radius of the segment differs depending on the value. This type of chart is often used when we want to show a comparison data similar to a pie chart, but also show a scale of values for context.” -- Source http://www.chartjs.org

Angular

Use the same code of “pie-chart.component.ts” and write following code

  1. public showPolarAreaChart() {  
  2.         this.chartType = 'polarArea';  
  3.         this.chartName = 'Polar Area Chart';  
  4.     }  

In the file “pie-chart.component.html” below line

  1. <button (click)="showPolarAreaChart()" style="font-size:24px">Polar Area Chart</button>  

Apart from this, no other change is required. It will automatically render a polar area chart. Following is the screenshot of the same.

Angular

Scatter Chart

“A scatter plot (also called a scatterplot, scatter graph, scatter chart, scattergram, or scatter diagram) is a type of plot or mathematical diagram using Cartesian coordinates to display values for typically two variables for a set of data. If the points are color-coded, one additional variable can be displayed.” -- Source Wikipedia.

Step 1 Add a .NET Core API Controller

Add a .NET Core API Controller “ScatterChartController” for getting scatter chart data.

ScatterChartController.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using Microsoft.AspNetCore.Mvc;  
  4.   
  5. namespace AngularCharts.Controllers  
  6. {  
  7.     [Produces("application/json")]  
  8.     [Route("api/ScatterChart")]  
  9.     public class ScatterChartController : Controller  
  10.     {  
  11.         [HttpGet("[action]")]  
  12.         public Scatter[] GetScatterChartData()  
  13.         {  
  14.             List<Scatter> scatterList = new List<Scatter>();  
  15.             var rng = new Random();  
  16.             for (int i = 1; i <= 5; i++)  
  17.             {  
  18.                 List<ScatterData> scatterDataList = new List<ScatterData>();  
  19.                 for (int j = 1; j <= 5; j++)  
  20.                 {  
  21.                     scatterDataList.Add(new ScatterData { X = rng.Next(-20, 55), Y = rng.Next(-20, 55) });  
  22.                 }  
  23.                 scatterList.Add(new Scatter { Data = scatterDataList, Label = $"DataSet {i:00}" });  
  24.             }  
  25.             return scatterList.ToArray();  
  26.         }  
  27.     }  
  28.   
  29.     public class Scatter  
  30.     {  
  31.         public List<ScatterData> Data { get; set; }  
  32.         public string Label { get; set; }  
  33.     }  
  34.     public class ScatterData  
  35.     {  
  36.         public int X { get; set; }  
  37.         public int Y { get; set; }  
  38.     }  
  39. }  

Sample JSON returned from “~/api/ScatterChart/ GetScatterChartData”

  1. [  
  2.     {  
  3.         "data": [  
  4.             {  
  5.                 "x": 13,  
  6.                 "y": -15  
  7.             },  
  8.             {  
  9.                 "x": -3,  
  10.                 "y": 28  
  11.             },  
  12.             {  
  13.                 "x": 50,  
  14.                 "y": 39  
  15.             },  
  16.             {  
  17.                 "x": -19,  
  18.                 "y": -12  
  19.             },  
  20.             {  
  21.                 "x": 27,  
  22.                 "y": -20  
  23.             }  
  24.         ],  
  25.         "label""DataSet 01"  
  26.     },  
  27.     {  
  28.         "data": [  
  29.             {  
  30.                 "x": 18,  
  31.                 "y": 45  
  32.             },  
  33.             {  
  34.                 "x": 54,  
  35.                 "y": -6  
  36.             },  
  37.             {  
  38.                 "x": 43,  
  39.                 "y": 25  
  40.             },  
  41.             {  
  42.                 "x": 10,  
  43.                 "y": -16  
  44.             },  
  45.             {  
  46.                 "x": 10,  
  47.                 "y": 25  
  48.             }  
  49.         ],  
  50.         "label""DataSet 02"  
  51.     },  
  52.     {  
  53.         "data": [  
  54.             {  
  55.                 "x": 20,  
  56.                 "y": 1  
  57.             },  
  58.             {  
  59.                 "x": 2,  
  60.                 "y": 49  
  61.             },  
  62.             {  
  63.                 "x": 42,  
  64.                 "y": -13  
  65.             },  
  66.             {  
  67.                 "x": -18,  
  68.                 "y": 16  
  69.             },  
  70.             {  
  71.                 "x": -5,  
  72.                 "y": 44  
  73.             }  
  74.         ],  
  75.         "label""DataSet 03"  
  76.     },  
  77.     {  
  78.         "data": [  
  79.             {  
  80.                 "x": -13,  
  81.                 "y": -9  
  82.             },  
  83.             {  
  84.                 "x": -9,  
  85.                 "y": 15  
  86.             },  
  87.             {  
  88.                 "x": -8,  
  89.                 "y": 29  
  90.             },  
  91.             {  
  92.                 "x": 50,  
  93.                 "y": 2  
  94.             },  
  95.             {  
  96.                 "x": 9,  
  97.                 "y": 45  
  98.             }  
  99.         ],  
  100.         "label""DataSet 04"  
  101.     },  
  102.     {  
  103.         "data": [  
  104.             {  
  105.                 "x": -14,  
  106.                 "y": -16  
  107.             },  
  108.             {  
  109.                 "x": 2,  
  110.                 "y": 2  
  111.             },  
  112.             {  
  113.                 "x": -7,  
  114.                 "y": 32  
  115.             },  
  116.             {  
  117.                 "x": 38,  
  118.                 "y": -14  
  119.             },  
  120.             {  
  121.                 "x": -7,  
  122.                 "y": -20  
  123.             }  
  124.         ],  
  125.         "label""DataSet 05"  
  126.     }  
  127. ]  

Step 2 Add an angular component

Add an angular component “ScatterChartComponent” for creating scatter chart.

scatter-chart.component.ts

  1. import { Component, Inject } from '@angular/core';  
  2. import { Http } from '@angular/http';  
  3.   
  4. @Component({  
  5.     selector: 'app-scatter-chart',  
  6.     templateUrl: './scatter-chart.component.html',  
  7.     styleUrls: ['./scatter-chart.component.css']  
  8. })  
  9.   
  10. export class ScatterChartComponent {  
  11.   
  12.     public scatterData: Scatter[];  
  13.   
  14.     constructor(http: Http, @Inject('BASE_URL') baseUrl: string) {  
  15.         http.get(baseUrl + 'api/ScatterChart/GetScatterChartData').subscribe(result => {  
  16.             this.scatterData = result.json() as Scatter[];  
  17.         }, error => console.error(error));  
  18.     }  
  19.   
  20.     public chartLegend: boolean = true;  
  21.       
  22.     public chartColors: Array<any> = [  
  23.         {  
  24.   
  25.             backgroundColor: 'rgba(0,128,0,1)',  
  26.             borderColor: 'rgba(0,128,0,1)',  
  27.             pointBackgroundColor: 'rgba(0,128,0,1)',  
  28.             pointBorderColor: '#fff',  
  29.             pointHoverBackgroundColor: '#fff',  
  30.             pointHoverBorderColor: 'rgba(148,159,177,0.8)',  
  31.             pointRadius: 8  
  32.         },  
  33.         {  
  34.   
  35.             backgroundColor: 'rgba(77,83,96,1)',  
  36.             borderColor: 'rgba(77,83,96,1)',  
  37.             pointBackgroundColor: 'rgba(77,83,96,1)',  
  38.             pointBorderColor: '#fff',  
  39.             pointHoverBackgroundColor: '#fff',  
  40.             pointHoverBorderColor: 'rgba(77,83,96,1)',  
  41.             lineTension: 0,  
  42.             pointRadius: 10,  
  43.             pointStyle: 'rectRounded'  
  44.         },  
  45.         {  
  46.             backgroundColor: 'rgba(255,99,33,1)',  
  47.             borderColor: 'rgba(255,99,33,1)',  
  48.             pointBackgroundColor: 'rgba(255,99,33,1)',  
  49.             pointBorderColor: '#fff',  
  50.             pointHoverBackgroundColor: '#fff',  
  51.             pointHoverBorderColor: 'rgba(148,159,177,0.8)',  
  52.             pointStyle: 'rect',  
  53.             pointRadius: 12,  
  54.   
  55.         },  
  56.   
  57.         {  
  58.   
  59.             backgroundColor: 'rgba(0,0,255,1)',  
  60.             borderColor: 'rgba(0,0,255,1)',  
  61.             pointBackgroundColor: 'rgba(0,0,255,1)',  
  62.             pointBorderColor: 'rgba(0,0,255,1)',  
  63.             pointHoverBackgroundColor: '#fff',  
  64.             pointHoverBorderColor: 'rgba(148,159,177,0.8)',  
  65.             pointStyle: 'triangle',  
  66.             pointRadius: 10,  
  67.             borderWidth: 2,  
  68.         },  
  69.   
  70.         {  
  71.   
  72.             backgroundColor: 'rgba(255,0,0,1)',  
  73.             borderColor: 'rgba(255,0,0,1)',  
  74.             pointBackgroundColor: 'rgba(255,0,0,1)',  
  75.             pointBorderColor: '#fff',  
  76.             pointHoverBackgroundColor: '#fff',  
  77.             pointHoverBorderColor: 'rgba(148,159,177,0.8)',  
  78.             pointStyle: 'rectRot',  
  79.             pointRadius: 12,  
  80.         }  
  81.     ];  
  82.     public scatterChartType: string = 'scatter';  
  83.     public ScatterchartOptions: any = {  
  84.         responsive: true,  
  85.         legend: {  
  86.             position: 'bottom'  
  87.         },  
  88.   
  89.         scales: {  
  90.             xAxes: [{  
  91.                 type: 'linear',  
  92.                 position: 'bottom'  
  93.             }]  
  94.         }  
  95.     };  
  96. }  
  97.   
  98. interface Scatter {  
  99.     data: Array<ScatterData>;  
  100.     label: string;  
  101. }  
  102.   
  103. interface ScatterData {  
  104.     x: number;  
  105.     y: number;  
  106. }  

scatter-chart.component.html

  1. <h1>Colorful Scatter Chart</h1>  
  2.   
  3. <div style="display: block" *ngIf="scatterData">  
  4.     <canvas baseChart  
  5.             [datasets]="scatterData"              
  6.             [options]="ScatterchartOptions"  
  7.             [legend]="chartLegend"  
  8.             [colors]="chartColors"  
  9.             [chartType]="scatterChartType"></canvas>  
  10. </div>  

Step 3 Changes for Routing and Navigation

Make required changes in “app.shared.module.ts” file for routing.

Add code in “navmenu.component.html” for navigation.

Angular

Bubble Chart

A bubble chart is a variation of a scatter chart in which the data points are replaced with bubbles, and an additional dimension of the data is represented in the size of the bubbles. Just like a scatter chart, a bubble chart does not use a category axis — both horizontal and vertical axes are valued axes. -- Source Wikipedia.

Step 1 Add a .NET Core API Controller

Add a .NET Core API Controller “BubbleChartController” for getting bubble chart data.

BubbleChartController.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using Microsoft.AspNetCore.Mvc;  
  4.   
  5. namespace AngularCharts.Controllers  
  6. {  
  7.     [Produces("application/json")]  
  8.     [Route("api/BubbleChart")]  
  9.     public class BubbleChartController : Controller  
  10.     {  
  11.         [HttpGet("[action]")]  
  12.         public Bubble[] GetBubbleChartData()  
  13.         {  
  14.             List<Bubble> bubbleList = new List<Bubble>();  
  15.   
  16.             var rng = new Random();  
  17.             for (int i = 1; i <= 5; i++)  
  18.             {  
  19.                 List<BubbleData> bubbleDataList = new List<BubbleData>();  
  20.                 for (int j = 1; j <= 5; j++)  
  21.                 {  
  22.                     bubbleDataList.Add(new BubbleData { X = rng.Next(-20, 55), Y = rng.Next(-20, 55), R = rng.Next(2, 20) });  
  23.                 }  
  24.                 bubbleList.Add(new Bubble { Data = bubbleDataList, Label = $"DataSet {i:00}" });  
  25.   
  26.             }  
  27.   
  28.             return bubbleList.ToArray();  
  29.         }  
  30.     }  
  31.   
  32.     public class BubbleData  
  33.     {  
  34.         public int X { get; set; }  
  35.         public int Y { get; set; }  
  36.         public int R { get; set; }  
  37.     }  
  38.   
  39.     public class Bubble  
  40.     {  
  41.         public List<BubbleData> Data { get; set; }  
  42.         public string Label { get; set; }  
  43.     }  
  44. }  

Sample JSON returned from “~/api/BubbleChart/GetBubbleChartData”

  1. [  
  2.     {  
  3.         "data": [  
  4.             {  
  5.                 "x": -13,  
  6.                 "y": 43,  
  7.                 "r": 2  
  8.             },  
  9.             {  
  10.                 "x": -12,  
  11.                 "y": -6,  
  12.                 "r": 5  
  13.             },  
  14.             {  
  15.                 "x": 6,  
  16.                 "y": -19,  
  17.                 "r": 12  
  18.             },  
  19.             {  
  20.                 "x": -16,  
  21.                 "y": -17,  
  22.                 "r": 14  
  23.             },  
  24.             {  
  25.                 "x": 41,  
  26.                 "y": 10,  
  27.                 "r": 7  
  28.             }  
  29.         ],  
  30.         "label""DataSet 01"  
  31.     },  
  32.     {  
  33.         "data": [  
  34.             {  
  35.                 "x": 10,  
  36.                 "y": -1,  
  37.                 "r": 19  
  38.             },  
  39.             {  
  40.                 "x": -3,  
  41.                 "y": 52,  
  42.                 "r": 9  
  43.             },  
  44.             {  
  45.                 "x": 48,  
  46.                 "y": 25,  
  47.                 "r": 14  
  48.             },  
  49.             {  
  50.                 "x": -14,  
  51.                 "y": 33,  
  52.                 "r": 17  
  53.             },  
  54.             {  
  55.                 "x": -5,  
  56.                 "y": 0,  
  57.                 "r": 10  
  58.             }  
  59.         ],  
  60.         "label""DataSet 02"  
  61.     },  
  62.     {  
  63.         "data": [  
  64.             {  
  65.                 "x": 32,  
  66.                 "y": 43,  
  67.                 "r": 14  
  68.             },  
  69.             {  
  70.                 "x": 44,  
  71.                 "y": -3,  
  72.                 "r": 11  
  73.             },  
  74.             {  
  75.                 "x": -19,  
  76.                 "y": 1,  
  77.                 "r": 17  
  78.             },  
  79.             {  
  80.                 "x": 30,  
  81.                 "y": 1,  
  82.                 "r": 17  
  83.             },  
  84.             {  
  85.                 "x": 22,  
  86.                 "y": 6,  
  87.                 "r": 19  
  88.             }  
  89.         ],  
  90.         "label""DataSet 03"  
  91.     },  
  92.     {  
  93.         "data": [  
  94.             {  
  95.                 "x": 29,  
  96.                 "y": 39,  
  97.                 "r": 16  
  98.             },  
  99.             {  
  100.                 "x": 28,  
  101.                 "y": -20,  
  102.                 "r": 4  
  103.             },  
  104.             {  
  105.                 "x": -11,  
  106.                 "y": 30,  
  107.                 "r": 7  
  108.             },  
  109.             {  
  110.                 "x": 53,  
  111.                 "y": -6,  
  112.                 "r": 6  
  113.             },  
  114.             {  
  115.                 "x": 8,  
  116.                 "y": -19,  
  117.                 "r": 10  
  118.             }  
  119.         ],  
  120.         "label""DataSet 04"  
  121.     },  
  122.     {  
  123.         "data": [  
  124.             {  
  125.                 "x": 2,  
  126.                 "y": -9,  
  127.                 "r": 15  
  128.             },  
  129.             {  
  130.                 "x": -10,  
  131.                 "y": 7,  
  132.                 "r": 5  
  133.             },  
  134.             {  
  135.                 "x": 53,  
  136.                 "y": 52,  
  137.                 "r": 5  
  138.             },  
  139.             {  
  140.                 "x": 38,  
  141.                 "y": 8,  
  142.                 "r": 19  
  143.             },  
  144.             {  
  145.                 "x": -11,  
  146.                 "y": 21,  
  147.                 "r": 14  
  148.             }  
  149.         ],  
  150.         "label""DataSet 05"  
  151.     }  
  152. ]  

Step 2 Add an angular component

Add an angular component “BubbleChartComponent” for creating bubble chart.

bubble-chart.component.ts

  1. import { Component, Inject } from '@angular/core';  
  2. import { Http } from '@angular/http';  
  3.   
  4. @Component({  
  5.     selector: 'app-bubble-chart',  
  6.     templateUrl: './bubble-chart.component.html',  
  7.     styleUrls: ['./bubble-chart.component.css']  
  8. })  
  9. /** BubbleChart component*/  
  10. export class BubbleChartComponent {  
  11.   
  12.     public bubbleChartData: Bubble[];  
  13.   
  14.     public bublleChartType: string = 'bubble';  
  15.   
  16.     constructor(http: Http, @Inject('BASE_URL') baseUrl: string) {  
  17.         http.get(baseUrl + 'api/BubbleChart/GetBubbleChartData').subscribe(result => {  
  18.             this.bubbleChartData = result.json() as Bubble[];  
  19.         }, error => console.error(error));  
  20.     }  
  21.   
  22.     public chartColors: Array<any> = [  
  23.         {  
  24.             backgroundColor: 'rgba(0,128,0,1)',  
  25.         },  
  26.         {  
  27.             backgroundColor: 'rgba(77,83,96,1)',  
  28.         },  
  29.         {  
  30.             backgroundColor: 'rgba(255,99,33,1)',  
  31.         },  
  32.   
  33.         {  
  34.             backgroundColor: 'rgba(0,0,255,1)',  
  35.         },  
  36.   
  37.         {  
  38.             backgroundColor: 'maroon',  
  39.         }  
  40.     ];  
  41.   
  42. }  
  43.   
  44.   
  45. interface Bubble {  
  46.     data: Array<BubbleData>;  
  47.     label: string;  
  48. }  
  49.   
  50.   
  51. interface BubbleData {  
  52.     x: number;  
  53.     y: number;  
  54.     r: number;  
  55. }  

bubble-chart.component.html

  1. <div style="display: block" *ngIf="bubbleChartData">  
  2.     <canvas baseChart  
  3.             [datasets]="bubbleChartData"  
  4.             [colors]="chartColors"  
  5.             [chartType]="bublleChartType"></canvas>  
  6. </div>  

Step 3 Changes for Routing and Navigation

Make required changes in “app.shared.module.ts” file for routing.

Add code in “navmenu.component.html” for navigation.

Angular

Gauge Chart

Gauge chart can be created very quickly using ng2-google-charts. So, install package “ng2-google-charts” if it is not installed already.

Alternatively, you can add following code manually in “package.json” file.

  1. "dependencies": {  
  2.     "ng2-google-charts""^3.3.0"  
  3.   }  

For Gauge chart, I am not going to create a Web API Controller. I am going to use JSON data directly. If you wish you can fetch these data from API service as well.

Step 1 Add an angular component

Add an angular component “GaugeChartComponent” for creating gauge chart.

gauge-chart.component.ts

  1. import { Component } from '@angular/core';  
  2.   
  3. @Component({  
  4.     selector: 'app-gauge-chart',  
  5.     templateUrl: './gauge-chart.component.html',  
  6.     styleUrls: ['./gauge-chart.component.css']  
  7. })  
  8.   
  9. export class GaugeChartComponent {  
  10.   
  11.     constructor() {  
  12.   
  13.     }  
  14.   
  15.     guageChartData = {  
  16.         chartType: 'Gauge',  
  17.         dataTable: [  
  18.             ['Label''Value'],  
  19.             ['CPU', 33],  
  20.             ['Memory', 84],  
  21.             ['Disk', 55],  
  22.             ['Network', 5],  
  23.             ['GPU', 3]  
  24.         ],  
  25.         options: {  
  26.             'title''Tasks',  
  27.             'greenFrom': 0,  
  28.             'greenTo': 30,  
  29.             'yellowFrom': 30,  
  30.             'yellowTo': 80,  
  31.             //'yellowColor': '#CCCC00',  
  32.             'redFrom': 80, 'redTo': 100,  
  33.             'max': 100  
  34.         },  
  35.     };  
  36.   
  37.     risk = {  
  38.         chartType: 'Gauge',  
  39.         dataTable: [  
  40.             ['Label''Value'],  
  41.             ['Market Risk', 3.2],              
  42.         ],  
  43.         options: {  
  44.             'title''Market Risk',              
  45.             'max': 5,  
  46.             'greenFrom': 0,  
  47.             'greenTo': 3,  
  48.             'yellowFrom': 3,  
  49.             'yellowTo': 4,  
  50.             'redFrom': 4,  
  51.             'redTo': 5,  
  52.         },  
  53.     };  
  54.   
  55.     risk2 = {  
  56.         chartType: 'Gauge',  
  57.         dataTable: [  
  58.             ['Label''Value'],  
  59.             ['Risk', 1.8],  
  60.         ],  
  61.         options: {  
  62.             'title''Risk',  
  63.             'max': 5,  
  64.             'greenFrom': 0,  
  65.             'greenTo': 3,  
  66.             'yellowFrom': 3,  
  67.             'yellowTo': 4,  
  68.             'redFrom': 4,  
  69.             'redTo': 5,  
  70.             'minorTicks':10,  
  71.         },  
  72.     };  
  73. }  

gauge-chart.component.html

  1. <h1> Google Chart: Guage Chart</h1>  
  2.   
  3. <strong>Task Manager</strong>  
  4. <google-chart [data]="guageChartData"></google-chart>  
  5.   
  6. <br />  
  7. <strong>Risk Meter</strong>  
  8. <table>  
  9.     <tr>  
  10.         <td>  
  11.             <google-chart [data]="risk"></google-chart>  
  12.         </td>  
  13.         <td>  
  14.             <google-chart [data]="risk2"></google-chart>  
  15.         </td>  
  16.     </tr>  
  17. </table>  

Step 2 Changes for Routing and Navigation

Make required changes in “app.shared.module.ts” file for routing.

Angular

Add code in “navmenu.component.html” for navigation.

Angular

Radar Chart

“A radar chart is a graphical method of displaying multivariate data in the form of a two-dimensional chart of three or more quantitative variables represented on axes starting from the same point. The relative position and angle of the axes are typically uninformative.” -- Source Wikipedia.

Angular

Step 1 Add a .NET Core API Controller

Add a .NET Core API Controller “RadarChartController” for getting radar chart data.

RadarChartController.cs

  1. using System.Collections.Generic;  
  2. using Microsoft.AspNetCore.Mvc;  
  3.   
  4. namespace AngularCharts.Controllers  
  5. {  
  6.     [Produces("application/json")]  
  7.     [Route("api/RadarChart")]  
  8.     public class RadarChartController : Controller  
  9.     {  
  10.         [HttpGet("[action]")]  
  11.         public RadarChartData GetRadarChartData()  
  12.         {  
  13.             List<ChartData> data = new List<ChartData>();  
  14.             data.Add(new ChartData { Data = new decimal[] { 18.5m, 17.9m, 4.3m, 3.5m, 2.8m }, Label = "Population %" });  
  15.             data.Add(new ChartData { Data = new decimal[] { 15.1m, 3.15m, 24.9m, 1.31m, 2.75m }, Label = "GDP Nominal %" });  
  16.             string[] Countries = { "China""India""USA""Indonesia""Brazil" };  
  17.             return new RadarChartData { ChartData = data, ChartLabels = Countries, ChartType = "radar" };  
  18.         }  
  19.     }  
  20.   
  21.     public class ChartData  
  22.     {  
  23.         public decimal[] Data { get; set; }  
  24.         public string Label { get; set; }  
  25.     }  
  26.   
  27.     public class RadarChartData  
  28.     {  
  29.         public List<ChartData> ChartData { get; set; }  
  30.         public string[] ChartLabels { get; set; }  
  31.         public string ChartType { get; set; }  
  32.     }  
  33. }  

Step 2 Add an angular component

Add an angular component “RadarChartComponent” for creating a radar chart.

radar-chart.component.ts

  1. import { Component, Inject } from '@angular/core';  
  2. import { Http } from '@angular/http';  
  3.   
  4. @Component({  
  5.     selector: 'app-radar-chart',  
  6.     templateUrl: './radar-chart.component.html',  
  7.     styleUrls: ['./radar-chart.component.css']  
  8. })  
  9.   
  10. export class RadarChartComponent {  
  11.   
  12.     public chart: RadarChartData;  
  13.     constructor(http: Http, @Inject('BASE_URL') baseUrl: string) {  
  14.         http.get(baseUrl + 'api/RadarChart/GetRadarChartData').subscribe(result => {  
  15.             this.chart = result.json() as RadarChartData;  
  16.         }, error => console.error(error));  
  17.     }  
  18. }  
  19.   
  20. interface ChartData {  
  21.     data: Array<number>;  
  22.     label: string;  
  23. }  
  24.   
  25. interface RadarChartData {  
  26.     chartData: ChartData[];  
  27.     chartLabels: string[];  
  28. }  

radar-chart.component.html

  1. <h1> Radar Chart Population(%) & GDP Nominal(%)</h1>  
  2. <div class="row" *ngIf="chart">  
  3.     <div style="display: block;">  
  4.         <canvas baseChart  
  5.                 [datasets]="chart.chartData"  
  6.                 [labels]="chart.chartLabels"  
  7.                 [chartType]="chart.chartType"></canvas>  
  8.     </div>  
  9. </div>  

Step 3 Changes for Routing and Navigation

Make required changes in “app.shared.module.ts” file for routing.

Add code in “navmenu.component.html” for navigation.

Angular

So far, I have explained about the 10 most popular charts with complete code. For convenience, I have also attached the complete project. You can download it and try by yourself.