Pie Chart in Angular 12 using ng2-Charts

Pie chart in angular 12 using ng2-charts example; In this tutorial, i am going to show you how to install ng2-charts and create pie char in angular 12 app.

Pie charts can be used to show percentages of a whole, and represents percentages at a set point in time. Unlike bar graphs and line graphs, pie charts do not show changes over time. The following pages describe the different parts of a pie chart.

Angular /12 Pie Chart Using Ng2-Chart JS

  • Step 1 – Create New Angular App
  • Step 2 – Install Ng2-Charts JS Library
  • Step 3 – Import Modules on App.Module.ts File
  • Step 4 – Add Code on View File
  • Step 5 – Add Code On pie-chart.Component ts File
  • Step 6 – Start the Angular Pie Chart App

Step 1 – Create New Angular App

Execute the following command on terminal to install angular app:

ng new my-new-app

Step 2 – Install Ng2-Charts JS Library

Then install NPM package called ng2-charts chart.js –save for implement Pie chart in angular 11 app. So, You can install the packages by executing the following commands on the terminal:

npm install --save bootstrap

npm install ng2-charts chart.js --save

After that, open angular.json file and update the following code into it:

"styles": [
      "node_modules/bootstrap/dist/css/bootstrap.min.css",
      "src/styles.css"
]

Step 3 – Import Modules on App.Module.ts File

Go to src/directory and open app.module.ts file. And then add the following lines of into app.module.ts file:

import { ChartsModule } from 'ng2-charts';
@NgModule({
  declarations: [...],
  imports: [
    ChartsModule
  ],
  providers: [...],
  bootstrap: [...]
})
export class AppModule { }

Step 4 – Add Code on View File

Create pie chart in angular app. So, visit src/app/ and pie-chart.component.htmland update the following code into it:

<div class="chart-wrapper">
    <canvas baseChart 
    [data]="pieChartData" 
    [labels]="pieChartLabels" 
    [chartType]="pieChartType"
    [options]="pieChartOptions"
    [plugins]="pieChartPlugins"
    [legend]="pieChartLegend">
  </canvas>
</div>

Step 5 – Add Code On pie-chart.Component ts File

Go to src/ directory and open pie-chart.component.ts. Then add the following code into component.ts file:

import { Component } from '@angular/core';
import { ChartType, ChartOptions } from 'chart.js';
import { SingleDataSet, Label, monkeyPatchChartJsLegend, monkeyPatchChartJsTooltip } from 'ng2-charts';
@Component({
  selector: 'app-pie-chart',
  templateUrl: './pie-chart.component.html',
  styleUrls: ['./pie-chart.component.css']
})
export class PieChartComponent {
  public pieChartOptions: ChartOptions = {
    responsive: true,
  };
  public pieChartLabels: Label[] = [['SciFi'], ['Drama'], 'Comedy'];
  public pieChartData: SingleDataSet = [30, 50, 20];
  public pieChartType: ChartType = 'pie';
  public pieChartLegend = true;
  public pieChartPlugins = [];
  constructor() {
    monkeyPatchChartJsTooltip();
    monkeyPatchChartJsLegend();
  }
}

Step 6 – Start the Angular Pie Chart App

Execute the following command on terminal to start angular pie chart app:

ng serve

Recommended Angular Tutorials

Leave a Comment