Skip to content

Commit

Permalink
do not render hierarchy when partial sums > sector value
Browse files Browse the repository at this point in the history
- add d3.hierachy error message
- add appropriate tests
  • Loading branch information
etpinard committed Mar 28, 2019
1 parent b59be1d commit 4a2f7a5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/traces/sunburst/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ exports.calc = function(gd, trace) {
.id(function(d) { return d.id; })
.parentId(function(d) { return d.pid; })(cd);
} catch(e) {
return Lib.warn('Failed to build sunburst hierarchy.');
return Lib.warn('Failed to build sunburst hierarchy. Error: ' + e.message);
}

var hierarchy = d3Hierarchy.hierarchy(root);
var failed = false;

if(hasVals) {
switch(trace.branchvalues) {
Expand All @@ -149,7 +150,7 @@ exports.calc = function(gd, trace) {
return a + c.data.data.v;
}, 0);
if(v < partialSum) {
d.value = partialSum;
failed = true;
return Lib.warn([
'Total value for node', d.data.data.id,
'is smaller than the sum of its children.'
Expand All @@ -165,6 +166,8 @@ exports.calc = function(gd, trace) {
hierarchy.count();
}

if(failed) return;

// TODO add way to sort by height also?
hierarchy.sort(function(a, b) { return b.value - a.value; });

Expand Down
25 changes: 23 additions & 2 deletions test/jasmine/tests/sunburst_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,20 +233,41 @@ describe('Test sunburst calc:', function() {
expect(Lib.warn).toHaveBeenCalledTimes(0);
});

it('should warn when values under *branchvalues:total* do not add up', function() {
it('should warn when values under *branchvalues:total* do not add up and not show trace', function() {
_calc({
labels: ['Root', 'A', 'B', 'b'],
parents: ['', 'Root', 'Root', 'B'],
values: [0, 1, 2, 3],
branchvalues: 'total'
});

expect(extractPt('value')).toEqual([3, 3, 1, 3]);
expect(gd.calcdata[0][0].hierarchy).toBe(undefined, 'no computed hierarchy');

expect(Lib.warn).toHaveBeenCalledTimes(2);
expect(Lib.warn.calls.allArgs()[0][0]).toBe('Total value for node Root is smaller than the sum of its children.');
expect(Lib.warn.calls.allArgs()[1][0]).toBe('Total value for node B is smaller than the sum of its children.');
});

it('should warn labels/parents lead to ambiguous hierarchy', function() {
_calc({
labels: ['Root', 'A', 'A', 'B'],
parents: ['', 'Root', 'Root', 'A']
});

expect(Lib.warn).toHaveBeenCalledTimes(1);
expect(Lib.warn).toHaveBeenCalledWith('Failed to build sunburst hierarchy. Error: ambiguous: A');
});

it('should warn ids/parents lead to ambiguous hierarchy', function() {
_calc({
labels: ['label 1', 'label 2', 'label 3', 'label 4'],
ids: ['a', 'b', 'b', 'c'],
parents: ['', 'a', 'a', 'b']
});

expect(Lib.warn).toHaveBeenCalledTimes(1);
expect(Lib.warn).toHaveBeenCalledWith('Failed to build sunburst hierarchy. Error: ambiguous: b');
});
});

describe('Test sunburst hover:', function() {
Expand Down

0 comments on commit 4a2f7a5

Please sign in to comment.