,

A Step-by-Step Guide on Integrating Chart.js Using Angular 12 with Data from a REST API (2021)

Posted by


How to Integrate Chart.js Using Angular 12 with Data from a REST API (2021)

Chart.js is a popular JavaScript library used for creating beautiful and interactive charts and graphs. In this article, we will learn how to integrate Chart.js into an Angular 12 application and retrieve data from a REST API to populate the charts.

Getting Started

Before we start, make sure you have the following installed:

  • Node.js (version 14 or above)
  • Angular CLI (version 12 or above)

Now, let’s create a new Angular application:

$ ng new chart-app

Once the application is created, navigate to its root directory:

$ cd chart-app

Next, we need to install Chart.js and its Angular wrapper:

$ npm install chart.js ng2-charts

Creating the Chart Component

Now, let’s generate a new component to display our chart:

$ ng generate component chart

Open the newly generated component file chart.component.ts and import the necessary dependencies:

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ChartDataSets, ChartOptions } from 'chart.js';
import { Color, Label } from 'ng2-charts';

Define the necessary variables and constructor:

@Component({
selector: 'app-chart',
templateUrl: './chart.component.html',
styleUrls: ['./chart.component.css']
})
export class ChartComponent implements OnInit {

public lineChartData: ChartDataSets[] = [];
public lineChartLabels: Label[] = [];
public lineChartOptions: ChartOptions = { responsive: true };
public lineChartColors: Color[] = [];
public lineChartLegend = true;
public lineChartPlugins = [];
public lineChartType = 'line';

constructor(private http: HttpClient) { }

ngOnInit() { }

}

Now, let’s make use of the Angular HttpClient to make a GET request to our REST API in the ngOnInit method:

ngOnInit() {
this.http.get('http://api.example.com/data').subscribe((response: any) => {
this.lineChartData = response.data;
this.lineChartLabels = response.labels;
});
}

Please note that you should replace http://api.example.com/data with the actual URL of your REST API.

Adding the Chart to the Template

Now, let’s open the template file chart.component.html and add the Chart.js canvas element:

<div style="display: block;">
<canvas baseChart
[datasets]="lineChartData"
[labels]="lineChartLabels"
[options]="lineChartOptions"
[colors]="lineChartColors"
[legend]="lineChartLegend"
[plugins]="lineChartPlugins"
[chartType]="lineChartType">
</canvas>
</div>

Styling the Chart

Finally, let’s add some styling to our chart by opening the styles file chart.component.css:

:host {
display: block;
width: 600px;
margin: 0 auto;
}

Running the Application

Now, we can run our Angular 12 application and see the chart populated with data from the REST API:

$ ng serve -o

A new browser window should open automatically, and you should see your chart displayed.

Conclusion

In this article, we have learned how to integrate Chart.js into an Angular 12 application and retrieve data from a REST API to populate the charts. By following these steps, you can create dynamic and interactive charts to visualize data in your Angular applications.

0 0 votes
Article Rating
21 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
The Code Angle
1 year ago

If you are getting a CORS error or 403 forbidden error in the console, click on the error link, it will take you to another page. On this page, click on the "request temporary access to demo server" button. Then go back and refresh your page. The errors should disappear after that.

William Whang
1 year ago

Thansk mate!!! Help a lot!!! lol

didotti gaming
1 year ago

the .toPromise() function going to be deprecated , how can i replace it ? 04/09/2022

Germán González
1 year ago

Thank you so much for the video! Very good my man, one question, on reference to Auth.service.ts, when we're making the get request, and we use .topromise() the editor says that it's deprecated, what you should use next instead? observables?

Deeksha
1 year ago

Nice Tutorial! Thanks.
Please make a tutorial on integrating ngx charts in angular.

Никита Козырь
1 year ago

Thank you very much. I don't know how I would have solved this problem without you

Miguel
1 year ago

Thanks you good sir

M
M
1 year ago

thank you!!

Sky Star
1 year ago

thanks😂

Eduardo Diaz
1 year ago

Thanks a lot!! This was very helpful and easy to understand. I was able to get my personal API data to chart out bc of your great instruction. 🙂

Nithin Samuel
1 year ago

Thanks Nice Video…Subscribed 🙂

Petroleum Blockchain
1 year ago

lot of time waste on details unrelated with the main topic

Ashik T M
1 year ago

thanks…i was looking for registrable for hours

Alfredo Perez
1 year ago

Excellent video, could you make a method to show the most expensive or the cheapest coins, or only bring the name of the most expensive of the moment?
please.

Dragan Removic
1 year ago

hello bro, i follow all your steps, but show me undefined array… Can you help me why?

Nagaraju Velpula
1 year ago

im getting error,,,
ERROR TypeError: Cannot read properties of undefined (reading 'coins')

Jubril Hassan
1 year ago

Hello, please share the link to your telegram channel

Rushikesh Jagtap
1 year ago

good explaination brother

João Pedro Battistella Nadas
1 year ago

Amazing content! For a beginner, this was super easy to follow, but at the same time with good explanations on why you were doing things. Keep it up!

Mashiyat Husain060
1 year ago

Hello sir how to do same with my .net api