Angular PrimeNG Pie Chart Component
Last Updated :
08 Nov, 2022
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 Pie Chart Component in Angular PrimeNG.
A Pie Chart is a circular statistical graphic, which is divided into slices to illustrate numerical proportions.
Syntax:
<p-chart type="pie"
[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 Pie Chart Component in Angular PrimeNG, where we will display 3 datasets in the pie chart.
HTML
< div id = "GFG" >
< h1 style = "color:green" >GeeksforGeeks</ h1 >
< h2 >Angular PrimeNG Pie Chart </ h2 >
< div style = "width:20%;" >
< p-chart type = "pie"
[data]="data"
[options]="chartOptions"
[style]="{'width': '40%'}">
</ p-chart >
</ div >
</ div >
|
Javascript
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: [300, 50, 100],
backgroundColor: [
"#42A5F5" ,
"#66BB6A" ,
"#FFA726"
],
}
]
};
chartOptions = {
plugins: {
legend: {
labels: {
color: '#495057'
}
}
}
}
}
|
Javascript
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: In this example, we will add hoverOptions with a pie chart. When we will hover over the piechart, the component color will be changed. Also, add 4 datasets to be shown
Javascript
<div id= "GFG" >
<h1 style= "color:green" >GeeksforGeeks</h1>
<h2>Angular PrimeNG Pie Chart </h2>
<div style= "width:30%;" >
<p-chart type= "pie"
[data]= "data"
[options]= "chartOptions"
[style]= "{'width': '40%'}" >
</p-chart>
</div>
</div>
|
Javascript
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' , 'D' ],
datasets: [
{
data: [300, 50, 100, 60],
backgroundColor: [
"#42A5F5" ,
"#66BB6A" ,
"#FFA726" ,
'red'
],
hoverBackgroundColor: [
"#64B5F6" ,
"#81C784" ,
"#FFB74D" ,
'pink'
]
}
]
};
chartOptions = {
plugins: {
legend: {
labels: {
color: '#495057'
}
}
}
}
}
|
Javascript
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/pie
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...