Open In App

Angular PrimeNG BarChart Horizontal

Last Updated : 14 Nov, 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.

A bar chart or bar graph is a chart that presents Grouped data with rectangular bars with lengths proportional to the values that they represent. In this article, we will learn about Bar Charts Horizontal.

In the Bar chart Horizontal, datasets are represented in Horizontal orientation. This can be achieved by setting a property of indexAxis in options. Let’s learn about this using example

 

Syntax:

<p-chart type="bar" [data]="Data" 
  [options]="horizontalOptions">
</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:

app.component.html

HTML




<div id="GFG">
    <h1 style="color:green">GeeksforGeeks</h1>
    <h2>Angular PrimeNG Bar Chart Horizontal</h2>
    <div style="width:70%;">
        <p-chart type="bar" [data]="basicData" 
                [options]="horizontalOptions">
        </p-chart>
    </div>
</div>


app.component.ts

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    title = 'GFG';
    basicData = {
        labels:
            ['Sunday', 'Monday', 'Tuesday', 'Wednesday',
                'Thursday', 'Friday', 'Saturday'],
        datasets: [
            {
                label: 'Booked',
                backgroundColor: 'green',
                data: [66, 49, 81, 71, 26, 65, 60]
            },
  
        ]
    };
  
    horizontalOptions = {
        indexAxis: 'y',
        plugins: {
            legend: {
                labels: {
                    color: '#black'
                }
            }
        },
        scales: {
            x: {
                ticks: {
                    color: '#black'
                },
                grid: {
                    color: 'rgba(255,255,255,0.2)'
                }
            },
            y: {
                ticks: {
                    color: '#black'
                },
                grid: {
                    color: 'rgba(255,255,255,0.2)'
                }
            }
        }
    };
}


app.module.ts

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 learn how to display 2 datasets together horizontally in bar chart

app.component.html

HTML




<div id="GFG">
    <h1 style="color:green">GeeksforGeeks</h1>
    <h2>Angular PrimeNG Bar Chart Horizontal</h2>
    <div style="width:70%;">
        <p-chart type="bar" [data]="basicData" 
                 [options]="horizontalOptions">
       </p-chart>
    </div>
</div>


app.component.ts

Javascript




import { Component } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    title = 'GFG';
    basicData = {
        labels: ['Sunday', 'Monday', 'Tuesday',
            'Wednesday', 'Thursday', 'Friday', 'Saturday'],
        datasets: [
            {
                label: 'Orders on Swiggy',
                backgroundColor: 'green',
                data: [66, 49, 81, 71, 26, 65, 60]
            },
            {
                label: 'Orders on Zomato',
                backgroundColor: 'red',
                data: [56, 69, 89, 61, 36, 75, 50]
            },
  
        ]
    };
  
    horizontalOptions = {
        indexAxis: 'y',
        plugins: {
            legend: {
                labels: {
                    color: '#black'
                }
            }
        },
        scales: {
            x: {
                ticks: {
                    color: '#black'
                },
                grid: {
                    color: 'rgba(255,255,255,0.2)'
                }
            },
            y: {
                ticks: {
                    color: '#black'
                },
                grid: {
                    color: 'rgba(255,255,255,0.2)'
                }
            }
        }
    };
}


app.module.ts

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:

 

References: http://primefaces.org/primeng/chart/bar



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

Similar Reads