How can I help you?
Getting started with EJ2 TypeScript Sparkline control
19 Feb 202615 minutes to read
This document explains the steps to create a simple Sparkline and demonstrates the basic usage of the Sparkline component using the Essential® JS 2 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 documentation.
Dependencies
The following list of minimum dependencies are required to use the sparkline:
|-- @syncfusion/ej2-charts
|-- @syncfusion/ej2-base
|-- @syncfusion/ej2-data
|-- @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 sparkline to project
Add an HTML div element for the sparkline into your index.html. [src/index.html]
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 Sparkline</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 Sparkline-->
<div id='element'>
</div>
</body>
</html>Next, import the sparkline control into app.ts, create a sparkline instance, and append it to #element.
import { Sparkline } from '@syncfusion/ej2-charts';
// Initialize the sparkline control
let sparkline: Sparkline = new Sparkline();
// Render initialized sparkline
sparkline.appendTo('#element');Now, use the npm run start command to run the application in a browser.
npm run startimport { Sparkline } from '@syncfusion/ej2-charts';
let sparkline: Sparkline = new Sparkline();
sparkline.appendTo('#element');Since the data source has not been specified to the sparkline, no shapes will be rendered. Only an empty SVG element is appended to the sparkline container.
Module injection
The sparkline component is segregated into individual feature-wise modules. To use a particular feature, inject its feature module using the Sparkline.Inject() method. The module available in sparkline and its description is as follows.
- SparklineTooltip - Inject this module to enable tooltips.
In this application, the basic sparkline is modified to demonstrate various sparkline types.
In this application, the tooltip feature of the sparkline is used. Now, import the SparklineTooltip module from the sparkline package, and inject it into the sparkline control using the Sparkline.Inject method.
import { Sparkline, SparklineTooltip } from '@syncfusion/ej2-charts';
Sparkline.Inject(SparklineTooltip);Bind data source to sparkline
The dataSource property enables data binding for the sparkline. It accepts a collection of values as input, such as a list of objects.
import { Sparkline, SparklineTooltip } from '@syncfusion/ej2-charts';
Sparkline.Inject(SparklineTooltip);
let sparkline: Sparkline = new Sparkline({
height: '100px',
width: '70%',
dataSource: [
{ x: 0, xval: '2005', yval: 20090440 },
{ x: 1, xval: '2006', yval: 20264080 },
{ x: 2, xval: '2007', yval: 20434180 },
{ x: 3, xval: '2008', yval: 21007310 },
{ x: 4, xval: '2009', yval: 21262640 },
{ x: 5, xval: '2010', yval: 21515750 },
{ x: 6, xval: '2011', yval: 21766710 },
{ x: 7, xval: '2012', yval: 22015580 },
{ x: 8, xval: '2013', yval: 22262500 },
{ x: 9, xval: '2014', yval: 22507620 },
],
// Assign the dataSource values to series of sparkline 'xName and yName'
xName: 'xval', yName: 'yval'
});
sparkline.appendTo("#element");<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 for Sparkline </title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Essential JS 2 for Sparkline UI Control" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-charts/styles/material.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' style="margin-top: 15%;">
<div id="element" align="center" ></div>
</div>
</body>
</html>Change the type of sparkline
The sparkline type can be configured using the type property, which supports Line, Column, WinLoss, Pie, and Area. Here, the area type is applied.
import { Sparkline, SparklineTooltip } from '@syncfusion/ej2-charts';
Sparkline.Inject(SparklineTooltip);
let sparkline: Sparkline = new Sparkline({
height: '100px',
width: '70%',
dataSource: [
{ x: 0, xval: '2005', yval: 20090440 },
{ x: 1, xval: '2006', yval: 20264080 },
{ x: 2, xval: '2007', yval: 20434180 },
{ x: 3, xval: '2008', yval: 21007310 },
{ x: 4, xval: '2009', yval: 21262640 },
{ x: 5, xval: '2010', yval: 21515750 },
{ x: 6, xval: '2011', yval: 21766710 },
{ x: 7, xval: '2012', yval: 22015580 },
{ x: 8, xval: '2013', yval: 22262500 },
{ x: 9, xval: '2014', yval: 22507620 },
],
xName: 'xval', yName: 'yval',
// Assign 'area' as type of sparkline
type:'Area'
});
sparkline.appendTo("#element");<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 for Sparkline </title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Essential JS 2 for Sparkline UI Control" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-charts/styles/material.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' style="margin-top: 15%;">
<div id="element" align="center" ></div>
</div>
</body>
</html>Enable tooltip for sparkline
The sparkline provides additional information through a tooltip that appears when the mouse pointer hovers over the chart. You can enable the tooltip by setting the visible property to true in tooltipSettings and injecting the SparklineTooltip module using Sparkline.Inject(SparklineTooltip).
import { Sparkline, SparklineTooltip } from '@syncfusion/ej2-charts';
Sparkline.Inject(SparklineTooltip);
let sparkline: Sparkline = new Sparkline({
height: '100px',
width: '70%',
dataSource: [
{ x: 0, xval: '2005', yval: 20090440 },
{ x: 1, xval: '2006', yval: 20264080 },
{ x: 2, xval: '2007', yval: 20434180 },
{ x: 3, xval: '2008', yval: 21007310 },
{ x: 4, xval: '2009', yval: 21262640 },
{ x: 5, xval: '2010', yval: 21515750 },
{ x: 6, xval: '2011', yval: 21766710 },
{ x: 7, xval: '2012', yval: 22015580 },
{ x: 8, xval: '2013', yval: 22262500 },
{ x: 9, xval: '2014', yval: 22507620 },
],
xName: 'xval', yName: 'yval',
type:'Area',
// To enable the tooltip for sparkline
tooltipSettings: {
visible: true,
format: '${xval} : ${yval}'
}
});
sparkline.appendTo("#element");<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 for Sparkline </title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Essential JS 2 for Sparkline UI Control" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-charts/styles/material.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' style="margin-top: 15%;">
<div id="element" align="center" ></div>
</div>
</body>
</html>