- Create a new angular application using the following command
(Note: skip this step if you want to add ngx-charts in the existing angular application, At the time of writing this article I was using angular 9).
ng new ngx-charts-demo
- Install ngx-charts package in an angular application using the following command.
npm install @swimlane/ngx-charts --save
- At the time of installation if you get the following error
ERROR in The target entry-point "@swimlane/ngx-charts" has missing dependencies:
- @angular/cdk/portal
we need to add @angular/cdk
using the following
npm install @angular/cdk --save
- Import
NgxChartsModule
from 'ngx-charts'
in AppModule. - ngx-charts also required the
BrowserAnimationsModule
. Import it in AppModule
.
So our final AppModule
will look like :
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { NgxChartsModule }from '@swimlane/ngx-charts';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
NgxChartsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Great, Installation steps are done. Now let’s develop various charts using ngx-charts
components.
In AppComponent
we will create the following sales data array. We will use this object to generate charts.
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
saleData = [
{ name: "Mobiles", value: 105000 },
{ name: "Laptop", value: 55000 },
{ name: "AC", value: 15000 },
{ name: "Headset", value: 150000 },
{ name: "Fridge", value: 20000 }
];
}