Skip to content

fix: Update legend maxheight calculation logic #7483

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

Merged
merged 16 commits into from
Aug 7, 2025
Merged
Show file tree
Hide file tree
Changes from 13 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
8 changes: 4 additions & 4 deletions src/components/legend/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ module.exports = {
min: 0,
editType: 'legend',
description: [
'Sets the max height (in px) of the legend, or max height ratio (reference height * ratio) if less than one.',
'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 except for vertically oriented legends with',
'a `yref` of `"paper"`, where the reference height is the plot height.'
'The reference height is the full layout height with the following exceptions: legends to the side of the plot',
'or vertically oriented legends with a `yref` of `"paper". In this case, the reference height is the plot height.'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@camdecoster I think the previous version of this docstring correctly reflects what is in the code after your latest changes -- can you confirm?

Suggested change
'The reference height is the full layout height with the following exceptions: legends to the side of the plot',
'or vertically oriented legends with a `yref` of `"paper". In this case, the reference height is the plot height.'
'The reference height is the full layout height, except in the case of vertically oriented legends with',
'a `yref` of `"paper" located to the side of the plot, in which case the reference height',
'is the plot height.'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed, you're right. In some JS-like pseudocode:

isAboveBelow = isBelowPlotArea || isAbovePlotArea
!(isAboveBelow || !(orientation === "v" && yref === "paper")) -> !isAboveBelow && !(!(orientation === "v" && yref === "paper")) -> !isAboveBelow && (orientation === "v" && yref === "paper")

I'll switch it back the description back.

For future Cam reference, remember De Morgan's laws.

].join(' ')
},
borderwidth: {
Expand Down Expand Up @@ -243,7 +243,7 @@ module.exports = {
values: ['auto', 'top', 'middle', 'bottom'],
editType: 'legend',
description: [
'Sets the legend\'s vertical position anchor',
'Sets the legend\'s vertical position anchor.',
'This anchor binds the `y` position to the *top*, *middle*',
'or *bottom* of the legend.',
'Value *auto* anchors legends at their bottom for `y` values less than or equal to 1/3,',
Expand Down
2 changes: 1 addition & 1 deletion src/components/legend/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function groupDefaults(legendId, layoutIn, layoutOut, fullData) {

coerce('xanchor', defaultXAnchor);
coerce('yanchor', defaultYAnchor);
coerce('maxheight', isHorizontal ? 0.5 : 1);
coerce('maxheight');
coerce('valign');
Lib.noneOrAll(containerIn, containerOut, ['x', 'y']);

Expand Down
8 changes: 6 additions & 2 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,8 +769,12 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
var traceGroupGap = legendObj.tracegroupgap;
var legendGroupWidths = {};

var { maxheight, orientation, yref } = legendObj;
var heightToBeScaled = orientation === "v" && yref === "paper" ? gs.h : fullLayout.height;
const { orientation, yref } = legendObj;
let { maxheight } = legendObj;
const useFullLayoutHeight = isBelowPlotArea || isAbovePlotArea || !(orientation === "v" && yref === "paper")
// Set default maxheight here since it depends on values passed in by user
maxheight ||= useFullLayoutHeight ? 0.5 : 1;
const heightToBeScaled = useFullLayoutHeight ? fullLayout.height : gs.h;
legendObj._maxHeight = Math.max(maxheight > 1 ? maxheight : maxheight * heightToBeScaled, 30);

var toggleRectWidth = 0;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading