Open In App

Angular PrimeNG ChartModel Properties

Last Updated : 19 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 Properties.

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 Properties are used to modify the component and display according to the design of the website. We will learn to use chartmodel properties with some examples.

Angular PrimeNG ChartModel Properties:

  • type(string): It is the type of chart.
  • data(any): Here the data is provided to display.
  • options(any): Here are the options to customize the chart are provided.
  • plugins(any[]): It is the array of per-chart plugins to customize the chart behavior.
  • width(string): It is the width of the chart.
  • height(string): It is the height of the chart.
  • responsive(boolean): If set to true, the chart is redrawn on screen size change. The default value is true.
  • onDataSelect(function): It is the callback to execute when an element on the chart is clicked.

Syntax:

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

Creating Angular application & Module Installation:

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

ng new geeks_angular

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

cd geeks_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 --open

Example 1: In the following example, we will demonstrate the use of the Angular PrimeNG ChartModel Properties in which the chart has height and width.

app.component.html:

HTML




<h1 style="color: green; text-align:center;">GeeksforGeeks</h1>
<h3>Angular PrimeNG ChartModel Properties</h3>
<p-toast></p-toast>
<p-chart type="bar" 
         [data]="basicData"
         width="40vw" 
         height="40vh">
</p-chart>


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;
    constructor(
        private messageService: MessageService,
        private primengConfig: PrimeNGConfig
    ) {}
    ngOnInit() {
        this.basicData = {
            labels: ["January", "February", "March", "April", "May", "June"],
            datasets: [
                {
                    label: "2020",
                    data: [65, 59, 80, 81, 56, 55],
                    tension: 0.4,
                    backgroundColor: [
                        "#FF6371",
                        "#36A2EB",
                        "#FFCE45",
                        "#ff6200",
                        "#00ffbf",
                        "#9900ff"
                    ],
                    borderColor: "#42A5F5"
                }
            ]
        };
        this.basicOptions = {
            title: {
                display: true,
                text: "Article Views",
                fontSize: 32,
                position: "top"
            }
        };
    }
}


app.module.ts:

Javascript




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { HttpClientModule } from "@angular/common/http";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { ButtonModule } from "primeng/button";
import { ToastModule } from "primeng/toast";
import { RippleModule } from "primeng/ripple";
import { ImageModule } from "primeng/image";
import { ChartModule } from "primeng/chart";
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ToastModule,
        ButtonModule,
        RippleModule,
        FormsModule,
        ImageModule,
        ChartModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule {}


Output:

 

Example 2: In the following example, we will demonstrate the use of the Angular PrimeNG ChartModel Properties in which the chart is made responsive

app.component.html:

HTML




<h1 style="color: green; text-align: center;">GeeksforGeeks</h1>
<h3>Angular PrimeNG ChartModel Properties</h3>
<p-chart #chart 
         type="bar" 
         [data]="basicData" 
         [responsive]="true">
</p-chart>


app.component.ts:

Javascript




import { Component, ViewChild } 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;
    responsive: boolean;
    constructor(
        private messageService: MessageService,
        private primengConfig: PrimeNGConfig
    ) {}
    ngOnInit() {
        this.responsive = false;
        this.basicData = {
            labels: ["January", "February", "March", "April", "May", "June"],
            datasets: [
                {
                    label: "2020",
                    data: [65, 59, 80, 81, 56, 55],
                    tension: 0.4,
                    backgroundColor: [
                        "#FF6371",
                        "#36A2EB",
                        "#FFCE45",
                        "#ff6200",
                        "#00ffbf",
                        "#9900ff"
                    ],
                    borderColor: "#42A5F5"
                }
            ]
        };
        this.basicOptions = {
            title: {
                display: true,
                text: "Article Views",
                fontSize: 32,
                position: "top"
            }
        };
    }
}


app.module.ts:

Javascript




import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { HttpClientModule } from "@angular/common/http";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { ButtonModule } from "primeng/button";
import { ToastModule } from "primeng/toast";
import { RippleModule } from "primeng/ripple";
import { ImageModule } from "primeng/image";
import { ChartModule } from "primeng/chart";
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        ToastModule,
        ButtonModule,
        RippleModule,
        FormsModule,
        ImageModule,
        ChartModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule {}


Output:

 

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads