Getting started with Angular Sparkline component
11 Jul 202410 minutes to read
This section explains you the steps required to create a Sparkline and demonstrate the basic usage of the Sparkline control.
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
Adding Syncfusion Sparkline package
All the available Essential JS 2 packages are published in npmjs.com
registry.
To install sparkline component, use the following command.
npm install @syncfusion/ej2-angular-charts --save
The –save will instruct NPM to include the sparkline package inside of the
dependencies
section of thepackage.json
.
Add Sparkline component
Modify the template in app.component.ts
file to render the ej2-angular-charts
component [src/app/app.component.ts]
.
import { SparklineModule } from '@syncfusion/ej2-angular-charts'
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
imports: [
SparklineModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the Sparkline component
template: `<ejs-sparkline id='sparkline-container'></ejs-sparkline>`,
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
The below example shows a basic Sparkline.
import { SparklineModule } from '@syncfusion/ej2-angular-charts'
import { Component } from '@angular/core';
@Component({
imports: [
SparklineModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the Sparkline component
template: `<ejs-sparkline id='sparkline-container'></ejs-sparkline>`
})
export class AppComponent { }
As we didn’t specify dataSource to the Sparkline, no shape will be rendered and only an empty SVG element is appended to the Sparkline container.
Module Injection
Sparkline component are segregated into individual feature-wise modules. In order to use a particular feature, you need to inject its feature service in the AppModule. Find the relevant feature service available in Sparkline and its description as follows.
-
SparklineTooltipService - Inject this provider to use tooltip series.
In the current application, we are going to modify the above basic Sparkline to visualize the sparkline types.
For this application we are going to use tooltip features of the Sparkline.
Now import the SparklineTooltipService from Sparkline package and inject it into the AppModule.import { SparklineModule, SparklineTooltipService } from '@syncfusion/ej2-angular-charts' import { Component } from '@angular/core'; @Component({ imports: [ SparklineModule, ], standalone: true, providers: [ SparklineTooltipService ] })
Bind data source to Sparkline
The [dataSource
] property is used for binding data source to Sparkline. This property takes collection value as input. For example, the list of objects can be provided as input.
[app.component.ts
]
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Change the type of Sparkline
We can change the Sparkline types by setting the [type
] property as [Line
],[Column
],[WinLoss
],[Pie
],[Area
]. Here, we have given the Sparkline type as [Area
].
[app.component.ts
]
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { SparklineModule, SparklineTooltipService } from '@syncfusion/ej2-angular-charts'
import { Component } from '@angular/core';
@Component({
imports: [
SparklineModule
],
providers: [SparklineTooltipService],
standalone: true,
selector: 'app-container',
template: `<ejs-sparkline id='container' width='400px' height='350px' [dataSource]="data" xName="xval" yName="yval" type="Area">
</ejs-sparkline>`
})
export class AppComponent {
public data: object[] = [
{ xval: 1, yval: 20090440 },
{ xval: 2, yval: 20264080 },
{ xval: 3, yval: 20434180 },
{ xval: 4, yval: 21007310 },
{ xval: 5, yval: 21262640 },
{ xval: 6, yval: 21515750 },
{ xval: 7, yval: 21766710 },
{ xval: 8, yval: 22015580 },
{ xval: 9, yval: 22262500 },
{ xval: 10, yval: 22507620 },
];
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Enable tooltip for Sparkline
Sparkline will displays the sparkline details through tooltip, when the mouse is moved over the sparkline. You can enable tooltip by setting the [visible
] property as true in [tooltipSettings
] object and by injecting SparklineTooltipService
module in the AppModule.
[app.component.ts
]
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { SparklineModule, SparklineTooltipService } from '@syncfusion/ej2-angular-charts'
import { Component } from '@angular/core';
import { SparklineTooltipSettingsModel } from '@syncfusion/ej2-angular-charts';
@Component({
imports: [
SparklineModule
],
providers: [SparklineTooltipService],
standalone: true,
selector: 'app-container',
template: `<ejs-sparkline id='container' width='400px' height='350px' [dataSource]="data" xName="xval" yName="yval" type="Area"
[tooltipSettings]="tooltipSettings">
</ejs-sparkline>`
})
export class AppComponent {
public data: object[] = [
{ xval: 1, yval: 20090440 },
{ xval: 2, yval: 20264080 },
{ xval: 3, yval: 20434180 },
{ xval: 4, yval: 21007310 },
{ xval: 5, yval: 21262640 },
{ xval: 6, yval: 21515750 },
{ xval: 7, yval: 21766710 },
{ xval: 8, yval: 22015580 },
{ xval: 9, yval: 22262500 },
{ xval: 10, yval: 22507620 },
];
public tooltipSettings: SparklineTooltipSettingsModel = {
visible: true,
format: '${xval} : ${yval}'
};
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));