Skip to content

Allow configuration of horizontal legend max height (ratio || pixel) #5106

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

Closed
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
18 changes: 18 additions & 0 deletions src/components/legend/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ module.exports = {
editType: 'legend',
description: 'Sets the color of the border enclosing the legend.'
},
hmaxheightratio: {
valType: 'number',
min: 2,
dflt: 2,
role: 'style',
editType: 'legend',
description: [
'Sets the max height ratio (layout / ratio) of the visible legend when horizontaly aligned.',
'Default value is 2; the legend will take up to 50% of the layout height before displaying a scrollbar',
].join(' ')
},
hmaxheight: {
valType: 'number',
min: 0,
role: 'style',
editType: 'legend',
description: 'Sets the max height (in px) of the horizontaly aligned legend.'
},
Copy link
Contributor

Choose a reason for hiding this comment

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

@p1-ra thanks very much for the PR.
Maybe by inverting the hmaxheightratio: we could combine the two attributes into one.
Something like:

maxheight: {
    valType: 'number',
    min: 0,
    dflt: 0.5,
    role: 'style',
    editType: 'legend',
    description: [
      'If greater than one, it sets the max height (in px) of the horizontaly aligned legend.',
      'Otherwise, it sets the max height ratio (layout * ratio) of the visible legend when horizontaly aligned.',
      'Default value is 0.5; the legend will take up to 50% of the layout height before displaying a scrollbar'
    ].join(' ')
},

borderwidth: {
valType: 'number',
min: 0,
Expand Down
2 changes: 2 additions & 0 deletions src/components/legend/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ module.exports = function legendDefaults(layoutIn, layoutOut, fullData) {
coerce('xanchor');
coerce('y', defaultY);
coerce('yanchor', defaultYAnchor);
coerce('hmaxheightratio');
coerce('hmaxheight');
coerce('valign');
Lib.noneOrAll(containerIn, containerOut, ['x', 'y']);

Expand Down
16 changes: 11 additions & 5 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,12 +602,18 @@ function computeLegendDimensions(gd, groups, traces, opts) {
var isBelowPlotArea = opts.y < 0 || (opts.y === 0 && yanchor === 'top');
var isAbovePlotArea = opts.y > 1 || (opts.y === 1 && yanchor === 'bottom');

// - if below/above plot area, give it the maximum potential margin-push value
// - if below/above plot area, give it the [user defined] maximum potential margin-push value
// - otherwise, extend the height of the plot area
opts._maxHeight = Math.max(
(isBelowPlotArea || isAbovePlotArea) ? fullLayout.height / 2 : gs.h,
30
);
if (isBelowPlotArea || isAbovePlotArea) {
if (opts.hmaxheight !== undefined) {
opts._maxHeight = opts.hmaxheight;
} else {
opts._maxHeight = fullLayout.height / opts.hmaxheightratio;
}
}
else {
opts._maxHeight = Math.max(gs.h, 30);
}

var toggleRectWidth = 0;
opts._width = 0;
Expand Down