Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/traces/sunburst/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand All @@ -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:
Expand All @@ -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];
Expand Down
22 changes: 22 additions & 0 deletions test/jasmine/tests/sunburst_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down