Skip to content

Commit 8baac47

Browse files
committed
Don't expand vertical margin if legend doesn't fit
1 parent b747224 commit 8baac47

File tree

1 file changed

+51
-18
lines changed

1 file changed

+51
-18
lines changed

src/components/legend/draw.js

+51-18
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,18 @@ module.exports = function draw(gd) {
172172
});
173173

174174
// 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+
}
176187

177188
// Scroll section must be executed after repositionLegend.
178189
// It requires the legend width, height, x and y to position the scrollbox
@@ -201,21 +212,16 @@ module.exports = function draw(gd) {
201212
// Make sure the legend top and bottom are visible
202213
// (legends with a scroll bar are not allowed to stretch beyond the extended
203214
// margins)
204-
var lyMin = 0,
205-
lyMax = fullLayout.height,
206-
legendHeight = opts.height,
215+
var legendHeight = opts.height,
207216
legendHeightMax = gs.h;
208217

209-
210218
if(legendHeight > legendHeightMax) {
211219
ly = gs.t;
212220
legendHeight = legendHeightMax;
213221
}
214222
else {
215223
if(ly > lyMax) ly = lyMax - legendHeight;
216-
217224
if(ly < lyMin) ly = lyMin;
218-
219225
legendHeight = Math.min(lyMax - ly, opts.height);
220226
}
221227

@@ -369,7 +375,10 @@ function drawTexts(context, gd, d, i, traces) {
369375

370376
function textLayout(s) {
371377
Plotly.util.convertToTspans(s, function() {
372-
if(gd.firstRender) repositionLegend(gd, traces);
378+
if(gd.firstRender) {
379+
computeLegendDimensions(gd, traces);
380+
expandMargin(gd);
381+
}
373382
});
374383
s.selectAll('tspan.line').attr({x: s.attr('x')});
375384
}
@@ -388,7 +397,7 @@ function drawTexts(context, gd, d, i, traces) {
388397
else text.call(textLayout);
389398
}
390399

391-
function repositionLegend(gd, traces) {
400+
function computeLegendDimensions(gd, traces) {
392401
var fullLayout = gd._fullLayout,
393402
opts = fullLayout.legend,
394403
borderwidth = opts.borderwidth;
@@ -441,7 +450,6 @@ function repositionLegend(gd, traces) {
441450
opts.width = Math.max(opts.width, tWidth || 0);
442451
});
443452

444-
445453
opts.width += 45 + borderwidth * 2;
446454
opts.height += 10 + borderwidth * 2;
447455

@@ -452,29 +460,31 @@ function repositionLegend(gd, traces) {
452460
traces.selectAll('.legendtoggle')
453461
.attr('width', (gd._context.editable ? 0 : opts.width) + 40);
454462

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;
457471

458472
var xanchor = 'left';
459473
if(anchorUtils.isRightAnchor(opts)) {
460474
xanchor = 'right';
461475
}
462-
if(anchorUtils.isCenterAnchor(opts)) {
476+
else if(anchorUtils.isCenterAnchor(opts)) {
463477
xanchor = 'center';
464478
}
465479

466480
var yanchor = 'top';
467481
if(anchorUtils.isBottomAnchor(opts)) {
468482
yanchor = 'bottom';
469483
}
470-
if(anchorUtils.isMiddleAnchor(opts)) {
484+
else if(anchorUtils.isMiddleAnchor(opts)) {
471485
yanchor = 'middle';
472486
}
473487

474-
// make sure we're only getting full pixels
475-
opts.width = Math.ceil(opts.width);
476-
opts.height = Math.ceil(opts.height);
477-
478488
// lastly check if the margin auto-expand has changed
479489
Plots.autoMargin(gd, 'legend', {
480490
x: opts.x,
@@ -485,3 +495,26 @@ function repositionLegend(gd, traces) {
485495
t: opts.height * ({bottom: 1, middle: 0.5}[yanchor] || 0)
486496
});
487497
}
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

Comments
 (0)