Introduction
This article is all about how to use your data from various resources and generate ng2-charts using Angular 10. In this article, I am using simple data and ng2-charts for creating charts.
Prerequisites
- We should have a basic knowledge of Angular 2 and above.
- Visual Studio Code can be installed
Implementation
We have different open sources to create charts in Angular 2 and above. Here I am going to use ng2-charts. These charts having a very simple configuration and adopted for all the screens.
Installation
- Create a new angular project using the below command
ng new angchar - install bootstrap using the below command
npm install --save bootstrap - Install ng2-charts using npm
npm install --save ng2-charts - You need to install and include the Chart.js library in your application (it is a peer dependency of this library) (more info can be found in the official chart.js
Install Chart.js library
npm install --save chart.js
Now open the newly created angular application .then double click app.module.ts and type below line of codes
- import { BrowserModule } from'@angular/platform-browser';
- import { NgModule} from'@angular/core';
- import { HttpClientModule } from'@angular/common/http';
- import { AppComponent } from'./app.component';
- import { ChartsModule } from'ng2-charts';
- @NgModule({
- declarations: [
- AppComponent,
- MychartComponent
- ],
- imports: [
- BrowserModule, HttpClientModule, ChartsModule
- ],
- providers: [],
- bootstrap: [AppComponent]
- })
- exportclassAppModule {}
Now create one new component for the chart to implementation by type the below command in terminal
ng g c Mychart
Once new chart component is created include in the app.module.js
- import { MychartComponent } from'./mychart/mychart.component';
Here I am going to use Pie ,bar &doughnut chart for demo. So open mychart.component.html, paste the below lines of code. I will explain each line.
- <hr>
- <divclass="row">
- <divclass="column">
- <divclass="card">
- <divclass="box">
- <divid="chart-container">
- <canvasbaseChart[chartType]="chartType_a"[data]="chartData_a"[labels]="chartLabels_a"
- [options]="chartOptions_a">
- </canvas>
- </div>
- </div>
- </div>
- </div>
- <divclass="column">
- <divclass="card">
- <divclass="box">
- <divid="chart-container">
- <canvasbaseChart[chartType]="chartType_b"[data]="chartData_b"[labels]="chartLabels_b"
- [options]="chartOptions_b">
- </canvas>
- </div>
- </div>
- </div>
- </div>
- <divclass="column">
- <divclass="card">
- <divclass="box">
- <divid="chart-container">
- <canvasbaseChart[chartType]="chartType_c"[data]="chartData_c"[labels]="chartLabels_c"
- [options]="chartOptions_c">
- </canvas>
- </div>
- </div>
- </div>
- </div>
- </div>
Here the below line of code is mainly creating the chart.
- <canvasbaseChart[chartType]="chartType_a"[data]="chartData_a"[labels]="chartLabels_a"
- [options]="chartOptions_a">
- </canvas>
In mychartcomponent.ts file – write the below line of codes


Join the conversation! Your thoughts help the community grow.