Skip to content

Commit 3004a9a

Browse files
committed
fix more percentage edge cases - handle branchvalues remainder
1 parent 1fdf574 commit 3004a9a

8 files changed

+56
-12
lines changed

src/traces/sunburst/plot.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -488,12 +488,9 @@ exports.formatSliceLabel = function(pt, entry, trace, cd, fullLayout) {
488488
var cdi = pt.data.data;
489489
var hierarchy = cd0.hierarchy;
490490
var ref;
491-
var calcPercent = function() {
492-
var refData = ref.data ? ref.data.data : ref;
493491

494-
return cdi.hasOwnProperty('v') ?
495-
cdi.v / refData.v :
496-
cdi.value / refData.value;
492+
var calcPercent = function() {
493+
return (trace.branchvalues ? cdi.v : cdi.value) / (ref.value || ref.v);
497494
};
498495

499496
if(trace.type === 'treemap' && helpers.isHeader(pt, trace)) {
@@ -549,15 +546,15 @@ exports.formatSliceLabel = function(pt, entry, trace, cd, fullLayout) {
549546
addPercent(key);
550547
};
551548

552-
if(hasFlag('percent parent') && parent) {
549+
if(hasFlag('percent parent')) {
553550
ref = parent;
554551
makePercent('parent');
555552
}
556-
if(hasFlag('percent entry') && parent) {
553+
if(hasFlag('percent entry')) {
557554
ref = entry;
558555
makePercent('entry');
559556
}
560-
if(hasFlag('percent root') && (parent || helpers.isLeaf(pt))) {
557+
if(hasFlag('percent root')) {
561558
ref = hierarchy;
562559
makePercent('root');
563560
}
-362 Bytes
Loading
-2.76 KB
Loading
-48 Bytes
Loading
Loading
Loading

test/image/mocks/treemap_with-without_values.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"type": "treemap",
7575
"textinfo": "label+value+percent parent+percent entry+percent root+text+current path",
7676
"hoverinfo": "all",
77-
"name": "with values",
77+
"name": "with total values",
7878
"level": "Oscar",
7979
"labels": [
8080
"Alpha",
@@ -221,7 +221,7 @@
221221
"type": "treemap",
222222
"textinfo": "label+value+percent parent+percent entry+percent root+text+current path",
223223
"hoverinfo": "all",
224-
"name": "with values",
224+
"name": "with remainder values",
225225
"level": "Oscar",
226226
"labels": [
227227
"Alpha",

test/jasmine/tests/sunburst_test.js

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,9 +1375,10 @@ describe('Test sunburst texttemplate without `values` should work:', function()
13751375
]);
13761376
});
13771377

1378-
describe('Test sunburst texttemplate with `values` should work:', function() {
1378+
describe('Test sunburst texttemplate with *total* `values` should work:', function() {
13791379
checkTextTemplate([{
13801380
type: 'sunburst',
1381+
branchvalues: 'total',
13811382
labels: ['Eve', 'Cain', 'Seth', 'Enos', 'Noam', 'Abel', 'Awan', 'Enoch', 'Azura'],
13821383
parents: ['', 'Eve', 'Eve', 'Seth', 'Seth', 'Eve', 'Eve', 'Awan', 'Eve' ],
13831384
values: [65, 14, 12, 10, 2, 6, 6, 1, 4],
@@ -1391,7 +1392,7 @@ describe('Test sunburst texttemplate with `values` should work:', function() {
13911392
['%{percentEntry} of %{entry}', ['100% of Eve', '22% of Eve', '18% of Eve', '9% of Eve', '9% of Eve', '6% of Eve', '15% of Eve', '3% of Eve', '2% of Eve']],
13921393
['%{percentParent} of %{parent}', [
13931394
'%{percentParent} of %{parent}', // TODO: what should be printed for the parent of root?
1394-
'83% of Seth', '22% of Eve', '18% of Eve', '9% of Eve', '9% of Eve', '6% of Eve', '17% of Seth', '17% of Awan'
1395+
'22% of Eve', '18% of Eve', '9% of Eve', '9% of Eve', '6% of Eve', '83% of Seth', '17% of Seth', '17% of Awan'
13951396
]],
13961397
[
13971398
[
@@ -1419,3 +1420,49 @@ describe('Test sunburst texttemplate with `values` should work:', function() {
14191420
]
14201421
]);
14211422
});
1423+
1424+
describe('Test sunburst texttemplate with *remainder* `values` should work:', function() {
1425+
checkTextTemplate([{
1426+
type: 'sunburst',
1427+
branchvalues: 'remainder',
1428+
labels: ['Eve', 'Cain', 'Seth', 'Enos', 'Noam', 'Abel', 'Awan', 'Enoch', 'Azura'],
1429+
parents: ['', 'Eve', 'Eve', 'Seth', 'Seth', 'Eve', 'Eve', 'Awan', 'Eve' ],
1430+
values: [65, 14, 12, 10, 2, 6, 6, 1, 4],
1431+
text: ['sixty-five', 'fourteen', 'twelve', 'ten', 'two', 'six', 'six', 'one', 'four']
1432+
}], 'g.slicetext', [
1433+
['color: %{color}', ['color: rgba(0,0,0,0)', 'color: #1f77b4', 'color: #ff7f0e', 'color: #2ca02c', 'color: #d62728', 'color: #9467bd', 'color: #ff7f0e', 'color: #ff7f0e', 'color: #d62728']],
1434+
['label: %{label}', ['label: Eve', 'label: Cain', 'label: Seth', 'label: Enos', 'label: Noam', 'label: Abel', 'label: Awan', 'label: Enoch', 'label: Azura']],
1435+
['value: %{value}', ['value: 65', 'value: 14', 'value: 12', 'value: 10', 'value: 2', 'value: 6', 'value: 6', 'value: 1', 'value: 4']],
1436+
['text: %{text}', ['text: sixty-five', 'text: fourteen', 'text: twelve', 'text: ten', 'text: two', 'text: six', 'text: six', 'text: one', 'text: four']],
1437+
['%{percentRoot} of %{root}', ['54% of Eve', '10% of Eve', '12% of Eve', '5% of Eve', '5% of Eve', '3% of Eve', '8% of Eve', '2% of Eve', '1% of Eve']],
1438+
['%{percentEntry} of %{entry}', ['54% of Eve', '10% of Eve', '12% of Eve', '5% of Eve', '5% of Eve', '3% of Eve', '8% of Eve', '2% of Eve', '1% of Eve']],
1439+
['%{percentParent} of %{parent}', [
1440+
'%{percentParent} of %{parent}', // TODO: what should be printed for the parent of root?
1441+
'10% of Eve', '12% of Eve', '5% of Eve', '5% of Eve', '3% of Eve', '42% of Seth', '8% of Seth', '14% of Awan'
1442+
]],
1443+
[
1444+
[
1445+
'label: %{label}',
1446+
'text: %{text}',
1447+
'value: %{value}',
1448+
'%{percentRoot} of %{root}',
1449+
'%{percentEntry} of %{entry}',
1450+
'%{percentParent} of %{parent}',
1451+
'%{percentParent} of %{parent}',
1452+
'%{percentParent} of %{parent}',
1453+
'color: %{color}'
1454+
],
1455+
[
1456+
'label: Eve',
1457+
'text: fourteen',
1458+
'value: 12',
1459+
'5% of Eve',
1460+
'5% of Eve',
1461+
'8% of Eve',
1462+
'2% of Eve',
1463+
'14% of Awan',
1464+
'color: #9467bd'
1465+
]
1466+
]
1467+
]);
1468+
});

0 commit comments

Comments
 (0)