Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix legend container overflow #7158

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions draftlogs/7072-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix maximum dimensions for legend that is anchored to container [[#7072](https://github.com/plotly/plotly.js/pull/7072)]
67 changes: 46 additions & 21 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -774,18 +774,32 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
var endPad = 2 * (bw + itemGap);

var yanchor = getYanchor(legendObj);
var isBelowPlotArea = legendObj.y < 0 || (legendObj.y === 0 && yanchor === 'top');
var isAbovePlotArea = legendObj.y > 1 || (legendObj.y === 1 && yanchor === 'bottom');
var isBelowPlotArea;
var isAbovePlotArea;

var traceGroupGap = legendObj.tracegroupgap;
var legendGroupWidths = {};

// - if below/above plot area, give it the maximum potential margin-push value
// - otherwise, extend the height of the plot area
legendObj._maxHeight = Math.max(
(isBelowPlotArea || isAbovePlotArea) ? fullLayout.height / 2 : gs.h,
30
);
if(legendObj.yref === 'paper') {
isBelowPlotArea = legendObj.y < 0 || (legendObj.y === 0 && yanchor === 'top');
isAbovePlotArea = legendObj.y > 1 || (legendObj.y === 1 && yanchor === 'bottom');
// - if below/above plot area, give it the maximum potential margin-push value
// - otherwise, extend the height of the plot area
legendObj._maxHeight = Math.max(
(isBelowPlotArea || isAbovePlotArea) ? fullLayout.height / 2 : gs.h,
30
);
} else {
isBelowPlotArea = legendObj.y * fullLayout.height < gs.b || (legendObj.y * fullLayout.height === gs.b && yanchor === 'top');
isAbovePlotArea = legendObj.y * fullLayout.height > gs.b + gs.h || (legendObj.y * fullLayout.height === gs.b + gs.h && yanchor === 'bottom');
if(yanchor === 'top')
legendObj._maxHeight = legendObj.y * fullLayout.height;
else if(yanchor === 'bottom')
legendObj._maxHeight = (1 - legendObj.y) * fullLayout.height;
else // if (yanchor === 'middle')
legendObj._maxHeight = 2 * Math.min(1 - legendObj.y, legendObj.y) * fullLayout.height;
legendObj._maxHeight = Math.max(legendObj._maxHeight, 30);
}

var toggleRectWidth = 0;
legendObj._width = 0;
Expand Down Expand Up @@ -815,19 +829,30 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
}
} else {
var xanchor = getXanchor(legendObj);
var isLeftOfPlotArea = legendObj.x < 0 || (legendObj.x === 0 && xanchor === 'right');
var isRightOfPlotArea = legendObj.x > 1 || (legendObj.x === 1 && xanchor === 'left');
var isBeyondPlotAreaY = isAbovePlotArea || isBelowPlotArea;
var hw = fullLayout.width / 2;

// - if placed within x-margins, extend the width of the plot area
// - else if below/above plot area and anchored in the margin, extend to opposite margin,
// - otherwise give it the maximum potential margin-push value
legendObj._maxWidth = Math.max(
isLeftOfPlotArea ? ((isBeyondPlotAreaY && xanchor === 'left') ? gs.l + gs.w : hw) :
isRightOfPlotArea ? ((isBeyondPlotAreaY && xanchor === 'right') ? gs.r + gs.w : hw) :
gs.w,
2 * textGap);
if(legendObj.xref === 'paper') {
var isLeftOfPlotArea = legendObj.x < 0 || (legendObj.x === 0 && xanchor === 'right');
var isRightOfPlotArea = legendObj.x > 1 || (legendObj.x === 1 && xanchor === 'left');
var isBeyondPlotAreaY = isAbovePlotArea || isBelowPlotArea;
var hw = fullLayout.width / 2;

// - if placed within x-margins, extend the width of the plot area
// - else if below/above plot area and anchored in the margin, extend to opposite margin,
// - otherwise give it the maximum potential margin-push value
legendObj._maxWidth = Math.max(
isLeftOfPlotArea ? ((isBeyondPlotAreaY && xanchor === 'left') ? gs.l + gs.w : hw) :
isRightOfPlotArea ? ((isBeyondPlotAreaY && xanchor === 'right') ? gs.r + gs.w : hw) :
gs.w,
2 * textGap);
} else {
if(xanchor === 'right')
legendObj._maxWidth = legendObj.x * fullLayout.width;
else if(xanchor === 'left')
legendObj._maxWidth = (1 - legendObj.x) * fullLayout.width;
else // if (xanchor === 'center')
legendObj._maxWidth = 2 * Math.min(1 - legendObj.x, legendObj.x) * fullLayout.width;
legendObj._maxWidth = Math.max(legendObj._maxWidth, 2 * textGap);
}

var maxItemWidth = 0;
var combinedItemWidth = 0;
traces.each(function(d) {
Expand Down