How can I help you?
Getting started with EJ2 TypeScript Bullet chart control
19 Feb 202614 minutes to read
This document explains how to create a simple Bullet Chart and configure its features in TypeScript using the Essential JS 2 webpack quickstart seed repository.
This application is integrated with the
webpack.config.jsconfiguration and uses the latest version of the webpack-cli. It requires nodev14.15.0or higher. For more information about webpack and its features, refer to the webpack getting-started guide.
Dependencies
Below is the list of minimum dependencies required to use the Bullet Chart.
|-- @syncfusion/ej2-charts
|-- @syncfusion/ej2-base
|-- @syncfusion/ej2-data
|-- @syncfusion/ej2-pdf-export
|-- @syncfusion/ej2-file-utils
|-- @syncfusion/ej2-compression
|-- @syncfusion/ej2-svg-base
Set up development environment
Open the command prompt from the required directory, and run the following command to clone the Syncfusion JavaScript (Essential JS 2) quickstart project from GitHub.
git clone https://github.com/SyncfusionExamples/ej2-quickstart-webpack ej2-quickstartAfter cloning the application in the ej2-quickstart folder, run the following command line to navigate to the ej2-quickstart folder.
cd ej2-quickstartAdd Syncfusion JavaScript packages
Syncfusion JavaScript (Essential JS 2) packages are available on the npmjs.com public registry. You can install all Syncfusion JavaScript (Essential JS 2) controls in a single @syncfusion/ej2 package or individual packages for each control.
The quickstart application is preconfigured with the dependent @syncfusion/ej2 package in the ~/package.json file. Use the following command to install the dependent npm packages from the command prompt.
npm installAdd Bullet Chart to the Project
Open the project in Visual Studio Code and add the Bullet Chart to the application.
Add the HTML div tag with its id attribute as element in your ~/src/index.html file to initialize the Bullet chart.
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Bullet Chart</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="TypeScript UI Controls" />
<meta name="author" content="Syncfusion" />
....
....
</head>
<body>
<!--container which is going to render the Bullet Chart-->
<div id='element'>
</div>
</body>
</html>Import the Bullet Chart component into src/app/app.ts to instantiate and render the Bullet Chart.
import { BulletChart } from '@syncfusion/ej2-charts';
// initialize BulletChart component
let bulletChart: BulletChart = new BulletChart();
// render initialized Bullet Chart
bulletChart.appendTo('#element');Now use the npm run start command to run the application in the browser.
npm run startThe following example shows a basic Bullet Chart.
import { BulletChart } from '@syncfusion/ej2-charts';
let bulletChart: BulletChart = new BulletChart();
bulletChart.appendTo('#element');<!DOCTYPE html>
<html lang="en">
<head>
<title>Bullet Chart</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>Module Injection
The Bullet Chart component is segregated into individual feature-wise modules. In order to use a particular feature, you need to inject its feature module using the BulletChart.Inject() method. In this application, we use the tooltip feature of the Bullet Chart.
-
BulletTooltip- Inject this module to use the tooltip feature.
Now import the above-mentioned modules from the chart package and inject them into the Bullet Chart component using BulletChart.Inject method.
import { BulletChart, BulletTooltip } from '@syncfusion/ej2-charts';
BulletChart.Inject(BulletTooltip);BulletChart With Data
This section explains how to plot the following JSON data to the Bullet Chart.
let data: Object[] = [
{ 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 the dataSource property. The value and target fields should be mapped to valueField and targetField, respectively.
import { BulletChart } from '@syncfusion/ej2-charts';
let bulletChart: BulletChart = new BulletChart({
dataSource: [{ value: 100, target: 80 },
{ value: 200, target: 180 },
{ value: 300, target: 280 },
{ value: 400, target: 380 },
{ value: 500, target: 480 }],
valueField: 'value',
targetField: 'target',
height: '300',
minimum: 0, maximum: 500, interval: 50,
});
bulletChart.appendTo('#element');<!DOCTYPE html>
<html lang="en">
<head>
<title>Bullet Chart</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>Add Bullet Chart Title
You can add a title using the title property to the Bullet Chart to provide quick information to the user about the data plotted in the Bullet Chart.
import { BulletChart } from '@syncfusion/ej2-charts';
let bulletChart: BulletChart = new BulletChart({
dataSource: [{ value: 270, target: 250 },],
valueField: 'value',
targetField: 'target',
title: 'Revenue',
minimum: 0, maximum: 300, interval: 50,
});
bulletChart.appendTo('#element');<!DOCTYPE html>
<html lang="en">
<head>
<title>Bullet Chart</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>Ranges
You can add a range using the ranges property to the Bullet Chart.
import { BulletChart } from '@syncfusion/ej2-charts';
let bulletChart: BulletChart = new BulletChart({
dataSource: [{ value: 270, target: 250 },],
valueField: 'value',
targetField: 'target',
title: 'Revenue',
minimum: 0, maximum: 300, interval: 50,
ranges: [{ end: 100, color: 'red' },
{ end: 200, color: 'blue' },
{ end: 300, color: 'green' }
],
});
bulletChart.appendTo('#element');<!DOCTYPE html>
<html lang="en">
<head>
<title>Bullet Chart</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>Tooltip
You can use the tooltip for the Bullet Chart by setting the enable property to true in the tooltip object and by injecting the BulletTooltip module using the BulletChart.Inject(BulletTooltip) method.
import { BulletChart, BulletTooltip } from '@syncfusion/ej2-charts';
BulletChart.Inject(BulletTooltip);
let bulletChart: BulletChart = new BulletChart({
tooltip: { enable: true },
dataSource: [{ value: 70, target: 50 }],
valueField: 'value',
targetField: 'target',
animation: { enable: false },
ranges: [{ end: 30, color: '#599C20' },
{ end: 60, color: '#EFC820' },
{ end: 100, color: '#CA4218' }
],
minimum: 0, maximum: 100, interval: 10,
title: 'Revenue YTD'
});
bulletChart.appendTo('#element');<!DOCTYPE html>
<html lang="en">
<head>
<title>Bullet Chart</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript UI Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>