Getting started with Angular Bullet chart component
11 Jul 202412 minutes to read
This section explains you the steps required to create a simple Bullet Chart and demonstrate the basic usage of the Bullet Chart component in an Angular environment.
Setup Angular Environment
You can use Angular CLI
to setup your Angular applications. To install Angular CLI use the following command.
npm install -g @angular/cli
Create an Angular Application
Start a new Angular application using below Angular CLI command.
ng new my-app
cd my-app
Installing Syncfusion BulletChart package
Syncfusion packages are distributed in npm as @syncfusion
scoped packages. You can get all the Angular Syncfusion package from npm link.
Currently, Syncfusion provides two types of package structures for Angular components,
- Ivy library distribution package format
- Angular compatibility compiler(Angular’s legacy compilation and rendering pipeline) package.
Ivy library distribution package
Syncfusion Angular packages(>=20.2.36
) has been moved to the Ivy distribution to support the Angular Ivy rendering engine and the package are compatible with Angular version 12 and above. To download the package use the below command.
Add @syncfusion/ej2-angular-charts
package to the application.
npm install @syncfusion/ej2-angular-charts --save
Angular compatibility compiled package(ngcc)
For Angular version below 12, you can use the legacy (ngcc) package of the Syncfusion Angular components. To download the ngcc
package use the below.
Add @syncfusion/ej2-angular-charts@ngcc
package to the application.
npm install @syncfusion/ej2-angular-charts@ngcc --save
To mention the ngcc package in the package.json
file, add the suffix -ngcc
with the package version as below.
@syncfusion/ej2-angular-charts:"20.2.38-ngcc"
Note: If the ngcc tag is not specified while installing the package, the Ivy Library Package will be installed and this package will throw a warning.
Add Bullet Chart component
Modify the template in app.component.ts
file to render the ej2-angular-charts
component
[src/app/app.component.ts]
.
import { BulletChartModule} from '@syncfusion/ej2-angular-charts'
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
imports: [
BulletChartModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the Bullet Charts component
template: `<ejs-bulletchart id='container'></ejs-bulletchart>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent { }
Now use the app-container
in the index.html instead of default one.
<app-container></app-container>
Now run the application in the browser using the below command.
npm start
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { BulletChartModule} from '@syncfusion/ej2-angular-charts'
import { Component } from '@angular/core';
@Component({
imports: [
BulletChartModule
],
standalone: true,
selector: 'app-container',
// specifies the template string for the Chart component
template: `<ejs-bulletchart id="chart-container"></ejs-bulletchart>`
})
export class AppComponent {
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Module Injection
Bullet Chart component are segregated into individual feature-wise modules. In order to use a particular feature, you need to inject its feature service in the app.component.ts
. Please find relevant feature service name and description as follows.
-
BulletTooltipService
- Inject this provider to use tooltip feature.
These modules should be injected to the provider section as follows,
import { Component } from '@angular/core';
import { BulletChartModule} from '@syncfusion/ej2-angular-charts'
import { BulletTooltipService } from '@syncfusion/ej2-angular-charts';
@Component({
imports: [
BulletChartModule
],
standalone: true,
providers: [ BulletTooltipService ]
})
Bullet Chart with Data
This section explains how to plot local data to the bullet chart.
export class AppComponent implements OnInit {
public data: Object[];
ngOnInit(): void {
// Data for bullet chart
this.data = [
{ value: 100, target: 80 },
{ value: 200, target: 180 },
{ value: 300, target: 280 },
{ value: 400, target: 380 },
{ value: 500, target: 480 },
];
}
}
Now assign the local data to dataSource
property. value and target values should be mapped with valueField
and targetField
respectively.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { BulletChartModule } from '@syncfusion/ej2-angular-charts'
import { Component } from '@angular/core';
@Component({
imports: [
BulletChartModule
],
providers: [ ],
standalone: true,
selector: 'app-container',
template: `<ejs-bulletchart valueField='value' targetField='target'
[minimum]='minimum' [maximum]='maximum' [interval]='interval' [dataSource]='data'>
</ejs-bulletchart>`
})
export class AppComponent {
public minimum: number = 0;
public maximum: number = 300;
public interval: number = 50;
public data: Object[] = [
{ value: 100, target: 80 },
{ value: 200, target: 180 },
{ value: 300, target: 280 },
{ value: 400, target: 380 },
{ value: 500, target: 480 },
];
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Add Bullet Chart Title
You can add a title using title
property to the bullet chart to provide quick information to the user about the data plotted in the bullet chart.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { BulletChartModule} from '@syncfusion/ej2-angular-charts'
import { Component } from '@angular/core';
@Component({
imports: [
BulletChartModule
],
providers: [ ],
standalone: true,
selector: 'app-container',
template: `<ejs-bulletchart valueField='value' targetField='target' title='Revenue'
[minimum]='minimum' [maximum]='maximum' [interval]='interval' [dataSource]='data'>
</ejs-bulletchart>`
})
export class AppComponent {
public minimum: number = 0;
public maximum: number = 300;
public interval: number = 50;
public data: Object[] = [
{ value: 270, target: 250 }
];
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Ranges
You can add a range using ranges
property to the bullet chart.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { BulletChartModule} from '@syncfusion/ej2-angular-charts'
import { Component } from '@angular/core';
@Component({
imports: [
BulletChartModule
],
providers: [ ],
standalone: true,
selector: 'app-container',
template: `<ejs-bulletchart valueField='value' targetField='target' title='Revenue'
[minimum]='minimum' [maximum]='maximum' [interval]='interval' [dataSource]='data'>
<e-bullet-range-collection>
<e-bullet-range end='100' color='#ebebeb'></e-bullet-range>
<e-bullet-range end='200' color='#d8d8d8'></e-bullet-range>
<e-bullet-range end='300' color='#7f7f7f'></e-bullet-range>
</e-bullet-range-collection>
</ejs-bulletchart>`
})
export class AppComponent {
public minimum: number = 0;
public maximum: number = 300;
public interval: number = 50;
public data: Object[] = [
{ value: 270, target: 250 }
];
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Enable Tooltip
The tooltip is useful when you cannot display information by using the data labels due to space constraints. You can enable tooltip by setting the enable property as true in tooltip
object and by injecting BulletTooltipService
into the @NgModule.providers
.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { BulletChartModule} from '@syncfusion/ej2-angular-charts'
import { BulletTooltipService} from '@syncfusion/ej2-angular-charts'
import { Component } from '@angular/core';
@Component({
imports: [
BulletChartModule
],
providers: [ BulletTooltipService ],
standalone: true,
selector: 'app-container',
template: `<ejs-bulletchart valueName='value' targetName='target' title='Revenue'
[minimum]='minimum' [maximum]='maximum' [interval]='interval' [dataSource]='data'
[tooltip]='tooltip'>
<e-bullet-range-collection>
<e-bullet-range end='100' color='#ebebeb'></e-bullet-range>
<e-bullet-range end='200' color='#d8d8d8'></e-bullet-range>
<e-bullet-range end='300' color='#7f7f7f'></e-bullet-range>
</e-bullet-range-collection>
</ejs-bulletchart>`
})
export class AppComponent {
public minimum: number = 0;
public maximum: number = 300;
public interval: number = 50;
public tooltip: Object = {enable: true};
public data: Object[] = [
{ value: 270, target: 250 }
];
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));