Skip to content

Commit 869538c

Browse files
committed
Legend: refactor computation of text dimensions
* Checked that all jasmine and image tests still pass.
1 parent 92ff540 commit 869538c

File tree

1 file changed

+53
-35
lines changed

1 file changed

+53
-35
lines changed

src/components/legend/draw.js

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,51 @@ function setupTraceToggle(gd, container, legendItem) {
406406
});
407407
}
408408

409+
function computeTextDimensions(gd, container, legendItem) {
410+
var opts = gd._fullLayout.legend,
411+
g = d3.select(container),
412+
bg = g.selectAll('.legendtoggle'),
413+
mathjaxGroup = g.select('g[class*=math-group]'),
414+
lineHeight = opts.font.size * 1.3,
415+
height,
416+
width;
417+
418+
if(!legendItem.trace.showlegend) {
419+
g.remove();
420+
return;
421+
}
422+
423+
if(mathjaxGroup.node()) {
424+
var mathjaxBB = Drawing.bBox(mathjaxGroup.node());
425+
426+
height = mathjaxBB.height;
427+
width = mathjaxBB.width;
428+
429+
mathjaxGroup.attr('transform','translate(0,' + (height / 4) + ')');
430+
}
431+
else {
432+
var text = g.selectAll('.legendtext'),
433+
textSpans = g.selectAll('.legendtext>tspan'),
434+
textLines = textSpans[0].length || 1;
435+
436+
height = lineHeight * textLines;
437+
width = text.node() && Drawing.bBox(text.node()).width;
438+
439+
// approximation to height offset to center the font
440+
// to avoid getBoundingClientRect
441+
var textY = lineHeight * (0.3 + (1 - textLines) / 2);
442+
text.attr('y', textY);
443+
textSpans.attr('y', textY);
444+
}
445+
446+
height = Math.max(height, 16) + 3;
447+
448+
bg.attr({x: 0, y: -height / 2, height: height});
449+
450+
legendItem.height = height;
451+
legendItem.width = width;
452+
}
453+
409454
function computeLegendDimensions(gd, traces) {
410455
var fullLayout = gd._fullLayout,
411456
opts = fullLayout.legend,
@@ -415,48 +460,21 @@ function computeLegendDimensions(gd, traces) {
415460
opts.height = 0;
416461

417462
traces.each(function(d) {
418-
var trace = d[0].trace,
419-
g = d3.select(this),
420-
bg = g.selectAll('.legendtoggle'),
421-
text = g.selectAll('.legendtext'),
422-
tspans = g.selectAll('.legendtext>tspan'),
423-
tHeight = opts.font.size * 1.3,
424-
tLines = tspans[0].length || 1,
425-
tWidth = text.node() && Drawing.bBox(text.node()).width,
426-
mathjaxGroup = g.select('g[class*=math-group]'),
427-
textY,
428-
tHeightFull;
429-
430-
if(!trace.showlegend) {
431-
g.remove();
432-
return;
433-
}
463+
var legendItem = d[0];
434464

435-
if(mathjaxGroup.node()) {
436-
var mathjaxBB = Drawing.bBox(mathjaxGroup.node());
437-
tHeight = mathjaxBB.height;
438-
tWidth = mathjaxBB.width;
439-
mathjaxGroup.attr('transform','translate(0,' + (tHeight / 4) + ')');
440-
}
441-
else {
442-
// approximation to height offset to center the font
443-
// to avoid getBoundingClientRect
444-
textY = tHeight * (0.3 + (1 - tLines) / 2);
445-
text.attr('y', textY);
446-
tspans.attr('y', textY);
447-
}
465+
computeTextDimensions(gd, this, legendItem);
448466

449-
tHeightFull = Math.max(tHeight * tLines, 16) + 3;
467+
var textHeight = legendItem.height,
468+
textWidth = legendItem.width;
450469

451-
g.attr('transform',
470+
d3.select(this).attr('transform',
452471
'translate(' + borderwidth + ',' +
453-
(5 + borderwidth + opts.height + tHeightFull / 2) +
472+
(5 + borderwidth + opts.height + textHeight / 2) +
454473
')'
455474
);
456-
bg.attr({x: 0, y: -tHeightFull / 2, height: tHeightFull});
457475

458-
opts.height += tHeightFull;
459-
opts.width = Math.max(opts.width, tWidth || 0);
476+
opts.height += textHeight;
477+
opts.width = Math.max(opts.width, textWidth);
460478
});
461479

462480
opts.width += 45 + borderwidth * 2;

0 commit comments

Comments
 (0)