Getting started with Angular Range navigator component
11 Jul 20247 minutes to read
This section explains you the steps required to create a simple RangeNavigator and demonstrate the basic usage of the RangeNavigator 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 Chart 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 RangeNavigator component
Modify the template in app.component.ts
file to render the ej2-angular-charts
component [src/app/app.component.ts]
.
import { ChartModule, RangeNavigatorModule } from '@syncfusion/ej2-angular-charts';
import { Component,ViewEncapsulation } from '@angular/core';
@Component({
imports: [
ChartModule, RangeNavigatorModule
],
standalone: true,
selector: 'app-root',
// specifies the template string for the RangeNavigator component
template: `<ejs-rangenavigator id="rn-container"></ejs-rangenavigator>`,
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
Module Injection
To create range navigator with additional features, inject the required modules. The following modules are used to extend rangenavigator’s basic functionality.
-
AreaSeriesService
- Inject this module to use area series. -
DateTimeService
- Inject this module to use date time axis. -
RangeTooltipService
- Inject this module to show the tooltip.
These modules should be injected to the provider section as follows,
import { Component } from '@angular/core';
import { ChartModule, RangeNavigatorModule } from '@syncfusion/ej2-angular-charts';
import { AreaSeriesService, DateTimeService, RangeTooltipService } from '@syncfusion/ej2-angular-charts';
@Component({
imports: [
ChartModule,
RangeNavigatorModule
],
standalone: true,
providers: [ AreaSeriesService, DateTimeService, RangeTooltipService ]
})
Populate Range Navigator with Data
Now, we are going to provide data to the range navigator. Add a series object to the range navigator by using series
property. Now map the field names x and y in the JSON data to the xName
and yName
properties of the series, then set the JSON data to dataSource property.
Since the JSON contains Datetime data, set the valueType
as DateTime
. By default, the axis valueType is Numeric.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { ChartModule, RangeNavigatorModule } from '@syncfusion/ej2-angular-charts'
import { AreaSeriesService, DateTimeService} from '@syncfusion/ej2-angular-charts'
import { Component, OnInit } from '@angular/core';
import { datasrc } from './datasource'
@Component({
imports: [
ChartModule, RangeNavigatorModule
],
providers: [ AreaSeriesService, DateTimeService ],
standalone: true,
selector: 'app-container',
template: `<ejs-rangenavigator id="rn-container" valueType='DateTime' [value]='value'>
<e-rangenavigator-series-collection>
<e-rangenavigator-series [dataSource]='chartData' type='Area' xName='x' yName='y' width=2>
</e-rangenavigator-series>
</e-rangenavigator-series-collection>
</ejs-rangenavigator>`
})
export class AppComponent implements OnInit {
public value?: Object[];
public chartData?: Object[];
ngOnInit(): void {
this.value = [new Date('2017-09-01'), new Date('2018-02-01')];
this.chartData = datasrc;
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Note: Get data from here.
The sample should look like our default, don’t worry about the gradient color, let it takes the default color.
Enable Tooltip
The tooltip is useful to show the selected data. You can enable tooltip by setting the enable property as true in tooltip
object and by injecting RangeTooltipService
module into the @NgModule.providers
.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { ChartModule, RangeNavigatorModule } from '@syncfusion/ej2-angular-charts'
import { AreaSeriesService, DateTimeService, RangeTooltipService} from '@syncfusion/ej2-angular-charts'
import { Component, OnInit } from '@angular/core';
import { datasrc } from './data'
@Component({
imports: [
ChartModule, RangeNavigatorModule
],
providers: [ AreaSeriesService, DateTimeService, RangeTooltipService ],
standalone: true,
selector: 'app-container',
template: `<ejs-rangenavigator id="rn-container" valueType='DateTime' [value]='value' [tooltip]='tooltip'>
<e-rangenavigator-series-collection>
<e-rangenavigator-series [dataSource]='chartData' type='Area' xName='x' yName='y' width=2>
</e-rangenavigator-series>
</e-rangenavigator-series-collection>
</ejs-rangenavigator>`
})
export class AppComponent implements OnInit {
public value?: Object[];
public chartData?: Object[];
public tooltip?: Object;
ngOnInit(): void {
this.value = [new Date('2017-09-01'), new Date('2018-02-01')];
this.chartData = datasrc;
this.tooltip = { enable: true, displayMode: 'Always' };
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));