Skip to content

Commit 7a7a3f8

Browse files
authored
Merge pull request #3903 from plotly/sunburst-ids-as-numbers
Accept number `0` as valid item in sunburst ids/parents
2 parents 554d3d1 + 4206c86 commit 7a7a3f8

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

Diff for: src/traces/sunburst/calc.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ exports.calc = function(gd, trace) {
3737
refs[v] = 1;
3838
};
3939

40+
// treat number `0` as valid
41+
var isValidKey = function(k) {
42+
return k || typeof k === 'number';
43+
};
44+
4045
var isValidVal = function(i) {
4146
return !hasVals || (isNumeric(vals[i]) && vals[i] >= 0);
4247
};
@@ -47,11 +52,11 @@ exports.calc = function(gd, trace) {
4752

4853
if(hasIds) {
4954
len = Math.min(ids.length, parents.length);
50-
isValid = function(i) { return ids[i] && isValidVal(i); };
55+
isValid = function(i) { return isValidKey(ids[i]) && isValidVal(i); };
5156
getId = function(i) { return String(ids[i]); };
5257
} else {
5358
len = Math.min(labels.length, parents.length);
54-
isValid = function(i) { return labels[i] && isValidVal(i); };
59+
isValid = function(i) { return isValidKey(labels[i]) && isValidVal(i); };
5560
// TODO We could allow some label / parent duplication
5661
//
5762
// From AJ:
@@ -67,13 +72,13 @@ exports.calc = function(gd, trace) {
6772
for(var i = 0; i < len; i++) {
6873
if(isValid(i)) {
6974
var id = getId(i);
70-
var pid = parents[i] ? String(parents[i]) : '';
75+
var pid = isValidKey(parents[i]) ? String(parents[i]) : '';
7176

7277
var cdi = {
7378
i: i,
7479
id: id,
7580
pid: pid,
76-
label: labels[i] ? String(labels[i]) : ''
81+
label: isValidKey(labels[i]) ? String(labels[i]) : ''
7782
};
7883

7984
if(hasVals) cdi.v = +vals[i];

Diff for: test/jasmine/tests/sunburst_test.js

+22
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,28 @@ describe('Test sunburst calc:', function() {
268268
expect(Lib.warn).toHaveBeenCalledTimes(1);
269269
expect(Lib.warn).toHaveBeenCalledWith('Failed to build sunburst hierarchy. Error: ambiguous: b');
270270
});
271+
272+
it('should accept numbers (even `0`) are ids/parents items', function() {
273+
_calc({
274+
labels: ['Eve', 'Cain', 'Seth', 'Enos', 'Noam', 'Abel', 'Awan', 'Enoch', 'Azura'],
275+
ids: [0, 1, 2, 3, 4, 5, 6, 7, 8],
276+
parents: ['', 0, 0, 2, 2, 0, 0, 6, 0]
277+
});
278+
279+
expect(extract('id')).toEqual(['0', '1', '2', '3', '4', '5', '6', '7', '8']);
280+
expect(extract('pid')).toEqual(['', '0', '0', '2', '2', '0', '0', '6', '0']);
281+
});
282+
283+
it('should accept mix typed are ids/parents items', function() {
284+
_calc({
285+
labels: ['Eve', 'Cain', 'Seth', 'Enos', 'Noam', 'Abel', 'Awan', 'Enoch', 'Azura'],
286+
ids: [true, 1, '2', 3, 4, 5, 6, 7, 8],
287+
parents: ['', true, true, 2, 2, 'true', 'true', '6', true]
288+
});
289+
290+
expect(extract('id')).toEqual(['true', '1', '2', '3', '4', '5', '6', '7', '8']);
291+
expect(extract('pid')).toEqual(['', 'true', 'true', '2', '2', 'true', 'true', '6', 'true']);
292+
});
271293
});
272294

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

0 commit comments

Comments
 (0)