@@ -134,7 +134,19 @@ module.exports = function draw(gd, opts) {
134
134
var gs = fullLayout . _size ;
135
135
var bw = opts . borderwidth ;
136
136
137
- var lx = gs . l + gs . w * opts . x - FROM_TL [ getXanchor ( opts ) ] * opts . _width ;
137
+ var xanchor = getXanchor ( opts ) ;
138
+ var yanchor = getYanchor ( opts ) ;
139
+ // Adjust centering of the legend box
140
+ var lx ;
141
+ if ( xanchor === 'center' && yanchor === 'top' ) {
142
+ lx = ( gs . w * opts . x - 0.5 * opts . _width ) * 1.8 ;
143
+ } else if ( xanchor === 'center' && yanchor === 'bottom' ) {
144
+ lx = ( gs . l + gs . w * opts . x - FROM_TL [ getXanchor ( opts ) ] * opts . _width ) - 30 ;
145
+ } else if ( xanchor === 'center' && yanchor === 'middle' ) {
146
+ lx = ( gs . l + gs . w * opts . x - FROM_TL [ getXanchor ( opts ) ] * opts . _width ) - 15 ;
147
+ } else {
148
+ lx = gs . l + gs . w * opts . x - FROM_TL [ getXanchor ( opts ) ] * opts . _width ;
149
+ }
138
150
var ly = gs . t + gs . h * ( 1 - opts . y ) - FROM_TL [ getYanchor ( opts ) ] * opts . _effHeight ;
139
151
140
152
if ( opts . _main && fullLayout . margin . autoexpand ) {
@@ -640,6 +652,7 @@ function computeLegendDimensions(gd, groups, traces, opts) {
640
652
}
641
653
} else {
642
654
var xanchor = getXanchor ( opts ) ;
655
+ var isCenterOfPlotArea = ( xanchor === 'center' ) ;
643
656
var isLeftOfPlotArea = opts . x < 0 || ( opts . x === 0 && xanchor === 'right' ) ;
644
657
var isRightOfPlotArea = opts . x > 1 || ( opts . x === 1 && xanchor === 'left' ) ;
645
658
var isBeyondPlotAreaY = isAbovePlotArea || isBelowPlotArea ;
@@ -649,6 +662,7 @@ function computeLegendDimensions(gd, groups, traces, opts) {
649
662
// - else if below/above plot area and anchored in the margin, extend to opposite margin,
650
663
// - otherwise give it the maximum potential margin-push value
651
664
opts . _maxWidth = Math . max (
665
+ isCenterOfPlotArea ? fullLayout . width :
652
666
isLeftOfPlotArea ? ( ( isBeyondPlotAreaY && xanchor === 'left' ) ? gs . l + gs . w : hw ) :
653
667
isRightOfPlotArea ? ( ( isBeyondPlotAreaY && xanchor === 'right' ) ? gs . r + gs . w : hw ) :
654
668
gs . w ,
@@ -668,6 +682,7 @@ function computeLegendDimensions(gd, groups, traces, opts) {
668
682
var maxGroupHeightInRow = 0 ;
669
683
var groupOffsetX = 0 ;
670
684
var groupOffsetY = 0 ;
685
+ var next = 0 ;
671
686
groups . each ( function ( ) {
672
687
var maxWidthInGroup = 0 ;
673
688
var offsetY = 0 ;
@@ -682,7 +697,13 @@ function computeLegendDimensions(gd, groups, traces, opts) {
682
697
} ) ;
683
698
maxGroupHeightInRow = Math . max ( maxGroupHeightInRow , offsetY ) ;
684
699
685
- var next = maxWidthInGroup + itemGap ;
700
+ if ( yanchor === 'top' ) {
701
+ next = fullLayout . width / 3.99 ;
702
+ } else if ( yanchor === 'bottom' ) {
703
+ next = maxWidthInGroup + itemGap ;
704
+ } else {
705
+ next = maxWidthInGroup + itemGap ;
706
+ }
686
707
687
708
if ( ( next + bw + groupOffsetX ) > opts . _maxWidth ) {
688
709
maxRowWidth = Math . max ( maxRowWidth , groupOffsetX ) ;
0 commit comments