Skip to content

Commit 4420593

Browse files
authored
Merge pull request #4227 from plotly/treemap-text-transitions-maxdepth
Treemap text positions for out of range cases when transitioning with maxdepth
2 parents dfb3987 + 940c83a commit 4420593

File tree

4 files changed

+55
-47
lines changed

4 files changed

+55
-47
lines changed

src/traces/treemap/draw_ancestors.js

+6-13
Original file line numberDiff line numberDiff line change
@@ -132,32 +132,25 @@ module.exports = function drawAncestors(gd, cd, entry, slices, opts) {
132132
hovered: false
133133
});
134134

135+
pt._text = (helpers.getPtLabel(pt) || '').split('<br>').join(' ') || '';
136+
135137
var sliceTextGroup = Lib.ensureSingle(sliceTop, 'g', 'slicetext');
136138
var sliceText = Lib.ensureSingle(sliceTextGroup, 'text', '', function(s) {
137139
// prohibit tex interpretation until we can handle
138140
// tex and regular text together
139141
s.attr('data-notex', 1);
140142
});
141143

142-
var tx = (helpers.getPtLabel(pt) || ' ').split('<br>').join(' ');
143-
144-
sliceText.text(tx)
144+
sliceText.text(pt._text || ' ') // use one space character instead of a blank string to avoid jumps during transition
145145
.classed('slicetext', true)
146146
.attr('text-anchor', 'start')
147147
.call(Drawing.font, helpers.determineTextFont(trace, pt, fullLayout.font, trace.pathdir))
148148
.call(svgTextUtils.convertToTspans, gd);
149149

150150
pt.textBB = Drawing.bBox(sliceText.node());
151-
pt.transform = toMoveInsideSlice(
152-
pt.x0,
153-
pt.x1,
154-
pt.y0,
155-
pt.y1,
156-
pt.textBB,
157-
{
158-
onPathbar: true
159-
}
160-
);
151+
pt.transform = toMoveInsideSlice(pt, {
152+
onPathbar: true
153+
});
161154

162155
if(helpers.isOutsideText(trace, pt)) {
163156
// consider in/out diff font sizes

src/traces/treemap/draw_descendants.js

+14-20
Original file line numberDiff line numberDiff line change
@@ -157,39 +157,33 @@ module.exports = function drawDescendants(gd, cd, entry, slices, opts) {
157157
hovered: false
158158
});
159159

160+
if(pt.x0 === pt.x1 || pt.y0 === pt.y1) {
161+
pt._text = '';
162+
} else {
163+
if(isHeader) {
164+
pt._text = noRoomForHeader ? '' : helpers.getPtLabel(pt) || '';
165+
} else {
166+
pt._text = formatSliceLabel(pt, entry, trace, cd, fullLayout) || '';
167+
}
168+
}
169+
160170
var sliceTextGroup = Lib.ensureSingle(sliceTop, 'g', 'slicetext');
161171
var sliceText = Lib.ensureSingle(sliceTextGroup, 'text', '', function(s) {
162172
// prohibit tex interpretation until we can handle
163173
// tex and regular text together
164174
s.attr('data-notex', 1);
165175
});
166176

167-
var tx;
168-
if(isHeader) {
169-
if(noRoomForHeader) return;
170-
171-
tx = helpers.getPtLabel(pt);
172-
} else {
173-
tx = formatSliceLabel(pt, entry, trace, cd, fullLayout) || ' ';
174-
}
175-
176-
sliceText.text(tx)
177+
sliceText.text(pt._text || ' ') // use one space character instead of a blank string to avoid jumps during transition
177178
.classed('slicetext', true)
178179
.attr('text-anchor', hasRight ? 'end' : (hasLeft || isHeader) ? 'start' : 'middle')
179180
.call(Drawing.font, helpers.determineTextFont(trace, pt, fullLayout.font))
180181
.call(svgTextUtils.convertToTspans, gd);
181182

182183
pt.textBB = Drawing.bBox(sliceText.node());
183-
pt.transform = toMoveInsideSlice(
184-
pt.x0,
185-
pt.x1,
186-
pt.y0,
187-
pt.y1,
188-
pt.textBB,
189-
{
190-
isHeader: isHeader
191-
}
192-
);
184+
pt.transform = toMoveInsideSlice(pt, {
185+
isHeader: isHeader
186+
});
193187

