Open In App

Angular PrimeNG ChartModel Responsive

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 will see the Angular PrimeNG ChartModel Responsive.

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 provides different types of properties that help us to customize the charts in Angular. The ChartModel responsive is used to make the charts responsive based on screen sizes.



Angular PrimeNG ChartModel Responsive:

 



Syntax:

<p-chart type="line" [data]="data" 
    width="40vw" height="80vh">
</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
npm install chart.js --save

Include it in the project

"scripts": [
   "../node_modules/chart.js/dist/chart.js",
   //..others
],

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

 

Example 1: In the following example, we have a pie chart with some data and it is responsive

app.component.html:




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

app.component.ts




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




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 have a responsive layout using VH-VW units.

app.component.html




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

app.component.ts




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




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


Article Tags :