@@ -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
@@ -196,7 +220,7 @@ function drawOne(gd, opts) {
196
220
197
221
// re-calculate title position after legend width is derived. To allow for horizontal alignment
198
222
if ( title . text ) {
199
- textLayout ( titleEl , scrollBox , gd , legendObj , MAIN_TITLE ) ;
223
+ horizontalAlignTitle ( titleEl , legendObj , bw ) ;
200
224
}
201
225
202
226
if ( ! inHover ) {
@@ -667,18 +691,13 @@ function computeTextDimensions(g, gd, legendObj, aTitle) {
667
691
// approximation to height offset to center the font
668
692
// to avoid getBoundingClientRect
669
693
if ( aTitle === MAIN_TITLE ) {
670
- var titleOffset = 0 ;
671
694
if ( legendObj . title . side === 'left' ) {
672
695
// add extra space between legend title and itmes
673
696
width += constants . itemGap * 2 ;
674
- } else if ( legendObj . title . side === 'top center' ) {
675
- if ( legendObj . _width ) titleOffset = 0.5 * ( legendObj . _width - 2 * bw - 2 * constants . titlePad - width ) ;
676
- } else if ( legendObj . title . side === 'top right' ) {
677
- if ( legendObj . _width ) titleOffset = legendObj . _width - 2 * bw - 2 * constants . titlePad - width ;
678
697
}
679
698
680
699
svgTextUtils . positionText ( textEl ,
681
- bw + constants . titlePad + titleOffset ,
700
+ bw + constants . titlePad ,
682
701
bw + lineHeight
683
702
) ;
684
703
} else { // legend item
0 commit comments