diff --git a/draftlogs/7158_fix.md b/draftlogs/7158_fix.md new file mode 100644 index 00000000000..3bc4fdcf246 --- /dev/null +++ b/draftlogs/7158_fix.md @@ -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! diff --git a/src/components/legend/attributes.js b/src/components/legend/attributes.js index f6629622692..8d1af85ee08 100644 --- a/src/components/legend/attributes.js +++ b/src/components/legend/attributes.js @@ -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: { diff --git a/src/components/legend/draw.js b/src/components/legend/draw.js index b85a462bc2b..901f77ec98b 100644 --- a/src/components/legend/draw.js +++ b/src/components/legend/draw.js @@ -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; @@ -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) { @@ -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); + } +} diff --git a/src/types/generated/schema.d.ts b/src/types/generated/schema.d.ts index 7724577052f..853b8c815f9 100644 --- a/src/types/generated/schema.d.ts +++ b/src/types/generated/schema.d.ts @@ -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; diff --git a/test/jasmine/tests/legend_test.js b/test/jasmine/tests/legend_test.js index 18c2878d875..360e1343076 100644 --- a/test/jasmine/tests/legend_test.js +++ b/test/jasmine/tests/legend_test.js @@ -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 () { diff --git a/test/plot-schema.json b/test/plot-schema.json index 127a5fe337e..7ab1ae48d50 100644 --- a/test/plot-schema.json +++ b/test/plot-schema.json @@ -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"