194188
if(helpers.isOutsideText(trace, pt)) {
195189
// consider in/out diff font sizes

src/traces/treemap/plot.js

+32-11
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,23 @@ function plotOne(gd, cd, element, transitionOpts) {
273273
);
274274
};
275275

276-
var toMoveInsideSlice = function(x0, x1, y0, y1, textBB, opts) {
276+
var toMoveInsideSlice = function(pt, opts) {
277+
var x0 = pt.x0;
278+
var x1 = pt.x1;
279+
var y0 = pt.y0;
280+
var y1 = pt.y1;
281+
var textBB = pt.textBB;
282+
283+
if(x0 === x1) {
284+
x0 -= TEXTPAD;
285+
x1 += TEXTPAD;
286+
}
287+
288+
if(y0 === y1) {
289+
y0 -= TEXTPAD;
290+
y1 += TEXTPAD;
291+
}
292+
277293
var hasFlag = function(f) { return trace.textposition.indexOf(f) !== -1; };
278294

279295
var hasBottom = hasFlag('bottom');
@@ -299,6 +315,11 @@ function plotOne(gd, cd, element, transitionOpts) {
299315
if(opts.isHeader) {
300316
x0 += pad.l - TEXTPAD;
301317
x1 -= pad.r - TEXTPAD;
318+
if(x0 >= x1) {
319+
var mid = (x0 + x1) / 2;
320+
x0 = mid - TEXTPAD;
321+
x1 = mid + TEXTPAD;
322+
}
302323

303324
// limit the drawing area for headers
304325
var limY;
@@ -428,16 +449,16 @@ function plotOne(gd, cd, element, transitionOpts) {
428449
var origin = getOrigin(pt, onPathbar, refRect, size);
429450

430451
Lib.extendFlat(prev, {
431-
transform: toMoveInsideSlice(
432-
origin.x0,
433-
origin.x1,
434-
origin.y0,
435-
origin.y1,
436-
pt.textBB,
437-
{
438-
isHeader: helpers.isHeader(pt, trace)
439-
}
440-
)
452+
transform: toMoveInsideSlice({
453+
x0: origin.x0,
454+
x1: origin.x1,
455+
y0: origin.y0,
456+
y1: origin.y1,
457+
textBB: pt.textBB,
458+
_text: pt._text
459+
}, {
460+
isHeader: helpers.isHeader(pt, trace)
461+
})
441462
});
442463

443464
if(prev0) {

test/jasmine/tests/treemap_test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ describe('Test treemap restyle:', function() {
12081208
.then(_restyle({textinfo: 'value'}))
12091209
.then(_assert('show input values', ['Root', 'B', '1', '3']))
12101210
.then(_restyle({textinfo: 'none'}))
1211-
.then(_assert('no textinfo', ['Root', 'B', ' ', ' '])) // N.B. replaced empty string with space character for better transitions
1211+
.then(_assert('no textinfo', ['Root', 'B', ' ', ' '])) // use one space character instead of a blank string to avoid jumps during transition
12121212
.then(_restyle({textinfo: 'label+text+value'}))
12131213
.then(_assert('show everything', ['Root', 'B', 'A\n1\nnode1', 'b\n3\nnode3']))
12141214
.then(_restyle({textinfo: null}))
@@ -1269,7 +1269,7 @@ describe('Test treemap tweening:', function() {
12691269
if(attrName === 'transform') {
12701270
var fake = {attr: function() { return actual; }};
12711271
var xy = Drawing.getTranslate(fake);
1272-
expect([xy.x, xy.y]).toBeWithinArray(exp, 1, msg2);
1272+
expect([xy.x, xy.y]).toBeWithinArray(exp, 2, msg2);
12731273
} else {
12741274
// we could maybe to bring in:
12751275
// https://github.com/hughsk/svg-path-parser
@@ -1367,7 +1367,7 @@ describe('Test treemap tweening:', function() {
13671367
'M284.375,188.5L548.375,188.5L548.375,308.5L284.375,308.5Z'
13681368
);
13691369
_assert('move B text to new position', 'transform', 'B', [220.25126, 0]);
1370-
_assert('enter b text to new position', 'transform', 'b', [287.375195, 5]);
1370+
_assert('enter b text to new position', 'transform', 'b', [286.16071428571433, 35714285714286]);
13711371
})
13721372
.catch(failTest)
13731373
.then(done);

0 commit comments

Comments
 (0)