ItSolutionStuff.com

Angular Pie Chart Example Tutorial

By Hardik Savani • May 2, 2024
Angular

Hi All,

In this quick example, let's see angular pie chart example. This tutorial will give you simple example of angular pie chart npm. this example will help you angular ng2-charts pie chart . you'll learn how to add pie chart in angular. Here, Creating a basic example of angular pie chart example.

you can easily add pie chart in angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13, angular 14, angular 15, angular 16 and angular 17 version app.

In this example we will use ng2-charts npm package to create pie chart example in angular 11 application. we will simply install that ng2-charts npm package and use ChartsModule module to create code.

So, let's see bellow step and get qr code as like bellow screenshot:

Preview:

Step 1: Create New App

You can easily create your angular app using bellow command:

ng new myNewApp

Step 2: Install ng2-charts npm Package

Now in this step, we need to just install ng2-charts in our angular application. so let's add as like bellow:

npm install ng2-charts chart.js --save

Step 3: Import ChartsModule

we will import ChartsModule module as like bellow code:

src/app/app.module.ts

import { NgModule } from '@angular/core';

import { BrowserModule } from '@angular/platform-browser';

import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';

import { ChartsModule } from 'ng2-charts';

@NgModule({

imports: [ BrowserModule, FormsModule, ChartsModule ],

declarations: [ AppComponent ],

bootstrap: [ AppComponent ]

})

export class AppModule { }

Step 4: Update Ts File

here, we need to update ts file as like bellow:

src/app/app.component.ts

import { Component, VERSION } from '@angular/core';

import { ChartType, ChartOptions } from 'chart.js';

import { SingleDataSet, Label, monkeyPatchChartJsLegend, monkeyPatchChartJsTooltip } from 'ng2-charts';

@Component({

selector: 'my-app',

templateUrl: './app.component.html',

styleUrls: [ './app.component.css' ]

})

export class AppComponent {

name = 'Angular ' + VERSION.major;

public pieChartOptions: ChartOptions = {

responsive: true,

};

public pieChartLabels: Label[] = ['PHP', '.Net', 'Java'];

public pieChartData: SingleDataSet = [50, 30, 20];

public pieChartType: ChartType = 'pie';

public pieChartLegend = true;

public pieChartPlugins = [];

constructor() {

monkeyPatchChartJsTooltip();

monkeyPatchChartJsLegend();

}

}

Step 5: Update HTML File

here, we need to update html file as like bellow code:

src/app/app.component.html

<h1>Angular pie chart example - ItSolutionStuff.com</h1>

<div class="chart-wrapper">

<canvas baseChart

[data]="pieChartData"

[labels]="pieChartLabels"

[chartType]="pieChartType"

[options]="pieChartOptions"

[plugins]="pieChartPlugins"

[legend]="pieChartLegend">

</canvas>

</div>

Now you can run by bellow command:

ng serve

now you can check it.

I hope it can help you...

Tags: Angular
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Angular 11 Google OAuth Social Login Example

Read Now →

Angular Pipe Number 2 Decimal Places Example

Read Now →

Angular Material Autocomplete Select Option Event Example

Read Now →

Angular Material Datepicker Change Date Format Example

Read Now →

Angular 11|10 Date Range Picker Example

Read Now →

Angular PDF Viewer Example

Read Now →

Angular Google Maps Multiple Markers Example

Read Now →

Angular Material Copy to Clipboard Example

Read Now →

Angular 10 Display JSON Data in Table Example

Read Now →

Angular 11/10 Multi Select Dropdown Example

Read Now →