Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 96eb397

Browse files
committed
handle maxdepth cases where leaves can be removed and added
1 parent 656fadc commit 96eb397

File tree

1 file changed

+63
-11
lines changed

1 file changed

+63
-11
lines changed

src/traces/sunburst/plot.js

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,40 @@ module.exports = function plot(gd, cdmodule) {
113113
var slicePath = sliceTop.select('path.surface');
114114

115115
slicePath.attrTween('d', function(pt2) {
116+
var next;
116117
var rootPtPrev = rootPt.data.data.prev;
117-
var a = pt2.x1 > rootPtPrev.x1 ? 2 * Math.PI : 0;
118-
// if pt to remove:
119-
// - is 'below' where the root-node used to be: shrink it radially
120-
// - otherwise, collapse it clockwise or counterclockwise which ever is shortest to theta=0
121-
var next = pt2.rpx1 < rootPtPrev.rpx1 ? {rpx0: 0, rpx1: 0} : {x0: a, x1: a};
118+
119+
if(rootPtPrev) {
120+
var a = pt2.x1 > rootPtPrev.x1 ? 2 * Math.PI : 0;
121+
// if pt to remove:
122+
// - if 'below' where the root-node used to be: shrink it radially inward
123+
// - otherwise, collapse it clockwise or counterclockwise which ever is shortest to theta=0
124+
next = pt2.rpx1 < rootPtPrev.rpx1 ? {rpx0: 0, rpx1: 0} : {x0: a, x1: a};
125+
} else {
126+
// this happens when maxdepth is set, when leaves must
127+
// be removed and the rootPt is new (i.e. does not have a 'prev' object)
128+
var parent;
129+
var parentId = pt2.parent.data.data.id;
130+
slices.each(function(pt3) {
131+
if(!parent && pt3.data.data.id === parentId) {
132+
parent = pt3;
133+
}
134+
});
135+
var parentChildren = parent.children;
136+
var ci;
137+
parentChildren.forEach(function(pt3, i) {
138+
if(pt3.data.data.id === pt2.data.data.id) {
139+
return ci = i;
140+
}
141+
});
142+
var n = parentChildren.length;
143+
var interp = d3.interpolate(parent.x0, parent.x1);
144+
next = {
145+
rpx0: rMax, rpx1: rMax,
146+
x0: interp(ci / n), x1: interp((ci + 1) / n)
147+
};
148+
}
149+
122150
return makeTweenFn(pt.data.data.prev, next);
123151
});
124152

@@ -136,8 +164,9 @@ module.exports = function plot(gd, cdmodule) {
136164

137165
var nextX1ofPrevRootPt = null;
138166
if(prevRootPt) {
167+
var prevRootPtId = prevRootPt.data.data.id;
139168
slices.each(function(pt) {
140-
if(nextX1ofPrevRootPt === null && (pt.data.data.id === prevRootPt.data.data.id)) {
169+
if(nextX1ofPrevRootPt === null && (pt.data.data.id === prevRootPtId)) {
141170
nextX1ofPrevRootPt = pt.x1;
142171
}
143172
});
@@ -178,11 +207,34 @@ module.exports = function plot(gd, cdmodule) {
178207
if(prevRootPt) {
179208
// if trace was visible before
180209
if(pt2.parent) {
181-
// if new branch, twist it in clockwise or
182-
// counterclockwise which ever is shorter to
183-
// its final angle
184-
var a = pt2.x1 > nextX1ofPrevRootPt ? 2 * Math.PI : 0;
185-
prev = {x0: a, x1: a};
210+
if(nextX1ofPrevRootPt) {
211+
// if new branch, twist it in clockwise or
212+
// counterclockwise which ever is shorter to
213+
// its final angle
214+
var a = pt2.x1 > nextX1ofPrevRootPt ? 2 * Math.PI : 0;
215+
prev = {x0: a, x1: a};
216+
} else {
217+
var parent = pt2.parent;
218+
var parentPrev = parent.data.data.prev;
219+
220+
// if new leaf (when maxdepth is set),
221+
// grow it radially and angularly from
222+
// its parent node
223+
prev = {rpx0: rMax, rpx1: rMax};
224+
225+
if(parentPrev) {
226+
var parentChildren = parent.children;
227+
var ci = parentChildren.indexOf(pt2);
228+
var n = parentChildren.length;
229+
var interp = d3.interpolate(parentPrev.x0, parentPrev.x1);
230+
prev.x0 = interp(ci / n);
231+
prev.x1 = interp(ci / n);
232+
} else {
233+
// TODO !!!
234+
// HOW ???
235+
prev.x0 = prev.x1 = 0;
236+
}
237+
}
186238
} else {
187239
// if new root-node, grow it radially
188240
prev = {rpx0: 0, rpx1: 0};

0 commit comments

Comments
 (0)