Search results

Getting Started

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 the package.json.

Registering Sparkline Module

  • Import Sparkline module into Angular application(app.module.ts) from the package @syncfusion/ej2-angular-charts [src/app/app.module.ts].
import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
// import the SparklineModule for the Sparkline component
import { SparklineModule } from '@syncfusion/ej2-angular-charts';
import { AppComponent }  from './app.component';

@NgModule({
  //declaration of sparkline module into NgModule
  imports:      [ BrowserModule, SparklineModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }
  • Modify the template in app.component.ts file to render the ej2-angular-charts component [src/app/app.component.ts].
import { Component, ViewEncapsulation } from '@angular/core';

@Component({
  selector: 'app-container',
  // 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 { Component } from '@angular/core';

@Component({
    selector: 'app-container',
    // 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 { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { AppComponent } from './app.component';
    import { SparklineComponent, SparklineTooltipService } from '@syncfusion/ej2-angular-charts';

    @NgModule({
        imports: [
            BrowserModule,
        ],
        declarations: [AppComponent, SparklineComponent],
        bootstrap: [AppComponent],
        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]

Source
Preview
app.component.ts
app.module.ts
main.ts
import { Component } from '@angular/core';

@Component({
    selector: 'app-container',
    template: `<ejs-sparkline id='container' width='400px' height='350px' [dataSource]="data" xName="xval" yName="yval">
    </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 { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { SparklineModule, SparklineTooltipService } from '@syncfusion/ej2-angular-charts';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule, SparklineModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
	providers: [SparklineTooltipService]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

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]

Source
Preview
app.component.ts
app.module.ts
main.ts
import { Component } from '@angular/core';

@Component({
    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 { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { SparklineModule, SparklineTooltipService } from '@syncfusion/ej2-angular-charts';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule, SparklineModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
	providers: [SparklineTooltipService]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

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]

Source
Preview
app.component.ts
app.module.ts
main.ts
import { Component } from '@angular/core';
import { TooltipSettingsModel } from '@syncfusion/ej2-charts';

@Component({
    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: TooltipSettingsModel = {
        visible: true,
        format: '${xval} : ${yval}'
    };
}
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { SparklineModule, SparklineTooltipService } from '@syncfusion/ej2-angular-charts';

/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule, SparklineModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent],
	providers: [SparklineTooltipService]
})
export class AppModule { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);