@@ -22,6 +22,8 @@ var getLegendData = require('./get_legend_data');
22
22
var style = require ( './style' ) ;
23
23
var helpers = require ( './helpers' ) ;
24
24
25
+ var MAIN_TITLE = 1 ;
26
+
25
27
module . exports = function draw ( gd , opts ) {
26
28
var fullLayout = gd . _fullLayout ;
27
29
var clipId = 'legend' + fullLayout . _uid ;
@@ -85,7 +87,7 @@ module.exports = function draw(gd, opts) {
85
87
. call ( Drawing . font , title . font )
86
88
. text ( title . text ) ;
87
89
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
89
91
} else {
90
92
scrollBox . selectAll ( '.legendtitletext' ) . remove ( ) ;
91
93
}
@@ -386,7 +388,6 @@ function drawTexts(g, gd, opts) {
386
388
var legendItem = g . data ( ) [ 0 ] [ 0 ] ;
387
389
var trace = legendItem . trace ;
388
390
var isPieLike = Registry . traceIs ( trace , 'pie-like' ) ;
389
- var traceIndex = trace . index ;
390
391
var isEditable = ! opts . _inHover && gd . _context . edits . legendText && ! isPieLike ;
391
392
var maxNameLength = opts . _maxNameLength ;
392
393
@@ -432,7 +433,7 @@ function drawTexts(g, gd, opts) {
432
433
update . name = newName ;
433
434
}
434
435
435
- return Registry . call ( '_guiRestyle' , gd , update , traceIndex ) ;
436
+ return Registry . call ( '_guiRestyle' , gd , update , trace . index ) ;
436
437
} ) ;
437
438
} else {
438
439
textLayout ( textEl , g , gd , opts ) ;
@@ -491,14 +492,14 @@ function setupTraceToggle(g, gd) {
491
492
} ) ;
492
493
}
493
494
494
- function textLayout ( s , g , gd , opts ) {
495
+ function textLayout ( s , g , gd , opts , aTitle ) {
495
496
if ( opts . _inHover ) s . attr ( 'data-notex' , true ) ; // do not process MathJax for unified hover
496
497
svgTextUtils . convertToTspans ( s , gd , function ( ) {
497
- computeTextDimensions ( g , gd , opts ) ;
498
+ computeTextDimensions ( g , gd , opts , aTitle ) ;
498
499
} ) ;
499
500
}
500
501
501
- function computeTextDimensions ( g , gd , opts ) {
502
+ function computeTextDimensions ( g , gd , opts , aTitle ) {
502
503
var legendItem = g . data ( ) [ 0 ] [ 0 ] ;
503
504
if ( ! opts . _inHover && legendItem && ! legendItem . trace . showlegend ) {
504
505
g . remove ( ) ;
@@ -509,7 +510,7 @@ function computeTextDimensions(g, gd, opts) {
509
510
var mathjaxNode = mathjaxGroup . node ( ) ;
510
511
if ( ! opts ) opts = gd . _fullLayout . legend ;
511
512
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 ;
513
514
var height , width ;
514
515
515
516
if ( mathjaxNode ) {
@@ -518,14 +519,14 @@ function computeTextDimensions(g, gd, opts) {
518
519
height = mathjaxBB . height ;
519
520
width = mathjaxBB . width ;
520
521
521
- if ( legendItem ) {
522
+ if ( aTitle === MAIN_TITLE ) {
523
+ Drawing . setTranslate ( mathjaxGroup , bw , bw + height * 0.75 ) ;
524
+ } else { // legend item
522
525
Drawing . setTranslate ( mathjaxGroup , 0 , height * 0.25 ) ;
523
- } else { // case of title
524
- Drawing . setTranslate ( mathjaxGroup , bw , height * 0.75 + bw ) ;
525
526
}
526
527
} else {
527
- var textEl = g . select ( legendItem ?
528
- '.legendtext ' : '.legendtitletext '
528
+ var textEl = g . select ( aTitle === MAIN_TITLE ?
529
+ '.legendtitletext ' : '.legendtext '
529
530
) ;
530
531
var textLines = svgTextUtils . lineCount ( textEl ) ;
531
532
var textNode = textEl . node ( ) ;
@@ -535,22 +536,26 @@ function computeTextDimensions(g, gd, opts) {
535
536
536
537
// approximation to height offset to center the font
537
538
// 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
+ ) ;
544
549
}
545
550
}
546
551
547
- if ( legendItem ) {
552
+ if ( aTitle === MAIN_TITLE ) {
553
+ opts . _titleWidth = width ;
554
+ opts . _titleHeight = height ;
555
+ } else { // legend item
548
556
legendItem . lineHeight = lineHeight ;
549
557
legendItem . height = Math . max ( height , 16 ) + 3 ;
550
558
legendItem . width = width ;
551
- } else { // case of title
552
- opts . _titleWidth = width ;
553
- opts . _titleHeight = height ;
554
559
}
555
560
}
556
561
@@ -599,6 +604,8 @@ function computeLegendDimensions(gd, groups, traces, opts) {
599
604
var isBelowPlotArea = opts . y < 0 || ( opts . y === 0 && yanchor === 'top' ) ;
600
605
var isAbovePlotArea = opts . y > 1 || ( opts . y === 1 && yanchor === 'bottom' ) ;
601
606
607
+ var traceGroupGap = opts . tracegroupgap ;
608
+
602
609
// - if below/above plot area, give it the maximum potential margin-push value
603
610
// - otherwise, extend the height of the plot area
604
611
opts . _maxHeight = Math . max (
@@ -681,7 +688,7 @@ function computeLegendDimensions(gd, groups, traces, opts) {
681
688
if ( ( next + bw + groupOffsetX ) > opts . _maxWidth ) {
682
689
maxRowWidth = Math . max ( maxRowWidth , groupOffsetX ) ;
683
690
groupOffsetX = 0 ;
684
- groupOffsetY += maxGroupHeightInRow + opts . tracegroupgap ;
691
+ groupOffsetY += maxGroupHeightInRow + traceGroupGap ;
685
692
maxGroupHeightInRow = offsetY ;
686
693
}
687
694
0 commit comments