@@ -154,38 +154,39 @@ function drawOne(gd, opts) {
154
154
function ( ) {
155
155
var gs = fullLayout . _size ;
156
156
var bw = legendObj . borderwidth ;
157
+ var isPaperX = legendObj . xref === 'paper' ;
158
+ var isPaperY = legendObj . yref === 'paper' ;
157
159
158
160
if ( ! inHover ) {
159
- var expMargin = expandMargin ( gd , legendId ) ;
160
-
161
- // IF expandMargin return a Promise (which is truthy),
162
- // we're under a doAutoMargin redraw, so we don't have to
163
- // draw the remaining pieces below
164
- if ( expMargin ) return ;
165
-
166
161
var lx , ly ;
167
162
168
- if ( legendObj . xref === 'paper' ) {
163
+ if ( isPaperX ) {
169
164
lx = gs . l + gs . w * legendObj . x - FROM_TL [ getXanchor ( legendObj ) ] * legendObj . _width ;
170
165
} else {
171
166
legendObj . x = Lib . constrain ( legendObj . x , 0 , 1 ) ; // TODO: Move this to defaults setting?
172
167
lx = fullLayout . width * legendObj . x - FROM_TL [ getXanchor ( legendObj ) ] * legendObj . _width ;
173
168
}
174
169
175
- if ( legendObj . yref === 'paper' ) {
170
+ if ( isPaperY ) {
176
171
ly = gs . t + gs . h * ( 1 - legendObj . y ) - FROM_TL [ getYanchor ( legendObj ) ] * legendObj . _effHeight ;
177
172
} else {
178
173
legendObj . y = Lib . constrain ( legendObj . y , 0 , 1 ) ; // TODO: Move this to defaults setting?
179
174
ly = fullLayout . height * ( 1 - legendObj . y ) - FROM_TL [ getYanchor ( legendObj ) ] * legendObj . _effHeight ;
180
175
}
181
176
182
- // TODO: Does this also apply if y/xref=container?
177
+ var expMargin = expandMargin ( gd , legendId , lx , ly ) ;
178
+
179
+ // IF expandMargin return a Promise (which is truthy),
180
+ // we're under a doAutoMargin redraw, so we don't have to
181
+ // draw the remaining pieces below
182
+ if ( expMargin ) return ;
183
+
183
184
if ( fullLayout . margin . autoexpand ) {
184
185
var lx0 = lx ;
185
186
var ly0 = ly ;
186
187
187
- lx = Lib . constrain ( lx , 0 , fullLayout . width - legendObj . _width ) ;
188
- ly = Lib . constrain ( ly , 0 , fullLayout . height - legendObj . _effHeight ) ;
188
+ lx = isPaperX ? Lib . constrain ( lx , 0 , fullLayout . width - legendObj . _width ) : lx0 ;
189
+ ly = isPaperY ? Lib . constrain ( ly , 0 , fullLayout . height - legendObj . _effHeight ) : ly0 ;
189
190
190
191
if ( lx !== lx0 ) {
191
192
Lib . log ( 'Constrain ' + legendId + '.x to make legend fit inside graph' ) ;
@@ -890,20 +891,48 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
890
891
} ) ;
891
892
}
892
893
893
- function expandMargin ( gd , legendId ) {
894
+ function expandMargin ( gd , legendId , lx , ly ) {
894
895
var fullLayout = gd . _fullLayout ;
895
896
var legendObj = fullLayout [ legendId ] ;
896
897
var xanchor = getXanchor ( legendObj ) ;
897
898
var yanchor = getYanchor ( legendObj ) ;
898
899
899
- return Plots . autoMargin ( gd , legendId , {
900
- x : legendObj . x ,
901
- y : legendObj . y ,
902
- l : legendObj . _width * ( FROM_TL [ xanchor ] ) ,
903
- r : legendObj . _width * ( FROM_BR [ xanchor ] ) ,
904
- b : legendObj . _effHeight * ( FROM_BR [ yanchor ] ) ,
905
- t : legendObj . _effHeight * ( FROM_TL [ yanchor ] )
906
- } ) ;
900
+ var isPaperX = legendObj . xref === 'paper' ;
901
+ var isPaperY = legendObj . yref === 'paper' ;
902
+
903
+ gd . _fullLayout . _reservedMargin [ legendId ] = { } ;
904
+ var sideY = legendObj . y < 0.5 ? 'b' : 't' ;
905
+ var sideX = legendObj . x < 0.5 ? 'l' : 'r' ;
906
+ var possibleReservedMargins = {
907
+ r : ( fullLayout . width - lx ) ,
908
+ l : lx + legendObj . _width ,
909
+ b : ( fullLayout . height - ly ) ,
910
+ t : ly + legendObj . _effHeight
911
+ } ;
912
+
913
+ if ( isPaperX && isPaperY ) {
914
+ return Plots . autoMargin ( gd , legendId , {
915
+ x : legendObj . x ,
916
+ y : legendObj . y ,
917
+ l : legendObj . _width * ( FROM_TL [ xanchor ] ) ,
918
+ r : legendObj . _width * ( FROM_BR [ xanchor ] ) ,
919
+ b : legendObj . _effHeight * ( FROM_BR [ yanchor ] ) ,
920
+ t : legendObj . _effHeight * ( FROM_TL [ yanchor ] )
921
+ } ) ;
922
+ } else if ( isPaperX ) {
923
+ gd . _fullLayout . _reservedMargin [ legendId ] [ sideY ] = possibleReservedMargins [ sideY ] ;
924
+ return ;
925
+ } else if ( isPaperY ) {
926
+ gd . _fullLayout . _reservedMargin [ legendId ] [ sideX ] = possibleReservedMargins [ sideX ] ;
927
+ return ;
928
+ } else {
929
+ if ( legendObj . orientation === 'v' ) {
930
+ gd . _fullLayout . _reservedMargin [ legendId ] [ sideX ] = possibleReservedMargins [ sideX ] ;
931
+ } else {
932
+ gd . _fullLayout . _reservedMargin [ legendId ] [ sideY ] = possibleReservedMargins [ sideY ] ;
933
+ }
934
+ return ;
935
+ }
907
936
}
908
937
909
938
function getXanchor ( legendObj ) {
0 commit comments