Angular 11 Bar Chart using JS Chart Tutorial Example - Yudhy Network

Share on:

Yudhy Network - Chart js bar chart angular example. In this tutorial, you will learn how to implement bar chart using charts js in angular 11 app.

Angular 9/10/11 charts js bar chart example. In this tutorial, you will learn step by step how to implement bar chart using charts js library in angular 9/10/11 app.

And also, this tutorial will show you How to integrate bar chart using charts js Angular 10/11 application.

Chart JS bar Chart Angular 11 Example

  • Step 1 – Create New Angular App
  • Step 2 – Install Charts JS Library
  • Step 3 – Add Code on App.Module.ts File
  • Step 4 – Add Code on View File
  • Step 5 – Add Code On bar-chart.Component ts File
  • Step 6 – Start the Angular Bar Chart App

Step 1 – Create New Angular App

First of all, open your terminal and execute the following command on it to install angular app:

ng new my-new-app

Step 2 – Install Charts JS Library

Then install NPM package called ng2-charts chart.js –save for implement bar chart in angular 11 app. So, You can install the packages by executing the following commands on the terminal:

npm install --save bootstrap

npm install ng2-charts chart.js --save

After that, open angular.json file and update the following code into it:

"styles": [
      "node_modules/bootstrap/dist/css/bootstrap.min.css",
      "src/styles.css"
]
Recommended:- Angular 11 Reactive Forms Validation Tutorial Example

Step 3 – Add Code on App.Module.ts File

In this step, visit src/app directory and open app.module.ts file. And then add the following lines of into app.module.ts file:

import { ChartsModule } from 'ng2-charts';

@NgModule({
  declarations: [...],
  imports: [
    ChartsModule
  ],
  providers: [...],
  bootstrap: [...]
})

export class AppModule { }
Recommended:- Angular 11 Image Upload With Preview Tutorial

Step 4 – Add Code on View File

In this step, create bar chart in angular 11 app. So, visit src/app/ and bar-chart.component.htmland update the following code into it:

<div class="chart-wrapper">
    <canvas baseChart 
    [datasets]="barChartData"
    [labels]="barChartLabels"
    [options]="barChartOptions"
    [plugins]="barChartPlugins"
    [legend]="barChartLegend"
    [chartType]="barChartType">
  </canvas>
</div>

Step 5 – Add Code On bar-chart.Component ts File

In this step, visit the src/app directory and open bar-chart.component.ts. Then add the following code into component.ts file:

import { Component } from '@angular/core';
import { ChartOptions, ChartType, ChartDataSets } from 'chart.js';
import { Label } from 'ng2-charts';

@Component({
  selector: 'app-bar-chart',
  templateUrl: './bar-chart.component.html',
  styleUrls: ['./bar-chart.component.css']
})

export class BarChartComponent {

  barChartOptions: ChartOptions = {
    responsive: true,
  };
  barChartLabels: Label[] = ['Apple', 'Banana', 'Kiwifruit', 'Blueberry', 'Orange', 'Grapes'];
  barChartType: ChartType = 'bar';
  barChartLegend = true;
  barChartPlugins = [];

  barChartData: ChartDataSets[] = [
    { data: [45, 37, 60, 70, 46, 33], label: 'Best Fruits' }
  ];

}
Recommended:- Angular 11 DataTable Tutorial Example

Step 6 – Start the Angular Bar Chart App

In this step, execute the following command on terminal to start angular bar chart app:

ng serve

Recommended Angular Posts

Recommended:- Angular 11 Sweetalert2 Tutorial Example
Recommended:- Angular 11 Radio Button Reactive Form Example
Recommended:- Angular 11 Select Dropdown Example Tutorial
Recommended:- Angular 11 Stripe Payment Checkout Gateway Example
Recommended:- Angular 11 Date Range Picker Tutorial Example
Recommended:- Angular 11 CRUD Application Tutorial Example
Recommended:- Angular 11 Multiple File Upload Tutorial Example
Recommended:- Angular 11 Line Chart using JS Chart Tutorial Example
Recommended:- Angular 11 Google Maps Integration Tutorial Example
Recommended:- Angular 11 Full Page Scrolling Example
Recommended:- Angular 11 Datatable Print, Export to CSV, Excel Example

Please ask in the comments!

Suport : Cotekno.Com

comments powered by Disqus