Skip to content

Commit 6014148

Browse files
fix(heatmap): Handle data being set after initialization
Fixes #546
1 parent a91312c commit 6014148

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

src/charts/heatmap/heatmap.component.js

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,30 @@ angular.module('patternfly.charts').component('pfHeatmap', {
307307
});
308308
};
309309

310-
ctrl.updateAll = function () {
311-
// Need to deep watch changes in chart data
310+
var updateDisplay = function () {
311+
setStyles();
312+
313+
if (ctrl.chartDataAvailable !== false && ctrl.data) {
314+
ctrl.loadingDone = true;
315+
316+
// Allow the style change to take effect to update the container size
317+
$timeout(function () {
318+
setSizes();
319+
redraw();
320+
});
321+
}
322+
};
323+
324+
var handleDataUpdate = function () {
312325
prevData = angular.copy(ctrl.data);
326+
updateDisplay();
327+
};
313328

329+
var debounceResize = _.debounce(function () {
330+
updateDisplay();
331+
}, 250, 500);
332+
333+
var updateConfig = function () {
314334
//Allow overriding of defaults
315335
if (ctrl.maxBlockSize === undefined || isNaN(ctrl.maxBlockSize)) {
316336
ctrl.maxSize = 64;
@@ -360,46 +380,36 @@ angular.module('patternfly.charts').component('pfHeatmap', {
360380
}
361381
ctrl.height = ctrl.height || heightDefault;
362382
ctrl.showLegend = ctrl.showLegend || (ctrl.showLegend === undefined);
363-
ctrl.loadingDone = false;
364-
365-
angular.element($window).on('resize', function () {
366-
setSizes();
367-
redraw();
368-
});
369-
370-
ctrl.thisComponent = $element[0].querySelector('.heatmap-pf-svg');
371-
372-
$timeout(function () {
373-
setStyles();
374-
setSizes();
375-
redraw();
376-
});
377383
};
378384

385+
ctrl.loadingDone = false;
386+
379387
ctrl.$onChanges = function (changesObj) {
380388
if (changesObj.chartDataAvailable && !changesObj.chartDataAvailable.isFirstChange()) {
381389
setStyles();
382-
} else {
383-
ctrl.updateAll();
384-
ctrl.loadingDone = true;
390+
} else if (!changesObj.data) {
391+
updateConfig();
392+
updateDisplay();
385393
}
386394
};
387395

388396
ctrl.$doCheck = function () {
389397
// do a deep compare on chartData and config
390398
if (!angular.equals(ctrl.data, prevData)) {
391-
setStyles();
392-
if (ctrl.chartDataAvailable !== false) {
393-
setSizes();
394-
redraw();
395-
}
399+
handleDataUpdate();
396400
}
397401
};
398402

399403
ctrl.$postLink = function () {
400-
setStyles();
401-
setSizes();
402-
redraw();
404+
ctrl.thisComponent = $element[0].querySelector('.heatmap-pf-svg');
405+
updateConfig();
406+
handleDataUpdate();
407+
408+
angular.element($window).on('resize', debounceResize);
409+
};
410+
411+
ctrl.$onDestroy = function () {
412+
angular.element($window).off('resize', debounceResize);
403413
};
404414
}
405415
});

test/charts/heatmap/heatmap.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ describe('Component: pfHeatmap', function() {
99
'charts/heatmap/heatmap-legend.html'
1010
));
1111

12-
beforeEach(inject(function(_$compile_, _$rootScope_) {
12+
beforeEach(inject(function(_$compile_, _$rootScope_, _$timeout_) {
1313
$compile = _$compile_;
1414
$scope = _$rootScope_;
15+
$timeout = _$timeout_;
1516
}));
1617

1718
var compileChart = function (markup, scope) {
1819
var el = $compile(markup)(scope);
1920
scope.$digest();
21+
$timeout.flush();
2022
return angular.element(el);
2123
};
2224

0 commit comments

Comments
 (0)