@@ -54,6 +54,30 @@ module.exports = function draw(gd, opts) {
54
54
}
55
55
} ;
56
56
57
+ // After legend dimensions are calculated the title can be aligned horizontally left, center, right
58
+ function horizontalAlignTitle ( titleEl , legendObj , bw ) {
59
+ if ( ( legendObj . title . side !== 'top center' ) && ( legendObj . title . side !== 'top right' ) ) return ;
60
+
61
+ var font = legendObj . title . font ;
62
+ var lineHeight = font . size * LINE_SPACING ;
63
+ var titleOffset = 0 ;
64
+ var textNode = titleEl . node ( ) ;
65
+
66
+ var width = Drawing . bBox ( textNode ) . width ; // width of the title text
67
+
68
+ if ( legendObj . title . side === 'top center' ) {
69
+ titleOffset = 0.5 * ( legendObj . _width - 2 * bw - 2 * constants . titlePad - width ) ;
70
+ } else if ( legendObj . title . side === 'top right' ) {
71
+ titleOffset = legendObj . _width - 2 * bw - 2 * constants . titlePad - width ;
72
+ }
73
+
74
+ svgTextUtils . positionText ( titleEl ,
75
+ bw + constants . titlePad + titleOffset ,
76
+ bw + lineHeight
77
+ ) ;
78
+ }
79
+
80
+
57
81
function drawOne ( gd , opts ) {
58
82
var legendObj = opts || { } ;
59
83
@@ -148,8 +172,9 @@ function drawOne(gd, opts) {
148
172
var title = legendObj . title ;
149
173
legendObj . _titleWidth = 0 ;
150
174
legendObj . _titleHeight = 0 ;
175
+ var titleEl ;
151
176
if ( title . text ) {
152
- var titleEl = Lib . ensureSingle ( scrollBox , 'text' , legendId + 'titletext' ) ;
177
+ titleEl = Lib . ensureSingle ( scrollBox , 'text' , legendId + 'titletext' ) ;
153
178
titleEl . attr ( 'text-anchor' , 'start' )
154
179
. call ( Drawing . font , title . font )
155
180
. text ( title . text ) ;
@@ -193,6 +218,11 @@ function drawOne(gd, opts) {
193
218
var isPaperX = legendObj . xref === 'paper' ;
194
219
var isPaperY = legendObj . yref === 'paper' ;
195
220
221
+ // re-calculate title position after legend width is derived. To allow for horizontal alignment
222
+ if ( title . text ) {
223
+ horizontalAlignTitle ( titleEl , legendObj , bw ) ;
224
+ }
225
+
196
226
if ( ! inHover ) {
197
227
var lx , ly ;
198
228
@@ -661,18 +691,13 @@ function computeTextDimensions(g, gd, legendObj, aTitle) {
661
691
// approximation to height offset to center the font
662
692
// to avoid getBoundingClientRect
663
693
if ( aTitle === MAIN_TITLE ) {
664
- var titleOffset = 0 ;
665
694
if ( legendObj . title . side === 'left' ) {
666
695
// add extra space between legend title and itmes
667
696
width += constants . itemGap * 2 ;
668
- } else if ( legendObj . title . side === 'top center' ) {
669
- if ( legendObj . _width ) titleOffset = 0.5 * ( legendObj . _width - 2 * bw - 2 * constants . titlePad - width ) ;
670
- } else if ( legendObj . title . side === 'top right' ) {
671
- if ( legendObj . _width ) titleOffset = legendObj . _width - 2 * bw - 2 * constants . titlePad - width ;
672
697
}
673
698
674
699
svgTextUtils . positionText ( textEl ,
675
- bw + constants . titlePad + titleOffset ,
700
+ bw + constants . titlePad ,
676
701
bw + lineHeight
677
702
) ;
678
703
} else { // legend item
0 commit comments