@@ -172,7 +172,18 @@ module.exports = function draw(gd) {
172
172
} ) ;
173
173
174
174
// Position and size the legend
175
- repositionLegend ( gd , traces ) ;
175
+ var lyMin = 0 ,
176
+ lyMax = fullLayout . height ;
177
+
178
+ computeLegendDimensions ( gd , traces ) ;
179
+
180
+ if ( opts . height > lyMax ) {
181
+ // If the legend doesn't fit in the plot area,
182
+ // do not expand the vertical margins.
183
+ expandHorizontalMargin ( gd ) ;
184
+ } else {
185
+ expandMargin ( gd ) ;
186
+ }
176
187
177
188
// Scroll section must be executed after repositionLegend.
178
189
// It requires the legend width, height, x and y to position the scrollbox
@@ -201,21 +212,16 @@ module.exports = function draw(gd) {
201
212
// Make sure the legend top and bottom are visible
202
213
// (legends with a scroll bar are not allowed to stretch beyond the extended
203
214
// margins)
204
- var lyMin = 0 ,
205
- lyMax = fullLayout . height ,
206
- legendHeight = opts . height ,
215
+ var legendHeight = opts . height ,
207
216
legendHeightMax = gs . h ;
208
217
209
-
210
218
if ( legendHeight > legendHeightMax ) {
211
219
ly = gs . t ;
212
220
legendHeight = legendHeightMax ;
213
221
}
214
222
else {
215
223
if ( ly > lyMax ) ly = lyMax - legendHeight ;
216
-
217
224
if ( ly < lyMin ) ly = lyMin ;
218
-
219
225
legendHeight = Math . min ( lyMax - ly , opts . height ) ;
220
226
}
221
227
@@ -369,7 +375,10 @@ function drawTexts(context, gd, d, i, traces) {
369
375
370
376
function textLayout ( s ) {
371
377
Plotly . util . convertToTspans ( s , function ( ) {
372
- if ( gd . firstRender ) repositionLegend ( gd , traces ) ;
378
+ if ( gd . firstRender ) {
379
+ computeLegendDimensions ( gd , traces ) ;
380
+ expandMargin ( gd ) ;
381
+ }
373
382
} ) ;
374
383
s . selectAll ( 'tspan.line' ) . attr ( { x : s . attr ( 'x' ) } ) ;
375
384
}
@@ -388,7 +397,7 @@ function drawTexts(context, gd, d, i, traces) {
388
397
else text . call ( textLayout ) ;
389
398
}
390
399
391
- function repositionLegend ( gd , traces ) {
400
+ function computeLegendDimensions ( gd , traces ) {
392
401
var fullLayout = gd . _fullLayout ,
393
402
opts = fullLayout . legend ,
394
403
borderwidth = opts . borderwidth ;
@@ -441,7 +450,6 @@ function repositionLegend(gd, traces) {
441
450
opts . width = Math . max ( opts . width , tWidth || 0 ) ;
442
451
} ) ;
443
452
444
-
445
453
opts . width += 45 + borderwidth * 2 ;
446
454
opts . height += 10 + borderwidth * 2 ;
447
455
@@ -452,29 +460,31 @@ function repositionLegend(gd, traces) {
452
460
traces . selectAll ( '.legendtoggle' )
453
461
. attr ( 'width' , ( gd . _context . editable ? 0 : opts . width ) + 40 ) ;
454
462
455
- // now position the legend. for both x,y the positions are recorded as
456
- // fractions of the plot area (left, bottom = 0,0).
463
+ // make sure we're only getting full pixels
464
+ opts . width = Math . ceil ( opts . width ) ;
465
+ opts . height = Math . ceil ( opts . height ) ;
466
+ }
467
+
468
+ function expandMargin ( gd ) {
469
+ var fullLayout = gd . _fullLayout ,
470
+ opts = fullLayout . legend ;
457
471
458
472
var xanchor = 'left' ;
459
473
if ( anchorUtils . isRightAnchor ( opts ) ) {
460
474
xanchor = 'right' ;
461
475
}
462
- if ( anchorUtils . isCenterAnchor ( opts ) ) {
476
+ else if ( anchorUtils . isCenterAnchor ( opts ) ) {
463
477
xanchor = 'center' ;
464
478
}
465
479
466
480
var yanchor = 'top' ;
467
481
if ( anchorUtils . isBottomAnchor ( opts ) ) {
468
482
yanchor = 'bottom' ;
469
483
}
470
- if ( anchorUtils . isMiddleAnchor ( opts ) ) {
484
+ else if ( anchorUtils . isMiddleAnchor ( opts ) ) {
471
485
yanchor = 'middle' ;
472
486
}
473
487
474
- // make sure we're only getting full pixels
475
- opts . width = Math . ceil ( opts . width ) ;
476
- opts . height = Math . ceil ( opts . height ) ;
477
-
478
488
// lastly check if the margin auto-expand has changed
479
489
Plots . autoMargin ( gd , 'legend' , {
480
490
x : opts . x ,
@@ -485,3 +495,26 @@ function repositionLegend(gd, traces) {
485
495
t : opts . height * ( { bottom : 1 , middle : 0.5 } [ yanchor ] || 0 )
486
496
} ) ;
487
497
}
498
+
499
+ function expandHorizontalMargin ( gd ) {
500
+ var fullLayout = gd . _fullLayout ,
501
+ opts = fullLayout . legend ;
502
+
503
+ var xanchor = 'left' ;
504
+ if ( anchorUtils . isRightAnchor ( opts ) ) {
505
+ xanchor = 'right' ;
506
+ }
507
+ else if ( anchorUtils . isCenterAnchor ( opts ) ) {
508
+ xanchor = 'center' ;
509
+ }
510
+
511
+ // lastly check if the margin auto-expand has changed
512
+ Plots . autoMargin ( gd , 'legend' , {
513
+ x : opts . x ,
514
+ y : 0.5 ,
515
+ l : opts . width * ( { right : 1 , center : 0.5 } [ xanchor ] || 0 ) ,
516
+ r : opts . width * ( { left : 1 , center : 0.5 } [ xanchor ] || 0 ) ,
517
+ b : 0 ,
518
+ t : 0
519
+ } ) ;
520
+ }
0 commit comments