Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5757a02
Fix legend size calculations when it is anchored to container.
Sep 16, 2024
d5e9935
Add draftlog for PR 7072
Sep 16, 2024
1fc4266
Code formatting.
Sep 17, 2024
68980a4
Calculate flags isBelowPlotArea/isAbovePlotArea when legend yref is c…
Sep 17, 2024
1d5aea2
Update draftlogs/7072-fix.md
archmoj Dec 16, 2024
5be7cec
Merge remote-tracking branch 'origin/master' into fix-legend-containe…
camdecoster Apr 22, 2025
4a1119a
Update block to handle user specified maxheight
camdecoster Apr 22, 2025
737ae2a
Merge remote-tracking branch 'origin/master' into pr/7249
alexshoe Oct 27, 2025
e6732be
Reduce variable repetition and simplify logic
alexshoe Oct 28, 2025
7a6e064
Add image mock for testing the bug fix
alexshoe Oct 28, 2025
76b6ce6
Fix linting error using `biome`
alexshoe Oct 28, 2025
ba666af
Add baseline image for new mock
alexshoe Oct 28, 2025
30590f0
Merge branch 'master' into pr/7158
alexshoe Oct 29, 2025
9dd1b48
Add CircleCI generated baseline images
alexshoe Nov 3, 2025
5be429b
Improve logic and readability for legend height calculation
alexshoe Nov 11, 2025
c0e6fd6
Refactor legend maxheight/maxwidth computation for readability
alexshoe Nov 12, 2025
ee229b9
Fix logic for setting `maxheight` value
alexshoe Nov 13, 2025
3542f8e
Remove image test and replace with Jasmine test
alexshoe Nov 13, 2025
676f829
Remove debugging logs
alexshoe Nov 13, 2025
8d98c89
Merge remote-tracking branch 'origin/main' into fix-legend-container-…
camdecoster Nov 24, 2026
5f9ec5a
Limit legend size caps to container references
camdecoster Nov 24, 2026
dbe714b
Rename draftlog
camdecoster Nov 24, 2026
4a732b7
Update draftlog
camdecoster Nov 24, 2026
beb33ed
Correct the legend maxheight description
camdecoster Nov 24, 2026
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/7158_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix maximum height and width of a legend with `yref` or `xref` set to `"container"` [[#7158](https://github.com/plotly/plotly.js/pull/7158)], with thanks to @attatrol for the contribution!
7 changes: 5 additions & 2 deletions src/components/legend/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ module.exports = {
editType: 'legend',
description: [
'Sets the max height (in px) of the legend, or max height ratio (reference height * ratio) if less than or equal to 1.',
'Default value is: 0.5 for horizontal legends; 1 for vertical legends. The minimum allowed height is 30px.',
'Default value is: 1 for vertically oriented legends with a `yref` of `"paper"`, located to the side of the plot;',
'0.5 for all other legends. The minimum allowed height is 30px.',
'For a ratio of 0.5, the legend will take up to 50% of the reference height before displaying a scrollbar.',
'The reference height is the full layout height with the following exception: vertically oriented legends with',
'a `yref` of `"paper", located to the side of the plot. In this case, the reference height is the plot height.'
'a `yref` of `"paper"`, located to the side of the plot. In this case, the reference height is the plot height.',
'For a legend with a `yref` of `"container"`, the max height is also limited to the space between',
'the legend anchor and the container edge.'
].join(' ')
},
borderwidth: {
Expand Down
33 changes: 33 additions & 0 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,12 @@ function computeLegendDimensions(gd, groups, traces, legendObj, scrollBox) {
const heightToBeScaled = useFullLayoutHeight ? fullLayout.height : gs.h;
legendObj._maxHeight = Math.max(maxheight > 1 ? maxheight : maxheight * heightToBeScaled, 30);

// Margins do not expand for a container legend, so keep the legend inside the container
if (yref === 'container') {
const roomHeight = getContainerRoom(legendObj.y, yanchor) * fullLayout.height;
legendObj._maxHeight = Math.max(Math.min(legendObj._maxHeight, roomHeight), 30);
}

var toggleRectWidth = 0;
legendObj._width = 0;
legendObj._height = 0;
Expand Down Expand Up @@ -932,6 +938,13 @@ function computeLegendDimensions(gd, groups, traces, legendObj, scrollBox) {
isRightOfPlotArea ? ((isBeyondPlotAreaY && xanchor === 'right') ? gs.r + gs.w : hw) :
gs.w,
2 * textGap);

// The plot-area rules above do not apply to a container legend, so use the room on its anchor side
if (legendObj.xref === 'container') {
const roomWidth = getContainerRoom(legendObj.x, xanchor) * fullLayout.width;
legendObj._maxWidth = Math.max(roomWidth, 2 * textGap);
}

var maxItemWidth = 0;
var combinedItemWidth = 0;
traces.each(function(d) {
Expand Down Expand Up @@ -1149,3 +1162,23 @@ function getYanchor(legendObj) {
Lib.isMiddleAnchor(legendObj) ? 'middle' :
'top';
}

/**
* Get the fraction of the container that a legend has room to fill on its anchor side.
*
* @param pos - Legend `x` or `y` as a fraction of the container
* @param anchor - Legend anchor from `getXanchor` or `getYanchor`
* @returns The room as a fraction of the container width or height
*/
function getContainerRoom(pos, anchor) {
switch (anchor) {
case 'left':
case 'bottom':
return 1 - pos;
case 'right':
case 'top':
return pos;
default:
return 2 * Math.min(pos, 1 - pos);
}
}
2 changes: 1 addition & 1 deletion src/types/generated/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12803,7 +12803,7 @@ export interface Legend {
*/
itemwidth?: number;
/**
* Sets the max height (in px) of the legend, or max height ratio (reference height * ratio) if less than or equal to 1. Default value is: 0.5 for horizontal legends; 1 for vertical legends. The minimum allowed height is 30px. For a ratio of 0.5, the legend will take up to 50% of the reference height before displaying a scrollbar. The reference height is the full layout height with the following exception: vertically oriented legends with a `yref` of `"paper", located to the side of the plot. In this case, the reference height is the plot height.
* Sets the max height (in px) of the legend, or max height ratio (reference height * ratio) if less than or equal to 1. Default value is: 1 for vertically oriented legends with a `yref` of `"paper"`, located to the side of the plot; 0.5 for all other legends. The minimum allowed height is 30px. For a ratio of 0.5, the legend will take up to 50% of the reference height before displaying a scrollbar. The reference height is the full layout height with the following exception: vertically oriented legends with a `yref` of `"paper"`, located to the side of the plot. In this case, the reference height is the plot height. For a legend with a `yref` of `"container"`, the max height is also limited to the space between the legend anchor and the container edge.
* Minimum: 0
*/
maxheight?: number;
Expand Down
63 changes: 63 additions & 0 deletions test/jasmine/tests/legend_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,69 @@ describe('legend relayout update', function () {
})
.then(done, done.fail);
});

it('should constrain legend height when yref is container', function(done) {
var fig = {
data: Array.from({length: 20}, () => ({
x: [1, 2, 3],
y: [1, 2, 3]
})),
layout: {
legend: {
xref: 'container',
yref: 'container',
x:0.9
}
}
}

function _assert(msg, xy, wh) {
return function() {
var fullLayout = gd._fullLayout;
var legend3 = d3Select('g.legend');
var bg3 = legend3.select('rect.bg');
var translate = Drawing.getTranslate(legend3);
var x = translate.x;
var y = translate.y;
var w = +bg3.attr('width');
var h = +bg3.attr('height');

expect([x, y]).toBeWithinArray(xy, 25, msg + '| legend x,y');
expect([w, h]).toBeWithinArray(wh, 25, msg + '| legend w,h');
expect(x + w <= fullLayout.width).toBe(true, msg + '| fits in x');
expect(y + h <= fullLayout.height).toBe(true, msg + '| fits in y');
};
}

Plotly.newPlot(gd, fig)
.then(_assert('base', [539, 0], [101, 225]))
.then(function() { return Plotly.relayout(gd, 'legend.y', 0.6); })
.then(function() { return Plotly.relayout(gd, 'legend.yanchor', 'bottom'); })
.then(_assert('after moving legend towards the top edge', [539, 0], [101, 180]))
.then(function() { return Plotly.relayout(gd, 'legend.y', 0.4); })
.then(function() { return Plotly.relayout(gd, 'legend.yanchor', 'top'); })
.then(_assert('after moving legend towards the bottom edge', [539, 270], [101, 180]))
.then(done, done.fail);
});

it('should constrain horizontal legend width when xref is container', async () => {
const fig = {
data: Array.from({ length: 20 }, () => ({ x: [1, 2, 3], y: [1, 2, 3] })),
layout: {
legend: { orientation: 'h', xref: 'container', yref: 'container', x: 0.5 }
}
};

await Plotly.newPlot(gd, fig);

const { width } = gd._fullLayout;
const legend = d3Select('g.legend');
const x = Drawing.getTranslate(legend).x;
const w = +legend.select('rect.bg').attr('width');

expect(x).toBeCloseTo(width / 2, 0, 'legend x');
expect(x + w).toBeLessThanOrEqual(width, 'fits in x');
});
});

describe('legend orientation change:', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/plot-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3495,7 +3495,7 @@
"valType": "number"
},
"maxheight": {
"description": "Sets the max height (in px) of the legend, or max height ratio (reference height * ratio) if less than or equal to 1. Default value is: 0.5 for horizontal legends; 1 for vertical legends. The minimum allowed height is 30px. For a ratio of 0.5, the legend will take up to 50% of the reference height before displaying a scrollbar. The reference height is the full layout height with the following exception: vertically oriented legends with a `yref` of `\"paper\", located to the side of the plot. In this case, the reference height is the plot height.",
"description": "Sets the max height (in px) of the legend, or max height ratio (reference height * ratio) if less than or equal to 1. Default value is: 1 for vertically oriented legends with a `yref` of `\"paper\"`, located to the side of the plot; 0.5 for all other legends. The minimum allowed height is 30px. For a ratio of 0.5, the legend will take up to 50% of the reference height before displaying a scrollbar. The reference height is the full layout height with the following exception: vertically oriented legends with a `yref` of `\"paper\"`, located to the side of the plot. In this case, the reference height is the plot height. For a legend with a `yref` of `\"container\"`, the max height is also limited to the space between the legend anchor and the container edge.",
"editType": "legend",
"min": 0,
"valType": "number"
Expand Down
Loading