Search results

Getting started with Angular HeatMap Chart component

16 Sep 2021 / 11 minutes to read

This section explains the steps required to create a heat map and demonstrates the basic usage of the heat map control.

Setup Angular Environment

You can use Angular CLI to setup your Angular applications. To install Angular CLI use the following command.

Copied to clipboard
npm install -g @angular/cli

Create an Angular Application

Start a new Angular application using below Angular CLI command.

Copied to clipboard
ng new my-app
cd my-app

Adding Syncfusion Heatmap package

  • All the available Essential JS 2 packages are published in npmjs.com registry.
  • Install heatmap packages using below command.
Copied to clipboard
npm install @syncfusion/ej2-angular-heatmap --save
Copied to clipboard
The —save will instruct NPM to include the heatmap package inside of the dependencies section of the package.json.

Registering Heatmap Module

  • Import Heatmap module into Angular application(app.module.ts) from the package @syncfusion/ej2-ng-heatmap [src/app/app.module.ts].
Copied to clipboard
import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
// import the HeatMapModule for the heatmap component
import { HeatMapModule } from '@syncfusion/ej2-angular-heatmap';
import { AppComponent }  from './app.component';

@NgModule({
  //declaration of ej2-ng-heatmap module into NgModule
  imports:      [ BrowserModule, HeatMapModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

Add Heatmap component

  • Modify the template in app.component.ts file to render the ej2-ng-heatmap component [src/app/app.component.ts].
Copied to clipboard
import { Component, ViewEncapsulation } from '@angular/core';

@Component({
  selector: 'my-app',
  // specifies the template string for the Heatmap component
  template: `<ejs-heatmap id='heatmap-container'></ejs-heatmap>`,
  encapsulation: ViewEncapsulation.None
})
export class AppComponent  { }

Now use the my-app in the index.html instead of default one.

Copied to clipboard
<my-app></my-app>
  • Use the npm run start command to run the application in the browser.
Copied to clipboard
npm start

The below example shows a basic Heatmap.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component, ViewEncapsulation } from '@angular/core';

@Component({
selector: 'my-app',
// specifies the template string for the HeatMap component
template: `<ejs-heatmap id="heatmap-container"></ejs-heatmap>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent {

}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HeatMapModule} from '@syncfusion/ej2-angular-heatmap';

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

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

Module injection

The heat map components are segregated into individual feature-wise modules. To use its feature, you need to inject its feature module using the HeatMap.Inject() method. In the current application,the basic heat map is modified to visualize sales revenue data for week, and the tooltip and legend features of the heat map are used. Find the relevant feature modules and descriptions as follows.

  • LegendService - Provides the legend feature by injecting it.
  • TooltipService - Provides the tooltip feature by injecting it.

Now, import the above-mentioned modules from the heat map package and inject them into the heat map component as follows.

Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HeatmapComponent, Legend, Tooltip } from '@syncfusion/ej2-ng-heatmap';

@NgModule({
   imports: [
       BrowserModule,
   ],
   declarations: [AppComponent, HeatMapComponent],
   bootstrap: [AppComponent],
   providers: [ HeatmapComponent, LegendService, TooltipService ]
})

Populate heat map with data

This section explains how to populate the following two-dimensional array data to the heat map.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component, ViewEncapsulation } from '@angular/core';
import { HeatMap } from '@syncfusion/ej2-heatmap';

@Component({
selector: 'my-app',
template:
   `<ejs-heatmap id='container' style="display:block;" [dataSource]='dataSource'>
    </ejs-heatmap>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent{
    // Data for heatmap
     dataSource: Object[] = [
       [73, 39, 26, 39, 94, 0],
       [93, 58, 53, 38, 26, 68],
       [99, 28, 22, 4, 66, 90],
       [14, 26, 97, 69, 69, 3],
       [7, 46, 47, 47, 88, 6],
       [41, 55, 73, 23, 3, 79],
       [56, 69, 21, 86, 3, 33],
       [45, 7, 53, 81, 95, 79],
       [60, 77, 74, 68, 88, 51],
       [25, 25, 10, 12, 78, 14],
       [25, 56, 55, 58, 12, 82],
       [74, 33, 88, 23, 86, 59]
    ];
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HeatMapModule } from '@syncfusion/ej2-angular-heatmap';

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

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

Enable axis labels

You can add axis labels to the heat map and format those labels using the xAxis and yAxis properties. Axis labels provide additional information about the data points populated in the heat map.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component, ViewEncapsulation } from '@angular/core';
import { HeatMap } from '@syncfusion/ej2-heatmap';

@Component({
selector: 'my-app',
template:
   `<ejs-heatmap id='container' style="display:block;" [dataSource]='dataSource' [xAxis]='xAxis' [yAxis]='yAxis'>
    </ejs-heatmap>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent{
    // Data for heatmap
    dataSource: Object[] = [
       [73, 39, 26, 39, 94, 0],
       [93, 58, 53, 38, 26, 68],
       [99, 28, 22, 4, 66, 90],
       [14, 26, 97, 69, 69, 3],
       [7, 46, 47, 47, 88, 6],
       [41, 55, 73, 23, 3, 79],
       [56, 69, 21, 86, 3, 33],
       [45, 7, 53, 81, 95, 79],
       [60, 77, 74, 68, 88, 51],
       [25, 25, 10, 12, 78, 14],
       [25, 56, 55, 58, 12, 82],
       [74, 33, 88, 23, 86, 59]
    ];
    xAxis: Object = {
    labels: ['Nancy', 'Andrew', 'Janet', 'Margaret', 'Steven', 'Michael', 'Robert',
        'Laura', 'Anne', 'Paul', 'Karin', 'Mario'],
};
yAxis: Object = {
    labels: ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'],
};
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HeatMapModule} from '@syncfusion/ej2-angular-heatmap';

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

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

Add heat map title

Add a title using the titleSettings property to the heat map to provide quick information to the user about the data populated in the heat map.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component, ViewEncapsulation } from '@angular/core';
import { HeatMap } from '@syncfusion/ej2-heatmap';

@Component({
selector: 'my-app',
template:
   `<ejs-heatmap id='container' style="display:block;"  [titleSettings]='titleSettings' [dataSource]='dataSource' [xAxis]='xAxis' [yAxis]='yAxis'>
    </ejs-heatmap>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent{
 titleSettings: Object = {
    text: 'Sales Revenue per Employee (in 1000 US$)',
    textStyle: {
        size: '15px',
        fontWeight: '500',
        fontStyle: 'Normal'
    }
};
    // Data for heatmap
     dataSource: Object[] = [
       [73, 39, 26, 39, 94, 0],
       [93, 58, 53, 38, 26, 68],
       [99, 28, 22, 4, 66, 90],
       [14, 26, 97, 69, 69, 3],
       [7, 46, 47, 47, 88, 6],
       [41, 55, 73, 23, 3, 79],
       [56, 69, 21, 86, 3, 33],
       [45, 7, 53, 81, 95, 79],
       [60, 77, 74, 68, 88, 51],
       [25, 25, 10, 12, 78, 14],
       [25, 56, 55, 58, 12, 82],
       [74, 33, 88, 23, 86, 59]
    ];
    xAxis: Object = {
    labels: ['Nancy', 'Andrew', 'Janet', 'Margaret', 'Steven', 'Michael', 'Robert',
        'Laura', 'Anne', 'Paul', 'Karin', 'Mario'],
};
yAxis: Object = {
    labels: ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'],
};
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HeatMapModule} from '@syncfusion/ej2-angular-heatmap';
/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule, HeatMapModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

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

Enable legend

Use a legend for the heat map in the legendSettings object by setting the visible property to true and injecting the Legend module using the HeatMap.Inject(Legend) method.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component, ViewEncapsulation } from '@angular/core';
import { HeatMap } from '@syncfusion/ej2-heatmap';

@Component({
selector: 'my-app',
template:
   `<ejs-heatmap id='container' style="display:block;"  [titleSettings]='titleSettings' [dataSource]='dataSource'
   [xAxis]='xAxis' [yAxis]='yAxis' [legendSettings]='legendSettings'>
    </ejs-heatmap>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent{
 titleSettings: Object = {
    text: 'Sales Revenue per Employee (in 1000 US$)',
    textStyle: {
        size: '15px',
        fontWeight: '500',
        fontStyle: 'Normal'
    }
};
    // Data for heatmap
     dataSource: Object[] = [
       [73, 39, 26, 39, 94, 0],
       [93, 58, 53, 38, 26, 68],
       [99, 28, 22, 4, 66, 90],
       [14, 26, 97, 69, 69, 3],
       [7, 46, 47, 47, 88, 6],
       [41, 55, 73, 23, 3, 79],
       [56, 69, 21, 86, 3, 33],
       [45, 7, 53, 81, 95, 79],
       [60, 77, 74, 68, 88, 51],
       [25, 25, 10, 12, 78, 14],
       [25, 56, 55, 58, 12, 82],
       [74, 33, 88, 23, 86, 59]
    ];
    xAxis: Object = {
    labels: ['Nancy', 'Andrew', 'Janet', 'Margaret', 'Steven', 'Michael', 'Robert',
        'Laura', 'Anne', 'Paul', 'Karin', 'Mario'],
};
yAxis: Object = {
    labels: ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'],
};
public legendSettings: Object = {
    visible:true,
    position: 'Right',
    showLabel:true,
    height:'150px'
};
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HeatMapModule, LegendService } from '@syncfusion/ej2-angular-heatmap';

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

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

Add data label

Add data labels to improve the readability of the heat map. This can be achieved by setting the showLabel property to true in the cellSettings object.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component, ViewEncapsulation } from '@angular/core';
import { HeatMap } from '@syncfusion/ej2-heatmap';

@Component({
selector: 'my-app',
template:
   `<ejs-heatmap id='container' style="display:block;" [cellSettings]='cellSettings' [titleSettings]='titleSettings' [dataSource]='dataSource'
   [xAxis]='xAxis' [yAxis]='yAxis' [legendSettings]='legendSettings'>
    </ejs-heatmap>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent{
 titleSettings: Object = {
    text: 'Sales Revenue per Employee (in 1000 US$)',
    textStyle: {
        size: '15px',
        fontWeight: '500',
        fontStyle: 'Normal'
    }
};
    // Data for heatmap
     dataSource: Object[] = [
       [73, 39, 26, 39, 94, 0],
       [93, 58, 53, 38, 26, 68],
       [99, 28, 22, 4, 66, 90],
       [14, 26, 97, 69, 69, 3],
       [7, 46, 47, 47, 88, 6],
       [41, 55, 73, 23, 3, 79],
       [56, 69, 21, 86, 3, 33],
       [45, 7, 53, 81, 95, 79],
       [60, 77, 74, 68, 88, 51],
       [25, 25, 10, 12, 78, 14],
       [25, 56, 55, 58, 12, 82],
       [74, 33, 88, 23, 86, 59]
    ];
    xAxis: Object = {
    labels: ['Nancy', 'Andrew', 'Janet', 'Margaret', 'Steven', 'Michael', 'Robert',
        'Laura', 'Anne', 'Paul', 'Karin', 'Mario'],
};
yAxis: Object = {
    labels: ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'],
};
public legendSettings: Object = {
    visible:true,
    position: 'Right',
    showLabel:true,
    height:'150px'
};
public cellSettings: Object = {
    showLabel: true,
};
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HeatMapModule, LegendService } from '@syncfusion/ej2-angular-heatmap';

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

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

Add custom cell palette

The default palette settings of the heat map cells can be customized by using the paletteSettings property. Using the palette property in paletteSettings object, you can change the color set for the cells. You can change the color mode of the cells to fixed or gradient mode using the type property.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component, ViewEncapsulation } from '@angular/core';
import { HeatMap } from '@syncfusion/ej2-heatmap';

@Component({
selector: 'my-app',
template:
   `<ejs-heatmap id='container' style="display:block;" [paletteSettings]='paletteSettings'
   [cellSettings]='cellSettings' [titleSettings]='titleSettings' [dataSource]='dataSource'
   [xAxis]='xAxis' [yAxis]='yAxis' [legendSettings]='legendSettings'>
    </ejs-heatmap>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent{
 titleSettings: Object = {
    text: 'Sales Revenue per Employee (in 1000 US$)',
    textStyle: {
        size: '15px',
        fontWeight: '500',
        fontStyle: 'Normal'
    }
};
    // Data for heatmap
     dataSource: Object[] = [
       [73, 39, 26, 39, 94, 0],
       [93, 58, 53, 38, 26, 68],
       [99, 28, 22, 4, 66, 90],
       [14, 26, 97, 69, 69, 3],
       [7, 46, 47, 47, 88, 6],
       [41, 55, 73, 23, 3, 79],
       [56, 69, 21, 86, 3, 33],
       [45, 7, 53, 81, 95, 79],
       [60, 77, 74, 68, 88, 51],
       [25, 25, 10, 12, 78, 14],
       [25, 56, 55, 58, 12, 82],
       [74, 33, 88, 23, 86, 59]
    ];
    xAxis: Object = {
    labels: ['Nancy', 'Andrew', 'Janet', 'Margaret', 'Steven', 'Michael', 'Robert',
        'Laura', 'Anne', 'Paul', 'Karin', 'Mario'],
};
yAxis: Object = {
    labels: ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'],
};
public legendSettings: Object = {
    visible:true,
    position: 'Right',
    showLabel:true,
    height:'150px'
};
public cellSettings: Object = {
    showLabel: true,
};
 public paletteSettings: Object = {
    palette: [{ value: 0, color: '#C06C84' },
    { value: 50, color: '#6C5B7B' },
    { value: 100, color: '#355C7D' },
    ]
};
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HeatMapModule, LegendService } from '@syncfusion/ej2-angular-heatmap';

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

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

Enable tooltip

The tooltip is used when you cannot display information by using the data labels due to space constraints. You can enable the tooltip by setting the showTooltip property to true and injecting the Tooltip module using the HeatMap.Inject(Tooltip) method.

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component, ViewEncapsulation } from '@angular/core';
import { HeatMap } from '@syncfusion/ej2-heatmap';

@Component({
selector: 'my-app',
template:
   `<ejs-heatmap id='container' style="display:block;" showTooltip='true' [paletteSettings]='paletteSettings'
   [cellSettings]='cellSettings' [titleSettings]='titleSettings' [dataSource]='dataSource'
   [xAxis]='xAxis' [yAxis]='yAxis' [legendSettings]='legendSettings'>
    </ejs-heatmap>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent{
 titleSettings: Object = {
    text: 'Sales Revenue per Employee (in 1000 US$)',
    textStyle: {
        size: '15px',
        fontWeight: '500',
        fontStyle: 'Normal'
    }
};
    // Data for heatmap
     dataSource: Object[] = [
       [73, 39, 26, 39, 94, 0],
       [93, 58, 53, 38, 26, 68],
       [99, 28, 22, 4, 66, 90],
       [14, 26, 97, 69, 69, 3],
       [7, 46, 47, 47, 88, 6],
       [41, 55, 73, 23, 3, 79],
       [56, 69, 21, 86, 3, 33],
       [45, 7, 53, 81, 95, 79],
       [60, 77, 74, 68, 88, 51],
       [25, 25, 10, 12, 78, 14],
       [25, 56, 55, 58, 12, 82],
       [74, 33, 88, 23, 86, 59]
    ];
    xAxis: Object = {
    labels: ['Nancy', 'Andrew', 'Janet', 'Margaret', 'Steven', 'Michael', 'Robert',
        'Laura', 'Anne', 'Paul', 'Karin', 'Mario'],
};
yAxis: Object = {
    labels: ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'],
};
public legendSettings: Object = {
    visible:true,
    position: 'Right',
    showLabel:true,
    height:'150px'
};
public cellSettings: Object = {
    showLabel: true,
};
 public paletteSettings: Object = {
    palette: [{ value: 0, color: '#C06C84' },
    { value: 50, color: '#6C5B7B' },
    { value: 100, color: '#355C7D' },
    ]
};
}
Copied to clipboard
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HeatMapModule} from '@syncfusion/ej2-angular-heatmap';
import { LegendService, TooltipService} from '@syncfusion/ej2-angular-heatmap';

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

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