Skip to content

Commit 3cb8cad

Browse files
committed
Adjust d3 update to enable text mode animation
1 parent fce36fa commit 3cb8cad

File tree

2 files changed

+58
-48
lines changed

2 files changed

+58
-48
lines changed

src/components/drawing/index.js

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -339,51 +339,57 @@ drawing.tryColorscale = function(cont, contIn, prefix) {
339339
// draw text at points
340340
var TEXTOFFSETSIGN = {start: 1, end: -1, middle: 0, bottom: 1, top: -1},
341341
LINEEXPAND = 1.3;
342+
343+
drawing.singleTextPointStyle = function(d, sel, trace) {
344+
var el = sel.node(),
345+
text = d.tx || trace.text;
346+
347+
if(!text || Array.isArray(text)) {
348+
// isArray test handles the case of (intentionally) missing
349+
// or empty text within a text array
350+
sel.remove();
351+
return;
352+
}
353+
354+
var pos = d.tp || trace.textposition,
355+
v = pos.indexOf('top') !== -1 ? 'top' :
356+
pos.indexOf('bottom') !== -1 ? 'bottom' : 'middle',
357+
h = pos.indexOf('left') !== -1 ? 'end' :
358+
pos.indexOf('right') !== -1 ? 'start' : 'middle',
359+
fontSize = d.ts || trace.textfont.size,
360+
// if markers are shown, offset a little more than
361+
// the nominal marker size
362+
// ie 2/1.6 * nominal, bcs some markers are a bit bigger
363+
r = d.mrc ? (d.mrc / 0.8 + 1) : 0;
364+
365+
fontSize = (isNumeric(fontSize) && fontSize > 0) ? fontSize : 0;
366+
367+
sel.call(drawing.font,
368+
d.tf || trace.textfont.family,
369+
fontSize,
370+
d.tc || trace.textfont.color)
371+
.attr('text-anchor', h)
372+
.text(text)
373+
.call(svgTextUtils.convertToTspans);
374+
var pgroup = d3.select(el.parentNode),
375+
tspans = sel.selectAll('tspan.line'),
376+
numLines = ((tspans[0].length || 1) - 1) * LINEEXPAND + 1,
377+
dx = TEXTOFFSETSIGN[h] * r,
378+
dy = fontSize * 0.75 + TEXTOFFSETSIGN[v] * r +
379+
(TEXTOFFSETSIGN[v] - 1) * numLines * fontSize / 2;
380+
381+
// fix the overall text group position
382+
pgroup.attr('transform', 'translate(' + dx + ',' + dy + ')');
383+
384+
// then fix multiline text
385+
if(numLines > 1) {
386+
tspans.attr({ x: sel.attr('x'), y: sel.attr('y') });
387+
}
388+
};
389+
342390
drawing.textPointStyle = function(s, trace) {
343391
s.each(function(d) {
344-
var p = d3.select(this),
345-
text = d.tx || trace.text;
346-
if(!text || Array.isArray(text)) {
347-
// isArray test handles the case of (intentionally) missing
348-
// or empty text within a text array
349-
p.remove();
350-
return;
351-
}
352-
353-
var pos = d.tp || trace.textposition,
354-
v = pos.indexOf('top') !== -1 ? 'top' :
355-
pos.indexOf('bottom') !== -1 ? 'bottom' : 'middle',
356-
h = pos.indexOf('left') !== -1 ? 'end' :
357-
pos.indexOf('right') !== -1 ? 'start' : 'middle',
358-
fontSize = d.ts || trace.textfont.size,
359-
// if markers are shown, offset a little more than
360-
// the nominal marker size
361-
// ie 2/1.6 * nominal, bcs some markers are a bit bigger
362-
r = d.mrc ? (d.mrc / 0.8 + 1) : 0;
363-
364-
fontSize = (isNumeric(fontSize) && fontSize > 0) ? fontSize : 0;
365-
366-
p.call(drawing.font,
367-
d.tf || trace.textfont.family,
368-
fontSize,
369-
d.tc || trace.textfont.color)
370-
.attr('text-anchor', h)
371-
.text(text)
372-
.call(svgTextUtils.convertToTspans);
373-
var pgroup = d3.select(this.parentNode),
374-
tspans = p.selectAll('tspan.line'),
375-
numLines = ((tspans[0].length || 1) - 1) * LINEEXPAND + 1,
376-
dx = TEXTOFFSETSIGN[h] * r,
377-
dy = fontSize * 0.75 + TEXTOFFSETSIGN[v] * r +
378-
(TEXTOFFSETSIGN[v] - 1) * numLines * fontSize / 2;
379-
380-
// fix the overall text group position
381-
pgroup.attr('transform', 'translate(' + dx + ',' + dy + ')');
382-
383-
// then fix multiline text
384-
if(numLines > 1) {
385-
tspans.attr({ x: p.attr('x'), y: p.attr('y') });
386-
}
392+
drawing.singleTextPointStyle(d, d3.select(this), trace);
387393
});
388394
};
389395

src/traces/scatter/plot.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,18 +443,22 @@ function plotOne(gd, idx, plotinfo, cdscatter, cdscatterAll, element, transition
443443
}
444444

445445
// text points
446-
447446
selection = s.selectAll('g');
448-
449447
join = selection.data(textFilter, keyFunc);
450448

451449
// each text needs to go in its own 'g' in case
452450
// it gets converted to mathjax
453-
join.enter().append('g')
454-
.append('text');
451+
enter = join.enter().append('g')
452+
.append('text')
453+
.call(Drawing.textPointStyle, trace);
455454

456455
join.each(function(d) {
457-
var sel = d3.select(this).select('text');
456+
var node = d3.select(this).select('text');
457+
// We can't transition the text itself, so call this on the node:
458+
Drawing.singleTextPointStyle(d, node, trace);
459+
460+
// This duck-types the node to be set either as a transition or as a node:
461+
var sel = transition(node);
458462
Drawing.translatePoint(d, sel, xa, ya);
459463
});
460464

0 commit comments

Comments
 (0)