diff --git a/src/traces/sunburst/calc.js b/src/traces/sunburst/calc.js index 4c28d917386..a864e9bd708 100644 --- a/src/traces/sunburst/calc.js +++ b/src/traces/sunburst/calc.js @@ -37,6 +37,11 @@ exports.calc = function(gd, trace) { refs[v] = 1; }; + // treat number `0` as valid + var isValidKey = function(k) { + return k || typeof k === 'number'; + }; + var isValidVal = function(i) { return !hasVals || (isNumeric(vals[i]) && vals[i] >= 0); }; @@ -47,11 +52,11 @@ exports.calc = function(gd, trace) { if(hasIds) { len = Math.min(ids.length, parents.length); - isValid = function(i) { return ids[i] && isValidVal(i); }; + isValid = function(i) { return isValidKey(ids[i]) && isValidVal(i); }; getId = function(i) { return String(ids[i]); }; } else { len = Math.min(labels.length, parents.length); - isValid = function(i) { return labels[i] && isValidVal(i); }; + isValid = function(i) { return isValidKey(labels[i]) && isValidVal(i); }; // TODO We could allow some label / parent duplication // // From AJ: @@ -67,13 +72,13 @@ exports.calc = function(gd, trace) { for(var i = 0; i < len; i++) { if(isValid(i)) { var id = getId(i); - var pid = parents[i] ? String(parents[i]) : ''; + var pid = isValidKey(parents[i]) ? String(parents[i]) : ''; var cdi = { i: i, id: id, pid: pid, - label: labels[i] ? String(labels[i]) : '' + label: isValidKey(labels[i]) ? String(labels[i]) : '' }; if(hasVals) cdi.v = +vals[i]; diff --git a/test/jasmine/tests/sunburst_test.js b/test/jasmine/tests/sunburst_test.js index 6981b2d9e61..a6ce493d67f 100644 --- a/test/jasmine/tests/sunburst_test.js +++ b/test/jasmine/tests/sunburst_test.js @@ -268,6 +268,28 @@ describe('Test sunburst calc:', function() { expect(Lib.warn).toHaveBeenCalledTimes(1); expect(Lib.warn).toHaveBeenCalledWith('Failed to build sunburst hierarchy. Error: ambiguous: b'); }); + + it('should accept numbers (even `0`) are ids/parents items', function() { + _calc({ + labels: ['Eve', 'Cain', 'Seth', 'Enos', 'Noam', 'Abel', 'Awan', 'Enoch', 'Azura'], + ids: [0, 1, 2, 3, 4, 5, 6, 7, 8], + parents: ['', 0, 0, 2, 2, 0, 0, 6, 0] + }); + + expect(extract('id')).toEqual(['0', '1', '2', '3', '4', '5', '6', '7', '8']); + expect(extract('pid')).toEqual(['', '0', '0', '2', '2', '0', '0', '6', '0']); + }); + + it('should accept mix typed are ids/parents items', function() { + _calc({ + labels: ['Eve', 'Cain', 'Seth', 'Enos', 'Noam', 'Abel', 'Awan', 'Enoch', 'Azura'], + ids: [true, 1, '2', 3, 4, 5, 6, 7, 8], + parents: ['', true, true, 2, 2, 'true', 'true', '6', true] + }); + + expect(extract('id')).toEqual(['true', '1', '2', '3', '4', '5', '6', '7', '8']); + expect(extract('pid')).toEqual(['', 'true', 'true', '2', '2', 'true', 'true', '6', 'true']); + }); }); describe('Test sunburst hover:', function() {