We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Heatmap is not updated/re-rendered after datasource is changed

Hi I'm trying to change the datasource of the heatmap control, but it is not re-rendering. Please advise.

//Component
import { Component, OnInit } from '@angular/core';
import { HeatMap } from '@syncfusion/ej2-heatmap';
import { DataAlertService } from 'src/app/_services/data-alert.service';
import { first } from 'rxjs/operators';
import { DataAlert } from 'src/app/_models/dataAlert';
import { interval } from 'rxjs';

import * as moment from 'moment';

@Component({
selector: 'heatmap',
templateUrl: './heatmap.component.html',
styleUrls: ['./heatmap.component.css']
})
export class HeatmapComponent implements OnInit {

dataSource: Object[] = [
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]
];

alerts: DataAlert[] = [];

height = '200';

parameters = ['p1', 'p2', 'p3', 'p4', 'p5', 'p6'];
labels = [];
interval = 60;
slices:number = 5;
alertMatrix;
refreshTime = 5000;

xAxis: Object = {
labels: this.labels,
};
yAxis: Object = {
labels: this.parameters,
};

public cellSettings: Object = {
showLabel: false,
};

public paletteSettings: Object = {
palette: [{ value: 0, color: '#e6e6e6' },
{ value: 50, color: '#6C5B7B' },
{ value: 100, color: '#ff0000' },
]
};

constructor(private dataAlertService: DataAlertService) {

}

public ngOnInit() {

//set the labels
var now = moment();
var labelTick;
for (var i = 0; i< (this.interval / this.slices); i++) {
labelTick = moment(now).subtract(this.interval, 'minutes').add(i * this.slices, 'minutes').format('hh:mm')
this.labels.push(labelTick);
}

//create a matrix parameters x (interval / slices)
this.alertMatrix = Array(this.interval / this.slices).fill(0).map(() => Array(this.parameters.length).fill(0));
//this.alertMatrix = Array(this.parameters.length).fill(0).map(() => Array(this.interval / this.slices).fill(0));
console.log(this.alertMatrix);

//get all alerts within a certain interval
this.dataAlertService.getAllAlertsByInterval(this.interval).subscribe(
data => {
this.alerts = data;
this.slices;
},
error => console.error(error),
() => {
console.log("observable complete");
this.categorize();
}
);

//refresh the view if refreshTime is set
if (this.refreshTime != 0) {
interval(this.refreshTime).subscribe(() => {
this.dataAlertService.getAllAlertsByInterval(this.interval).pipe(first()).subscribe(
data => {
this.alerts = data;
},
error => console.error(error),
() => {
console.log("observable complete");
this.categorize();
}
);
});
}
}

private categorize() {
console.log("categorize accessed");

let bucketIndex;
let buckets = (this.interval / this.slices) - 1;
var now = moment();
for (var i = 0; i < this.alerts.length; i++) {
var createdMin = moment(this.alerts[i]['created']).format("mm");
var duration = moment.duration(now.diff(moment(this.alerts[i]['created'])));

bucketIndex = Math.trunc(duration.asMinutes() / this.slices);
console.log('durcation: ' + duration.asMinutes());
console.log('bucketIndex: ' + bucketIndex)

//find the index of the parameter
var foundIndex = this.parameters.findIndex(x => x == this.alerts[i]['parameter'])
console.log('foundIndex' + foundIndex);

//increment if index could be found
if (foundIndex != -1 && this.alertMatrix[buckets - bucketIndex][foundIndex] < 100) {
this.alertMatrix[buckets - bucketIndex][foundIndex] = 100;
}
}

this.dataSource = this.alertMatrix;
this.xAxis = { labels: this.labels }
console.log(this.dataSource);
}

}

//View

<div class="card">

<div class="card-header">
Alerts
</div>

<div class="card-body">
<ejs-heatmap id='container' style="display:block;" [dataSource]='dataSource' [xAxis]='xAxis' [yAxis]='yAxis'
[cellSettings]='cellSettings' [paletteSettings]='paletteSettings' [height]='height'>
</ejs-heatmap>
</div>

</div>

3 Replies

PP Pooja Priya Krishna Moorthy Syncfusion Team June 5, 2019 09:32 AM

Hi Green, 
  
Greetings from Syncfusion support. 
 
To update the data source we can directly assign the data to the dataSource property. From the code snippet shared by you we found that categorize method is called for every 5 seconds as you set the  ‘refreshTime’ value as 5000. We need to skip this process while dynamically changing data source. Please find the code example below. 
  
  
private categorize() 
        { 
            if (!this.clearTime) 
            { 
                //... 
            } 
        } 
changeData() : void { 
            this.clearTime = true; 
            this.dataSource = [ 
                [100, 90, 80, 70, 60, 30], 
                //... 
            ] 
        } 
  
Please find the below sample link. 
 
Please get back to us if you require further assistance on this. 
  
Regards, 
Pooja Priya K 
 



GL green lomu June 11, 2019 10:06 AM

Hi Pooja - 

thanks for working on this. 

Unfortunately - So far I could not understand your suggested solution and wasn't able to apply it. 

Is there any documentation about the changeData method? Is this a function that is called by the component? It seems as if it not accessed at all. 

Thanks for providing an answer. 

Kind Regards - 
Green


PP Pooja Priya Krishna Moorthy Syncfusion Team June 12, 2019 11:53 AM

Hi Green, 
 
changeData is not a public method of Heatmap component. This is the method bound to the onclick event of button element. On button click action, updated data can be directly bound to the dataSource property of Heatmap component. When updated data is set to the dataSource property, heatmap is refreshed based on updated data. Please find the code example below. 
 
[HTML] 
<button (click)="changeData()"> change</button> 
 
[TS] 
changeData(): void { 
            this.clearTime = true; 
            this.dataSource = [ 
                [100, 90, 80, 70, 60, 30], 
                [0, 0, 0, 0, 0, 0], 
                [0, 0, 0, 0, 0, 0], 
            ] 
    } 
 
Please get back to us if you require further assistance on this. 
 
Regards, 
Pooja Priya K 
 


Loader.
Live Chat Icon For mobile
Up arrow icon