Skip to content

Commit ee75947

Browse files
committed
better handle of main title
1 parent 6abbfb2 commit ee75947

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

src/components/legend/draw.js

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var getLegendData = require('./get_legend_data');
2222
var style = require('./style');
2323
var helpers = require('./helpers');
2424

25+
var MAIN_TITLE = 1;
26+
2527
module.exports = function draw(gd, opts) {
2628
var fullLayout = gd._fullLayout;
2729
var clipId = 'legend' + fullLayout._uid;
@@ -85,7 +87,7 @@ module.exports = function draw(gd, opts) {
8587
.call(Drawing.font, title.font)
8688
.text(title.text);
8789

88-
textLayout(titleEl, scrollBox, gd, opts); // handle mathjax or multi-line text and compute title height
90+
textLayout(titleEl, scrollBox, gd, opts, MAIN_TITLE); // handle mathjax or multi-line text and compute title height
8991
} else {
9092
scrollBox.selectAll('.legendtitletext').remove();
9193
}
@@ -386,7 +388,6 @@ function drawTexts(g, gd, opts) {
386388
var legendItem = g.data()[0][0];
387389
var trace = legendItem.trace;
388390
var isPieLike = Registry.traceIs(trace, 'pie-like');
389-
var traceIndex = trace.index;
390391
var isEditable = !opts._inHover && gd._context.edits.legendText && !isPieLike;
391392
var maxNameLength = opts._maxNameLength;
392393

@@ -432,7 +433,7 @@ function drawTexts(g, gd, opts) {
432433
update.name = newName;
433434
}
434435

435-
return Registry.call('_guiRestyle', gd, update, traceIndex);
436+
return Registry.call('_guiRestyle', gd, update, trace.index);
436437
});
437438
} else {
438439
textLayout(textEl, g, gd, opts);
@@ -491,14 +492,14 @@ function setupTraceToggle(g, gd) {
491492
});
492493
}
493494

494-
function textLayout(s, g, gd, opts) {
495+
function textLayout(s, g, gd, opts, aTitle) {
495496
if(opts._inHover) s.attr('data-notex', true); // do not process MathJax for unified hover
496497
svgTextUtils.convertToTspans(s, gd, function() {
497-
computeTextDimensions(g, gd, opts);
498+
computeTextDimensions(g, gd, opts, aTitle);
498499
});
499500
}
500501

501-
function computeTextDimensions(g, gd, opts) {
502+
function computeTextDimensions(g, gd, opts, aTitle) {
502503
var legendItem = g.data()[0][0];
503504
if(!opts._inHover && legendItem && !legendItem.trace.showlegend) {
504505
g.remove();
@@ -509,7 +510,7 @@ function computeTextDimensions(g, gd, opts) {
509510
var mathjaxNode = mathjaxGroup.node();
510511
if(!opts) opts = gd._fullLayout.legend;
511512
var bw = opts.borderwidth;
512-
var lineHeight = (legendItem ? opts : opts.title).font.size * LINE_SPACING;
513+
var lineHeight = (aTitle === MAIN_TITLE ? opts.title : opts).font.size * LINE_SPACING;
513514
var height, width;
514515

515516
if(mathjaxNode) {
@@ -518,14 +519,14 @@ function computeTextDimensions(g, gd, opts) {
518519
height = mathjaxBB.height;
519520
width = mathjaxBB.width;
520521

521-
if(legendItem) {
522+
if(aTitle === MAIN_TITLE) {
523+
Drawing.setTranslate(mathjaxGroup, bw, bw + height * 0.75);
524+
} else { // legend item
522525
Drawing.setTranslate(mathjaxGroup, 0, height * 0.25);
523-
} else { // case of title
524-
Drawing.setTranslate(mathjaxGroup, bw, height * 0.75 + bw);
525526
}
526527
} else {
527-
var textEl = g.select(legendItem ?
528-
'.legendtext' : '.legendtitletext'
528+
var textEl = g.select(aTitle === MAIN_TITLE ?
529+
'.legendtitletext' : '.legendtext'
529530
);
530531
var textLines = svgTextUtils.lineCount(textEl);
531532
var textNode = textEl.node();
@@ -535,22 +536,26 @@ function computeTextDimensions(g, gd, opts) {
535536

536537
// approximation to height offset to center the font
537538
// to avoid getBoundingClientRect
538-
var textY = lineHeight * ((textLines - 1) / 2 - 0.3);
539-
if(legendItem) {
540-
var textGap = opts.itemwidth + constants.itemGap * 2;
541-
svgTextUtils.positionText(textEl, textGap, -textY);
542-
} else { // case of title
543-
svgTextUtils.positionText(textEl, constants.titlePad + bw, lineHeight + bw);
539+
if(aTitle === MAIN_TITLE) {
540+
svgTextUtils.positionText(textEl,
541+
bw + constants.titlePad,
542+
bw + lineHeight
543+
);
544+
} else { // legend item
545+
svgTextUtils.positionText(textEl,
546+
opts.itemwidth + constants.itemGap * 2,
547+
-lineHeight * ((textLines - 1) / 2 - 0.3)
548+
);
544549
}
545550
}
546551

547-
if(legendItem) {
552+
if(aTitle === MAIN_TITLE) {
553+
opts._titleWidth = width;
554+
opts._titleHeight = height;
555+
} else { // legend item
548556
legendItem.lineHeight = lineHeight;
549557
legendItem.height = Math.max(height, 16) + 3;
550558
legendItem.width = width;
551-
} else { // case of title
552-
opts._titleWidth = width;
553-
opts._titleHeight = height;
554559
}
555560
}
556561

@@ -599,6 +604,8 @@ function computeLegendDimensions(gd, groups, traces, opts) {
599604
var isBelowPlotArea = opts.y < 0 || (opts.y === 0 && yanchor === 'top');
600605
var isAbovePlotArea = opts.y > 1 || (opts.y === 1 && yanchor === 'bottom');
601606

607+
var traceGroupGap = opts.tracegroupgap;
608+
602609
// - if below/above plot area, give it the maximum potential margin-push value
603610
// - otherwise, extend the height of the plot area
604611
opts._maxHeight = Math.max(
@@ -681,7 +688,7 @@ function computeLegendDimensions(gd, groups, traces, opts) {
681688
if((next + bw + groupOffsetX) > opts._maxWidth) {
682689
maxRowWidth = Math.max(maxRowWidth, groupOffsetX);
683690
groupOffsetX = 0;
684-
groupOffsetY += maxGroupHeightInRow + opts.tracegroupgap;
691+
groupOffsetY += maxGroupHeightInRow + traceGroupGap;
685692
maxGroupHeightInRow = offsetY;
686693
}
687694

0 commit comments

Comments
 (0)