Skip to content

Commit 4a2f7a5

Browse files
committed
do not render hierarchy when partial sums > sector value
- add d3.hierachy error message - add appropriate tests
1 parent b59be1d commit 4a2f7a5

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/traces/sunburst/calc.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,11 @@ exports.calc = function(gd, trace) {
130130
.id(function(d) { return d.id; })
131131
.parentId(function(d) { return d.pid; })(cd);
132132
} catch(e) {
133-
return Lib.warn('Failed to build sunburst hierarchy.');
133+
return Lib.warn('Failed to build sunburst hierarchy. Error: ' + e.message);
134134
}
135135

136136
var hierarchy = d3Hierarchy.hierarchy(root);
137+
var failed = false;
137138

138139
if(hasVals) {
139140
switch(trace.branchvalues) {
@@ -149,7 +150,7 @@ exports.calc = function(gd, trace) {
149150
return a + c.data.data.v;
150151
}, 0);
151152
if(v < partialSum) {
152-
d.value = partialSum;
153+
failed = true;
153154
return Lib.warn([
154155
'Total value for node', d.data.data.id,
155156
'is smaller than the sum of its children.'
@@ -165,6 +166,8 @@ exports.calc = function(gd, trace) {
165166
hierarchy.count();
166167
}
167168

169+
if(failed) return;
170+
168171
// TODO add way to sort by height also?
169172
hierarchy.sort(function(a, b) { return b.value - a.value; });
170173

test/jasmine/tests/sunburst_test.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -233,20 +233,41 @@ describe('Test sunburst calc:', function() {
233233
expect(Lib.warn).toHaveBeenCalledTimes(0);
234234
});
235235

236-
it('should warn when values under *branchvalues:total* do not add up', function() {
236+
it('should warn when values under *branchvalues:total* do not add up and not show trace', function() {
237237
_calc({
238238
labels: ['Root', 'A', 'B', 'b'],
239239
parents: ['', 'Root', 'Root', 'B'],
240240
values: [0, 1, 2, 3],
241241
branchvalues: 'total'
242242
});
243243

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

246246
expect(Lib.warn).toHaveBeenCalledTimes(2);
247247
expect(Lib.warn.calls.allArgs()[0][0]).toBe('Total value for node Root is smaller than the sum of its children.');
248248
expect(Lib.warn.calls.allArgs()[1][0]).toBe('Total value for node B is smaller than the sum of its children.');
249249
});
250+
251+
it('should warn labels/parents lead to ambiguous hierarchy', function() {
252+
_calc({
253+
labels: ['Root', 'A', 'A', 'B'],
254+
parents: ['', 'Root', 'Root', 'A']
255+
});
256+
257+
expect(Lib.warn).toHaveBeenCalledTimes(1);
258+
expect(Lib.warn).toHaveBeenCalledWith('Failed to build sunburst hierarchy. Error: ambiguous: A');
259+
});
260+
261+
it('should warn ids/parents lead to ambiguous hierarchy', function() {
262+
_calc({
263+
labels: ['label 1', 'label 2', 'label 3', 'label 4'],
264+
ids: ['a', 'b', 'b', 'c'],
265+
parents: ['', 'a', 'a', 'b']
266+
});
267+
268+
expect(Lib.warn).toHaveBeenCalledTimes(1);
269+
expect(Lib.warn).toHaveBeenCalledWith('Failed to build sunburst hierarchy. Error: ambiguous: b');
270+
});
250271
});
251272

252273
describe('Test sunburst hover:', function() {

0 commit comments

Comments
 (0)