Angular 18 Chart JS using ng2-charts Example
we will learn how to create chart using ng2-charts in angular 18. let us discuss about angular 18 chart js using ng2-charts. I explained simply step by step angular 18 chart js. you can understand a concept of angular 18 ng2 charts examples. Alright, let us dive into the steps.
The ng2-charts package offers a chart js for line charts, pie charts, Bar charts, Doughnut charts etc. in this example, we will create simple like charts using ng2-charts package. so, let's follow the following steps:
Step for How to Add Charts in Angular 18?
- Step 1: Create Angular 18 Project
- Step 2: Install ng2-charts Package
- Step 3: Update Ts File
- Step 4: Update HTML File
- Run Angular App
Let's follow the steps:
Step 1: Create Angular 18 Project
You can easily create your angular app using below command:
ng new my-new-app
Step 2: Install ng2-charts 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 --save
Step 3: Update Ts File
here, we need to update ts file as like bellow with lat and long variable:
src/app/app.component.ts
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NgChartsModule } from 'ng2-charts';
import { ChartConfiguration, ChartOptions, ChartType } from "chart.js";
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule, NgChartsModule],
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'ng2-charts-demo';
public lineChartData: ChartConfiguration['data'] = {
labels: [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
],
datasets: [
{
data: [ 40, 45, 50, 55, 60, 65, 70, 75, 70, 60, 50, 45 ],
label: 'Angular',
fill: true,
tension: 0.5,
borderColor: 'black',
backgroundColor: 'rgba(255,0,0,0.3)'
},
{
data: [ 45, 50, 60, 70, 75, 65, 50, 60, 55, 50, 45, 45 ],
label: 'React',
fill: true,
tension: 0.5,
borderColor: 'black',
backgroundColor: 'rgba(0,255,0,0.3)'
}
]
};
public lineChartOptions: ChartOptions = {
responsive: false
};
public lineChartLegend = true;
}
Step 4: Update HTML File
here, we need to update html file as like bellow code:
src/app/app.component.html
<h1>Angular 18 Chart JS using ng2-charts Example - ItSolutionStuff.com</h1>
<div style="display: block;">
<canvas baseChart width="800" height="400"
[type]="'line'"
[data]="lineChartData"
[options]="lineChartOptions"
[legend]="lineChartLegend">
</canvas>
</div>
Run Angular App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Angular app:
ng serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:4200
Preview:
now you can check it.
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Angular 18 Install Font Awesome Icons Example
- How to Integrate Google Maps in Angular 18?
- Angular 18 Stripe Payment Integration Example
- Angular 18 Login with Google Gmail Account Example
- Angular 18 Pagination with NGX Pagination Example
- Angular 18 HttpClient & Http Services Tutorial
- Angular 18 RxJS Observable with Httpclient Example
- How to Define Global Variables in Angular 18?
- Angular 18 Template Driven Form with Validation Example
- Angular 18 Multiple Image Upload Example Tutorial
- Angular 18 File Upload Tutorial Example
- Angular 18 Image Upload with Preview Example
- Angular 18 Reactive Forms with Validation Example
- How to Add Bootstrap 5 in Angular 18 Application?