Open In App

Angular PrimeNG BarChart Stacked

Last Updated : 15 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. In this article, we will learn about Bar Chart Stacked in Angular PrimeNG.

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 the Bar chart stacked, 1 dataset is represented above another dataset in a single bar. This can be done by setting stacked=”true” in options. Let’s learn about this using example

Syntax:

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

 

To run the above file run the below command:

ng serve --save

Example 1: In this example, we will create a stacked bar chart. We will compare orders on Zomato, Swiggy, Uber Eats, and Licious.

  • app.component.html:

HTML




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


  • app.component.ts:

Javascript




import { Component } from "@angular/core";
  
@Component({
    selector: "app-root",
    templateUrl: "./app.component.html",
    styleUrls: ["./app.component.scss"]
})
  
export class AppComponent {
    title = "GFG";
    basicData = {
        labels: [
            "Sunday",
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday"
        ],
        datasets: [
            {
                label: "Orders on Swiggy",
                backgroundColor: "lightgreen",
                data: [66, 49, 81, 71, 26, 65, 60]
            },
            {
                label: "Orders on Zomato",
                backgroundColor: "pink",
                data: [56, 69, 89, 61, 36, 75, 50]
            },
            {
                label: "Orders on Uber Eats",
                backgroundColor: "gold",
                data: [52, 59, 99, 71, 46, 85, 30]
            },
            {
                label: "Orders on Licious",
                backgroundColor: "skyblue",
                data: [56, 52, 69, 81, 43, 55, 40]
            }
        ]
    };
  
    StackedOptions = {
        plugins: {
            legend: {
                labels: {
                    color: "#black"
                }
            }
        },
        scales: {
            x: {
                stacked: true,
                ticks: {
                    color: "#black"
                },
                grid: {
                    color: "rgba(255,255,255,0.2)"
                }
            },
            y: {
                stacked: true,
                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: Below is another example code that illustrates the use of a Horizontal stacked Bar Chart in Angular PrimeNG.

  • app.component.html:

HTML




<h1 style="color: green;">GeeksforGeeks</h1>
<h2>Angular PrimeNG Bar Chart Stacked</h2>
<div style="width: 70%;">
    <p-chart 
        type="bar" 
        [data]="basicData" 
        [options]="StackedOptions">
    </p-chart>
</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: "lightgreen",
                data: [66, 49, 81, 71, 26, 65, 60]
            },
            {
                label: "Orders on Zomato",
                backgroundColor: "pink",
                data: [56, 69, 89, 61, 36, 75, 50]
            },
            {
                label: "Orders on Uber Eats",
                backgroundColor: "gold",
                data: [52, 59, 99, 71, 46, 85, 30]
            },
            {
                label: "Orders on Licious",
                backgroundColor: "skyblue",
                data: [56, 52, 69, 81, 43, 55, 40]
            }
        ]
    };
  
    StackedOptions = {
        indexAxis: "y",
        plugins: {
            legend: {
                labels: {
                    color: "#black"
                }
            }
        },
        scales: {
            x: {
                stacked: true,
                ticks: {
                    color: "#black"
                },
                grid: {
                    color: "rgba(255,255,255,0.2)"
                }
            },
            y: {
                stacked: true,
                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