Skip to content

Commit 63feec2

Browse files
authored
Merge pull request #7158 from attatrol/fix-legend-container-overflow
Fix legend container overflow
2 parents 5f101a8 + beb33ed commit 63feec2

6 files changed

Lines changed: 104 additions & 4 deletions

File tree

‎draftlogs/7158_fix.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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!

‎src/components/legend/attributes.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ module.exports = {
4040
editType: 'legend',
4141
description: [
4242
'Sets the max height (in px) of the legend, or max height ratio (reference height * ratio) if less than or equal to 1.',
43-
'Default value is: 0.5 for horizontal legends; 1 for vertical legends. The minimum allowed height is 30px.',
43+
'Default value is: 1 for vertically oriented legends with a `yref` of `"paper"`, located to the side of the plot;',
44+
'0.5 for all other legends. The minimum allowed height is 30px.',
4445
'For a ratio of 0.5, the legend will take up to 50% of the reference height before displaying a scrollbar.',
4546
'The reference height is the full layout height with the following exception: vertically oriented legends with',
46-
'a `yref` of `"paper", located to the side of the plot. In this case, the reference height is the plot height.'
47+
'a `yref` of `"paper"`, located to the side of the plot. In this case, the reference height is the plot height.',
48+
'For a legend with a `yref` of `"container"`, the max height is also limited to the space between',
49+
'the legend anchor and the container edge.'
4750
].join(' ')
4851
},
4952
borderwidth: {

‎src/components/legend/draw.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,12 @@ function computeLegendDimensions(gd, groups, traces, legendObj, scrollBox) {
891891
const heightToBeScaled = useFullLayoutHeight ? fullLayout.height : gs.h;
892892
legendObj._maxHeight = Math.max(maxheight > 1 ? maxheight : maxheight * heightToBeScaled, 30);
893893

894+
// Margins do not expand for a container legend, so keep the legend inside the container
895+
if (yref === 'container') {
896+
const roomHeight = getContainerRoom(legendObj.y, yanchor) * fullLayout.height;
897+
legendObj._maxHeight = Math.max(Math.min(legendObj._maxHeight, roomHeight), 30);
898+
}
899+
894900
var toggleRectWidth = 0;
895901
legendObj._width = 0;
896902
legendObj._height = 0;
@@ -932,6 +938,13 @@ function computeLegendDimensions(gd, groups, traces, legendObj, scrollBox) {
932938
isRightOfPlotArea ? ((isBeyondPlotAreaY && xanchor === 'right') ? gs.r + gs.w : hw) :
933939
gs.w,
934940
2 * textGap);
941+
942+
// The plot-area rules above do not apply to a container legend, so use the room on its anchor side
943+
if (legendObj.xref === 'container') {
944+
const roomWidth = getContainerRoom(legendObj.x, xanchor) * fullLayout.width;
945+
legendObj._maxWidth = Math.max(roomWidth, 2 * textGap);
946+
}
947+
935948
var maxItemWidth = 0;
936949
var combinedItemWidth = 0;
937950
traces.each(function(d) {
@@ -1149,3 +1162,23 @@ function getYanchor(legendObj) {
11491162
Lib.isMiddleAnchor(legendObj) ? 'middle' :
11501163
'top';
11511164
}
1165+
1166+
/**
1167+
* Get the fraction of the container that a legend has room to fill on its anchor side.
1168+
*
1169+
* @param pos - Legend `x` or `y` as a fraction of the container
1170+
* @param anchor - Legend anchor from `getXanchor` or `getYanchor`
1171+
* @returns The room as a fraction of the container width or height
1172+
*/
1173+
function getContainerRoom(pos, anchor) {
1174+
switch (anchor) {
1175+
case 'left':
1176+
case 'bottom':
1177+
return 1 - pos;
1178+
case 'right':
1179+
case 'top':
1180+
return pos;
1181+
default:
1182+
return 2 * Math.min(pos, 1 - pos);
1183+
}
1184+
}

‎src/types/generated/schema.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12803,7 +12803,7 @@ export interface Legend {
1280312803
*/
1280412804
itemwidth?: number;
1280512805
/**
12806-
* 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.
12806+
* 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.
1280712807
* Minimum: 0
1280812808
*/
1280912809
maxheight?: number;

‎test/jasmine/tests/legend_test.js‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,69 @@ describe('legend relayout update', function () {
15841584
})
15851585
.then(done, done.fail);
15861586
});
1587+
1588+
it('should constrain legend height when yref is container', function(done) {
1589+
var fig = {
1590+
data: Array.from({length: 20}, () => ({
1591+
x: [1, 2, 3],
1592+
y: [1, 2, 3]
1593+
})),
1594+
layout: {
1595+
legend: {
1596+
xref: 'container',
1597+
yref: 'container',
1598+
x:0.9
1599+
}
1600+
}
1601+
}
1602+
1603+
function _assert(msg, xy, wh) {
1604+
return function() {
1605+
var fullLayout = gd._fullLayout;
1606+
var legend3 = d3Select('g.legend');
1607+
var bg3 = legend3.select('rect.bg');
1608+
var translate = Drawing.getTranslate(legend3);
1609+
var x = translate.x;
1610+
var y = translate.y;
1611+
var w = +bg3.attr('width');
1612+
var h = +bg3.attr('height');
1613+
1614+
expect([x, y]).toBeWithinArray(xy, 25, msg + '| legend x,y');
1615+
expect([w, h]).toBeWithinArray(wh, 25, msg + '| legend w,h');
1616+
expect(x + w <= fullLayout.width).toBe(true, msg + '| fits in x');
1617+
expect(y + h <= fullLayout.height).toBe(true, msg + '| fits in y');
1618+
};
1619+
}
1620+
1621+
Plotly.newPlot(gd, fig)
1622+
.then(_assert('base', [539, 0], [101, 225]))
1623+
.then(function() { return Plotly.relayout(gd, 'legend.y', 0.6); })
1624+
.then(function() { return Plotly.relayout(gd, 'legend.yanchor', 'bottom'); })
1625+
.then(_assert('after moving legend towards the top edge', [539, 0], [101, 180]))
1626+
.then(function() { return Plotly.relayout(gd, 'legend.y', 0.4); })
1627+
.then(function() { return Plotly.relayout(gd, 'legend.yanchor', 'top'); })
1628+
.then(_assert('after moving legend towards the bottom edge', [539, 270], [101, 180]))
1629+
.then(done, done.fail);
1630+
});
1631+
1632+
it('should constrain horizontal legend width when xref is container', async () => {
1633+
const fig = {
1634+
data: Array.from({ length: 20 }, () => ({ x: [1, 2, 3], y: [1, 2, 3] })),
1635+
layout: {
1636+
legend: { orientation: 'h', xref: 'container', yref: 'container', x: 0.5 }
1637+
}
1638+
};
1639+
1640+
await Plotly.newPlot(gd, fig);
1641+
1642+
const { width } = gd._fullLayout;
1643+
const legend = d3Select('g.legend');
1644+
const x = Drawing.getTranslate(legend).x;
1645+
const w = +legend.select('rect.bg').attr('width');
1646+
1647+
expect(x).toBeCloseTo(width / 2, 0, 'legend x');
1648+
expect(x + w).toBeLessThanOrEqual(width, 'fits in x');
1649+
});
15871650
});
15881651

15891652
describe('legend orientation change:', function () {

‎test/plot-schema.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3495,7 +3495,7 @@
34953495
"valType": "number"
34963496
},
34973497
"maxheight": {
3498-
"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.",
3498+
"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.",
34993499
"editType": "legend",
35003500
"min": 0,
35013501
"valType": "number"

0 commit comments

Comments
 (0)