Skip to content

Commit 8416883

Browse files
committed
rework working with parent references in hover and plot
- move treemap header checks from sunburst plot to treemap/draw_ancestors - cleanup dirty _parents and hierarchy links for pt - display blank label as blank
1 parent 217c0f4 commit 8416883

14 files changed

+511
-104
lines changed

src/traces/sunburst/fx.js

+24-15
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,23 @@ module.exports = function attachFxHandlers(sliceTop, entry, gd, cd, opts) {
4747
var ptNumber = cdi.i;
4848
var isRoot = helpers.isHierarchyRoot(pt);
4949
var isEntry = helpers.isEntry(pt);
50-
var parent = isEntry ? pt._parent : pt.parent;
51-
var parentLabel = !parent ? '' : (parent.data && parent.data.data) ? parent.data.data.label : parent.label;
50+
51+
var rootLabel = hierarchy.data.data.pid;
52+
53+
var parent;
54+
var parentLabel;
55+
var parentValue;
56+
if(isEntry) {
57+
parent = pt.data.parent;
58+
parentLabel = parent ? parent.data.label : helpers.getPtLabel(hierarchy);
59+
parentValue = parent ? helpers.getVal(parent.data) : helpers.getVal(hierarchy);
60+
} else {
61+
parent = pt.parent;
62+
parentLabel = helpers.getPtLabel(parent);
63+
parentValue = helpers.getVal(parent);
64+
}
65+
66+
var val = helpers.getVal(cdi);
5267

5368
var _cast = function(astr) {
5469
return Lib.castOption(traceNow, ptNumber, astr);
@@ -74,18 +89,14 @@ module.exports = function attachFxHandlers(sliceTop, entry, gd, cd, opts) {
7489
var parts = [];
7590
var thisText = [];
7691
var hasFlag = function(flag) { return parts.indexOf(flag) !== -1; };
77-
var getVal = function(d) {
78-
var result = d.hasOwnProperty('v') ? d.v : d.value;
79-
return result !== undefined ? result : hierarchy.value;
80-
};
8192

8293
if(hoverinfo) {
8394
parts = hoverinfo === 'all' ?
8495
traceNow._module.attributes.hoverinfo.flags :
8596
hoverinfo.split('+');
8697
}
8798

88-
hoverPt.label = helpers.getLabelStr(cdi.label);
99+
hoverPt.label = cdi.label;
89100
if(hasFlag('label') && hoverPt.label) thisText.push(hoverPt.label);
90101

91102
if(cdi.hasOwnProperty('v')) {
@@ -108,12 +119,10 @@ module.exports = function attachFxHandlers(sliceTop, entry, gd, cd, opts) {
108119
}
109120
};
110121

111-
var val = getVal(cdi);
112-
113122
var ref2 = parent;
114123
if(ref2) {
115-
hoverPt.percentParent = pt.percentParent = val / getVal(ref2);
116-
hoverPt.parent = pt.parentString = helpers.getLabelString(parentLabel);
124+
hoverPt.percentParent = pt.percentParent = val / parentValue;
125+
hoverPt.parent = pt.parentString = helpers.replaceVoid(parentLabel, rootLabel);
117126
if(hasFlag('percent parent')) {
118127
tx = helpers.formatPercent(hoverPt.percentParent, separators) + ' of ' + hoverPt.parent;
119128
insertPercent();
@@ -122,8 +131,8 @@ module.exports = function attachFxHandlers(sliceTop, entry, gd, cd, opts) {
122131

123132
var ref1 = entry;
124133
if(ref1) {
125-
hoverPt.percentEntry = pt.percentEntry = val / getVal(ref1);
126-
hoverPt.entry = pt.entry = helpers.getLabelString(ref1.data.data.label);
134+
hoverPt.percentEntry = pt.percentEntry = val / helpers.getVal(ref1);
135+
hoverPt.entry = pt.entry = helpers.replaceVoid(helpers.getPtLabel(ref1), rootLabel);
127136
if(hasFlag('percent entry') && !isRoot && !pt.onPathbar) {
128137
tx = helpers.formatPercent(hoverPt.percentEntry, separators) + ' of ' + hoverPt.entry;
129138
insertPercent();
@@ -132,8 +141,8 @@ module.exports = function attachFxHandlers(sliceTop, entry, gd, cd, opts) {
132141

133142
var ref0 = hierarchy;
134143
if(ref0) {
135-
hoverPt.percentRoot = pt.percentRoot = val / getVal(ref0);
136-
hoverPt.root = pt.root = helpers.getLabelString(ref0.data.data.label);
144+
hoverPt.percentRoot = pt.percentRoot = val / helpers.getVal(ref0);
145+
hoverPt.root = pt.root = helpers.replaceVoid(helpers.getPtLabel(ref0), rootLabel);
137146
if(hasFlag('percent root') && !isRoot) {
138147
tx = helpers.formatPercent(hoverPt.percentRoot, separators) + ' of ' + hoverPt.root;
139148
insertPercent();

src/traces/sunburst/helpers.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ exports.getPtId = function(pt) {
5757
return pt.data.data.id;
5858
};
5959

60+
exports.getPtLabel = function(pt) {
61+
return pt.data.data.label;
62+
};
63+
64+
exports.replaceVoid = function(label, rootLabel) {
65+
return label === undefined ? rootLabel : label;
66+
};
67+
68+
exports.getVal = function(d) {
69+
return d.hasOwnProperty('v') ? d.v : d.value;
70+
};
71+
6072
exports.isHierarchyRoot = function(pt) {
6173
return pt.data.data.pid === '';
6274
};
@@ -145,15 +157,6 @@ exports.isHeader = function(pt, trace) { // it is only used in treemap.
145157
return !(exports.isLeaf(pt) || pt.depth === trace._maxDepth - 1);
146158
};
147159

148-
exports.getLabelStr = function(label) {
149-
return labelStr(label).split('<br>').join(' ');
150-
};
151-
152-
exports.getLabelString = function(label) { // used in hover to reference to the "root"
153-
var str = exports.getLabelStr(label);
154-
return str === '' ? '"root"' : str;
155-
};
156-
157160
exports.listPath = function(d, keyStr) {
158161
var parent = d.parent;
159162
if(!parent) return [];

src/traces/sunburst/plot.js

+34-66
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,6 @@ function plotOne(gd, cd, element, transitionOpts) {
8686
var trace = cd0.trace;
8787
var hierarchy = cd0.hierarchy;
8888
var entry = helpers.findEntryWithLevel(hierarchy, trace.level);
89-
var ancestorNodes = helpers.listPath(entry.data);
90-
var numAncestors = ancestorNodes.length;
91-
entry._parent = (numAncestors ?
92-
ancestorNodes[numAncestors - 1] :
93-
hierarchy // parent of root is itself
94-
).data;
95-
9689
var maxDepth = helpers.getMaxDepth(trace);
9790

9891
var gs = fullLayout._size;
@@ -484,54 +477,37 @@ function partition(entry) {
484477
}
485478

486479
exports.formatSliceLabel = function(pt, entry, trace, cd, fullLayout) {
480+
var texttemplate = trace.texttemplate;
481+
var textinfo = trace.textinfo;
482+
483+
if(!texttemplate && (!textinfo || textinfo === 'none')) {
484+
return '';
485+
}
486+
487+
var separators = fullLayout.separators;
487488
var cd0 = cd[0];
488489
var cdi = pt.data.data;
489490
var hierarchy = cd0.hierarchy;
490491
var isRoot = helpers.isHierarchyRoot(pt);
491492
var isEntry = helpers.isEntry(pt);
492-
var parent = isEntry ? pt._parent : pt.parent;
493-
494-
var ref;
495-
var getLabel = function(d) {
496-
var id = d.id;
497-
if(d.data) {
498-
id = d.data.id;
499-
if(d.data.data) {
500-
id = d.data.data.id;
501-
}
502-
}
503-
if(!id) return '';
504-
505-
for(var q = 0; q < cd.length; q++) {
506-
if(cd[q].label === id) {
507-
return id;
508-
}
509-
}
510-
return '';
511-
};
512493

513-
var getVal = function(d) {
514-
if(d.hasOwnProperty('hierarchy')) return d.hierarchy.value;
515-
return d.hasOwnProperty('v') ? d.v : d.value;
516-
};
494+
var rootLabel = hierarchy.data.data.pid;
517495

518-
var calcPercent = function() {
519-
var result = (trace.branchvalues ? cdi.v : cdi.value) / getVal(ref);
520-
return isFinite(result) ? result : 1;
521-
};
522-
523-
if(trace.type === 'treemap' && helpers.isHeader(pt, trace)) {
524-
return helpers.getLabelStr(cdi.label);
496+
var parent;
497+
var parentLabel;
498+
var parentValue;
499+
if(isEntry) {
500+
parent = pt.data.parent;
501+
parentLabel = parent ? parent.data.label : helpers.getPtLabel(hierarchy);
502+
parentValue = parent ? helpers.getVal(parent.data) : helpers.getVal(hierarchy);
503+
} else {
504+
parent = pt.parent;
505+
parentLabel = helpers.getPtLabel(parent);
506+
parentValue = helpers.getVal(parent);
525507
}
526508

527-
var texttemplate = trace.texttemplate;
528-
var textinfo = trace.textinfo;
509+
var val = helpers.getVal(cdi);
529510

530-
if(!texttemplate && (!textinfo || textinfo === 'none')) {
531-
return '';
532-
}
533-
534-
var separators = fullLayout.separators;
535511
if(!texttemplate) {
536512
var parts = textinfo.split('+');
537513
var hasFlag = function(flag) { return parts.indexOf(flag) !== -1; };
@@ -562,26 +538,21 @@ exports.formatSliceLabel = function(pt, entry, trace, cd, fullLayout) {
562538
var addPercent = function(key) {
563539
tx = helpers.formatPercent(percent, separators);
564540

565-
if(hasMultiplePercents) tx += ' of ' + key + ' ';
541+
if(hasMultiplePercents) tx += ' of ' + key;
566542
thisText.push(tx);
567543
};
568544

569-
var makePercent = function(key) {
570-
percent = calcPercent();
571-
addPercent(key);
572-
};
573-
574545
if(hasFlag('percent parent')) {
575-
ref = parent;
576-
makePercent('parent');
546+
percent = val / parentValue;
547+
addPercent('parent');
577548
}
578549
if(hasFlag('percent entry')) {
579-
ref = entry;
580-
makePercent('entry');
550+
percent = val / helpers.getVal(entry);
551+
addPercent('entry');
581552
}
582553
if(hasFlag('percent root')) {
583-
ref = hierarchy;
584-
makePercent('root');
554+
percent = val / helpers.getVal(hierarchy);
555+
addPercent('root');
585556
}
586557
}
587558
}
@@ -605,26 +576,23 @@ exports.formatSliceLabel = function(pt, entry, trace, cd, fullLayout) {
605576

606577
obj.currentPath = helpers.getPath(pt.data);
607578

608-
ref = parent;
609-
obj.percentParent = calcPercent();
579+
obj.percentParent = val / parentValue;
610580
obj.percentParentLabel = helpers.formatPercent(
611581
obj.percentParent, separators
612582
);
613-
obj.parent = helpers.getLabelString(getLabel(ref));
583+
obj.parent = helpers.replaceVoid(parentLabel, rootLabel);
614584

615-
ref = entry;
616-
obj.percentEntry = calcPercent();
585+
obj.percentEntry = val / helpers.getVal(entry);
617586
obj.percentEntryLabel = helpers.formatPercent(
618587
obj.percentEntry, separators
619588
);
620-
obj.entry = helpers.getLabelString(getLabel(ref));
589+
obj.entry = helpers.replaceVoid(helpers.getPtLabel(entry), rootLabel);
621590

622-
ref = hierarchy;
623-
obj.percentRoot = calcPercent();
591+
obj.percentRoot = val / helpers.getVal(hierarchy);
624592
obj.percentRootLabel = helpers.formatPercent(
625593
obj.percentRoot, separators
626594
);
627-
obj.root = helpers.getLabelString(getLabel(ref));
595+
obj.root = helpers.replaceVoid(helpers.getPtLabel(hierarchy), rootLabel);
628596

629597
if(cdi.hasOwnProperty('color')) {
630598
obj.color = cdi.color;

src/traces/treemap/draw_ancestors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ module.exports = function drawAncestors(gd, cd, entry, slices, opts) {
139139
s.attr('data-notex', 1);
140140
});
141141

142-
var tx = helpers.getLabelStr(pt.data.data.label) || ' ';
142+
var tx = (helpers.getPtLabel(pt) || ' ').split('<br>').join(' ');
143143

144144
sliceText.text(tx)
145145
.classed('slicetext', true)

src/traces/treemap/draw_descendants.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,14 @@ module.exports = function drawDescendants(gd, cd, entry, slices, opts) {
164164
s.attr('data-notex', 1);
165165
});
166166

167-
if(isHeader && noRoomForHeader) return;
167+
var tx;
168+
if(isHeader) {
169+
if(noRoomForHeader) return;
168170

169-
var tx = formatSliceLabel(pt, entry, trace, cd, fullLayout) || ' ';
171+
tx = helpers.getPtLabel(pt);
172+
} else {
173+
tx = formatSliceLabel(pt, entry, trace, cd, fullLayout) || ' ';
174+
}
170175

171176
sliceText.text(tx)
172177
.classed('slicetext', true)

src/traces/treemap/plot.js

-8
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,7 @@ function plotOne(gd, cd, element, transitionOpts) {
8383
var trace = cd0.trace;
8484
var hierarchy = cd0.hierarchy;
8585
var hasTransition = helpers.hasTransition(transitionOpts);
86-
8786
var entry = helpers.findEntryWithLevel(hierarchy, trace.level);
88-
var ancestorNodes = helpers.listPath(entry.data);
89-
var numAncestors = ancestorNodes.length;
90-
entry._parent = (numAncestors ?
91-
ancestorNodes[numAncestors - 1] :
92-
hierarchy // parent of root is itself
93-
).data;
94-
9587
var maxDepth = helpers.getMaxDepth(trace);
9688
var hasVisibleDepth = function(pt) {
9789
return pt.data.depth - entry.data.depth < maxDepth;
192 Bytes
Loading
Loading
-7.14 KB
Loading
3.44 KB
Loading
-872 Bytes
Loading

0 commit comments

Comments
 (0)