@@ -54,6 +54,30 @@ module.exports = function draw(gd, opts) {
5454 }
5555} ;
5656
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+
5781function drawOne ( gd , opts ) {
5882 var legendObj = opts || { } ;
5983
@@ -148,8 +172,9 @@ function drawOne(gd, opts) {
148172 var title = legendObj . title ;
149173 legendObj . _titleWidth = 0 ;
150174 legendObj . _titleHeight = 0 ;
175+ var titleEl ;
151176 if ( title . text ) {
152- var titleEl = Lib . ensureSingle ( scrollBox , 'text' , legendId + 'titletext' ) ;
177+ titleEl = Lib . ensureSingle ( scrollBox , 'text' , legendId + 'titletext' ) ;
153178 titleEl . attr ( 'text-anchor' , 'start' )
154179 . call ( Drawing . font , title . font )
155180 . text ( title . text ) ;
@@ -193,6 +218,11 @@ function drawOne(gd, opts) {
193218 var isPaperX = legendObj . xref === 'paper' ;
194219 var isPaperY = legendObj . yref === 'paper' ;
195220
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+
196226 if ( ! inHover ) {
197227 var lx , ly ;
198228
@@ -661,18 +691,13 @@ function computeTextDimensions(g, gd, legendObj, aTitle) {
661691 // approximation to height offset to center the font
662692 // to avoid getBoundingClientRect
663693 if ( aTitle === MAIN_TITLE ) {
664- var titleOffset = 0 ;
665694 if ( legendObj . title . side === 'left' ) {
666695 // add extra space between legend title and itmes
667696 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 ;
672697 }
673698
674699 svgTextUtils . positionText ( textEl ,
675- bw + constants . titlePad + titleOffset ,
700+ bw + constants . titlePad ,
676701 bw + lineHeight
677702 ) ;
678703 } else { // legend item
0 commit comments