Saturday, November 16, 2024
Google search engine
HomeLanguagesAngularAngular 14 Doughnut Chart Example Tutorial

Angular 14 Doughnut Chart Example Tutorial

Angular 14 doughnut chart using charts js; In this tutorial, you will learn how to create doughnut chart using ng2-charts js library in the angular 14 apps.

Angular 14 Doughnut Chart Example Tutorial

Use the following steps to create doughnut chart in angular 14 apps; as follows:

  • Step 1 – Create New Angular App
  • Step 2 – Install Charts JS Library
  • Step 3 – Import-Module in Module.ts File
  • Step 4 – Create Doughnut Chart in View File
  • Step 5 – Import Components in Component ts File
  • Step 6 – Start the Angular Doughnut Chart App

Step 1 – Create New Angular App

First of all, open your terminal and execute the following command on it to install angular app:

ng new my-new-app

Step 2 – Install Charts JS Library

Then install NPM package called ng2-charts chart.js –save for implement doughnut chart in angular 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-Module in Module.ts File

In this step, visit src/app directory and open app.module.ts file. And then add the following lines of into app.module.ts file:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
  
import { AppComponent } from './app.component';
import { ChartsModule } from 'ng2-charts';
  
@NgModule({
  imports:      [ BrowserModule, FormsModule, ChartsModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

Step 4 – Create Doughnut Chart in View File

In this step, create doughnut chart in angular app. So, visit src/app/ and app.component.htmland update the following code into it:

<h1>Angular 13 doughnut chart example - Tutsmake.com</h1>
   
<div style="display: block;">
  <canvas baseChart 
    [data]="doughnutChartData"
    [labels]="doughnutChartLabels"
    [chartType]="doughnutChartType">
  </canvas>
</div>

Step 5 – Import Components in Component ts File

In this step, visit the src/app directory and open app.component.ts. Then add the following code into component.ts file:

import { Component, OnInit } from '@angular/core';
import { ChartType } from 'chart.js';
import { MultiDataSet, Label } from 'ng2-charts';
  
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
   
  public doughnutChartLabels: Label[] = ['PHP', '.Net', 'Java'];
  
  public doughnutChartData: MultiDataSet = [
    [250, 150, 100],
    [160, 150, 130],
    [250, 130, 70],
  ];
  
  public doughnutChartType: ChartType = 'doughnut';
  
  constructor() { }
    
  ngOnInit() {
  }
}

Step 6 – Start the Angular Doughnut Chart App

In this step, execute the following command on terminal to start angular doughnut chart app:

ng serve

Recommended Angular Tutorials

RELATED ARTICLES

Most Popular

Recent Comments