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.
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.
Thansk mate!!! Help a lot!!! lol
the .toPromise() function going to be deprecated , how can i replace it ? 04/09/2022
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?
Nice Tutorial! Thanks.
Please make a tutorial on integrating ngx charts in angular.
Thank you very much. I don't know how I would have solved this problem without you
Thanks you good sir
thank you!!
thanks😂
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. 🙂
Thanks Nice Video…Subscribed 🙂
lot of time waste on details unrelated with the main topic
thanks…i was looking for registrable for hours
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.
hello bro, i follow all your steps, but show me undefined array… Can you help me why?
im getting error,,,
ERROR TypeError: Cannot read properties of undefined (reading 'coins')
Hello, please share the link to your telegram channel
good explaination brother
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!
Hello sir how to do same with my .net api