Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will see the DoughnutChart Component in Angular PrimeNG
A Doughnut chart is a variant of the pie chart, with a blank center allowing for additional information about the data as a whole to be included.
Syntax:
<p-chart type="doughnut" [data]="data" [options]="chartOptions" > </p-chart>
Creating Angular application & Module Installation:
Step 1: Create an Angular application using the following command.
ng new appname
Step 2: After creating your project folder i.e. appname, move to it using the following command.
cd appname
Step 3: Install PrimeNG in your given directory.
npm install primeng --save npm install primeicons --save npm install chart.js --save
Project Structure: It will look like the following:
Example 1: This example describes the DoughnutChart Component in Angular PrimeNG.
- app.component.html
< div id = "GFG" >
< h1 style = "color:green" >GeeksforGeeks</ h1 >
< h2 >Angular PrimeNG Doughnut Chart </ h2 >
< div style = "width:30%;" >
< p-chart type = "doughnut" [data]="data"
[options]="chartOptions">
</ p-chart >
</ div >
</ div >
|
- app.component.ts
import { Component } from "@angular/core" ;
@Component({ selector: "app-root" ,
templateUrl: "./app.component.html" ,
styleUrls: [ "./app.component.css" ],
}) export class AppComponent { title = "GFG" ;
data = {
labels: [ "A" , "B" , "C" ],
datasets: [
{
data: [150, 50, 100],
backgroundColor: [ "#FF6384" ,
"#36A2EB" ,
"#FFCE56" ],
},
],
};
chartOptions = {
plugins: {
legend: {
labels: {
color: "#495057" ,
},
},
},
};
} |
- app.module.ts
import { NgModule } from '@angular/core' ;
import { BrowserModule } from '@angular/platform-browser' ;
import { AppComponent } from './app.component' ;
import { ChartModule } from 'primeng/chart' ;
@NgModule({ declarations: [
AppComponent,
],
imports: [
BrowserModule,
ChartModule
],
providers: [],
bootstrap: [AppComponent]
}) export class AppModule { } |
Output:
Example 2: This example describes the DoughnutChart Component in Angular PrimeNG, where we will be creating hover options in Doughnut, and also adding more datasets.
- app.component.html
< div id = "GFG" >
< h1 style = "color:green" >GeeksforGeeks</ h1 >
< h2 >Angular PrimeNG Doughnut Chart </ h2 >
< div style = "width:30%;" >
< p-chart type = "doughnut" [data]="data"
[options]="chartOptions">
</ p-chart >
</ div >
</ div >
|
- app.component.ts
import { Component } from '@angular/core' ;
@Component({ selector: 'app-root' ,
templateUrl: './app.component.html' ,
}) export class AppComponent { title = 'GFG' ;
data = {
labels: [ 'A' , 'B' , 'C' , 'D' ],
datasets: [
{
data: [200, 150, 50, 100],
backgroundColor: [
"#FF6384" ,
"#36A2EB" ,
"#FFCE56" ,
"green"
],
hoverBackgroundColor: [
"#FF6384" ,
"#36A2EB" ,
"#FFCE56" ,
"lightgreen"
]
}
]
};
chartOptions = {
plugins: {
legend: {
labels: {
color: '#495057'
}
}
}
};
} |
- app.module.ts
import { NgModule } from '@angular/core' ;
import { BrowserModule } from '@angular/platform-browser' ;
import { AppComponent } from './app.component' ;
import { ChartModule } from 'primeng/chart' ;
@NgModule({ declarations: [
AppComponent,
],
imports: [
BrowserModule,
ChartModule
],
providers: [],
bootstrap: [AppComponent]
}) export class AppModule { } |
Output:
Reference: http://primefaces.org/primeng/chart/doughnut