Angular PrimeNG ChartModel Change Detection

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. It provides a lot of templates, components, theme design, an extensive icon library, and much more. In this article, we are going to learn Angular PrimeNG ChartModel Change Detection.

The ChartModel Component is used to create different types of charts and is based on Chart.js, an open-source HTML5-based charting library. The ChartModel Change Detection redraws the chart when a new data object is created. Changing the array contents without creating a new array instance does not trigger change detection.

Syntax:

<p-chart type="pie" 
    [data]="...">
</p-chart>

<button type="button" pButton 
    (click)="updateData($event)">
</button>
  • Update it as follows:
update(event: Event) {
    this.basicData= newData;
}

 

Creating Angular application & Module Installation:

Step 1: Create an Angular application using the following command.

ng new Beginner_angular

Step 2: After creating your project folder i.e. Beginner_angular, move to it using the following command.

cd Beginner_angular

Step 3: Install PrimeNG in your given directory.

npm install primeng --save
npm install primeicons --save

Project Structure: The project structure will look like the following:

 

Steps to run the application: Run the below command to see the output

ng serve --save

Example 1: In the following example, we have a button that adds data and the update button refreshes the chart.

  • app.component.html:

HTML




<h1 style="color: green; text-align: center;">
      w3wiki
</h1>
<h3>
      Angular PrimeNG ChartModel 
      Change Detection
</h3>
<p-chart #chart type="pie" 
    [data]="basicData" 
    [responsive]="true">
</p-chart>
<p-button label="Add Data" 
    (onClick)="addDataClick($event)">
</p-button>
<p-button label="Update Data" 
    (onClick)="updateDataClick($event)">
</p-button>


  • app.component.ts:

Javascript




import { Component } from "@angular/core";
import { PrimeNGConfig } from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html"
})
  
export class AppComponent {
    basicData: any;
    basicOptions: any;
    labels: string[] = ["Item1", "Item2"
        "Item3", "Item4", "Item5", "Item6"];
    data: number[] = [65, 59, 80, 81, 56, 55];
    constructor(private primengConfig: PrimeNGConfig) {}
    ngOnInit() {
        this.basicData = {
            labels: this.labels,
            datasets: [
                {
                    data: this.data,
                    tension: 0.4,
                    borderColor: "#42A5F5"
                }
            ]
        };
    }
    addDataClick(event) {
        this.labels.push(`Item${this.labels.length + 1}`);
        this.data.push(this.data[0] + 10);
    }
    updateDataClick(event) {
        this.basicData = { ...this.basicData };
    }
}


  • app.module.ts:

Javascript




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { ButtonModule } from "primeng/button";
import { ChartModule } from "primeng/chart";
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ButtonModule,
        ChartModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule {}


Output:

 

Example 2: In the following example, we are updating the data directly.

  • app.component.html:

HTML




<h1 style="color: green; text-align:center;">
      w3wiki
</h1>
<h3>
      Angular PrimeNG ChartModel 
      Change Detection
</h3>
<p-chart #chart type="pie" 
        [data]="basicData" 
        [responsive]="true">
</p-chart>
<p-button label="Update Data" 
        (onClick)="updateDataClick($event)">
</p-button>


  • app.component.ts:

Javascript




import { Component } from "@angular/core";
import { MessageService } from "primeng/api";
import { PrimeNGConfig } from "primeng/api";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
    providers: [MessageService]
})
  
export class AppComponent {
    basicData: any;
    basicOptions: any;
    labels: string[] = ["Item1", "Item2",
        "Item3", "Item4", "Item5", "Item6"];
    data: number[] = [65, 59, 80, 81, 56, 55];
    constructor(private primengConfig: PrimeNGConfig) {}
    ngOnInit() {
        this.basicData = {
            labels: this.labels,
            datasets: [
                {
                    data: this.data,
                    tension: 0.4,
                    borderColor: "#42A5F5"
                }
            ]
        };
    }
    updateDataClick(event) {
        this.basicData = {
            labels: [
                "Item1",
                "Item2",
                "Item3",
                "Item4",
                "Item5",
                "Item6",
                "Item7",
                "Item8"
            ],
            datasets: [
                {
                    data: [65, 59, 80, 81, 56, 55, 22, 58],
                    tension: 0.4,
                    borderColor: "#e23545"
                }
            ]
        };
    }
}


  • app.module.ts:

Javascript




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { ButtonModule } from "primeng/button";
import { ChartModule } from "primeng/chart";
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ButtonModule,
        ChartModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule {}


Output:

 

Reference: http://primefaces.org/primeng/chart