Skip to content
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

Add "top left" & "top center" side options to legend title #6711

Merged
merged 6 commits into from
Aug 22, 2023
Merged
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
3 changes: 2 additions & 1 deletion src/components/legend/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,15 @@ module.exports = {
}),
side: {
valType: 'enumerated',
values: ['top', 'left', 'top left'],
values: ['top', 'left', 'top left', 'top center', 'top right'],
editType: 'legend',
description: [
'Determines the location of legend\'s title',
'with respect to the legend items.',
'Defaulted to *top* with `orientation` is *h*.',
'Defaulted to *left* with `orientation` is *v*.',
'The *top left* options could be used to expand',
'top center and top right are for horizontal alignment',
'legend area in both x and y sides.'
].join(' ')
},
Expand Down
7 changes: 6 additions & 1 deletion src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,13 +661,18 @@ function computeTextDimensions(g, gd, legendObj, aTitle) {
// approximation to height offset to center the font
// to avoid getBoundingClientRect
if(aTitle === MAIN_TITLE) {
var titleOffset = 0;
if(legendObj.title.side === 'left') {
// add extra space between legend title and itmes
width += constants.itemGap * 2;
} else if(legendObj.title.side === 'top center') {
if(legendObj._width) titleOffset = 0.5 * (legendObj._width - 2 * bw - 2 * constants.titlePad - width);
} else if(legendObj.title.side === 'top right') {
if(legendObj._width) titleOffset = legendObj._width - 2 * bw - 2 * constants.titlePad - width;
}

svgTextUtils.positionText(textEl,
bw + constants.titlePad,
bw + constants.titlePad + titleOffset,
bw + lineHeight
);
} else { // legend item
Expand Down
Binary file modified test/image/baselines/legend_horizontal_autowrap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/legend_horizontal_one_row.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/legend_itemwidth_dashline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion test/image/mocks/legend_horizontal_autowrap.json
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,8 @@
"size": 11.9552
},
"y": -0.4,
"orientation": "h"
"orientation": "h",
"title": { "text": "Legend Title", "side": "top right" }
},
"hovermode": "closest",
"width": 680,
Expand Down
2 changes: 1 addition & 1 deletion test/image/mocks/legend_horizontal_one_row.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
],
"layout": {
"title": {"text": "Average Distribution per Month"},
"legend": { "orientation": "h" }
"legend": { "orientation": "h", "title": { "text": "Legend Title", "side": "top center" }}
}
}
3 changes: 2 additions & 1 deletion test/image/mocks/legend_itemwidth_dashline.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
],
"layout": {
"legend": {
"itemwidth": 60
"itemwidth": 60,
"title": { "text": "Title", "side": "top center" }
}
}
}
6 changes: 4 additions & 2 deletions test/plot-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2923,13 +2923,15 @@
},
"role": "object",
"side": {
"description": "Determines the location of legend's title with respect to the legend items. Defaulted to *top* with `orientation` is *h*. Defaulted to *left* with `orientation` is *v*. The *top left* options could be used to expand legend area in both x and y sides.",
"description": "Determines the location of legend's title with respect to the legend items. Defaulted to *top* with `orientation` is *h*. Defaulted to *left* with `orientation` is *v*. The *top left* options could be used to expand top center and top right are for horizontal alignment legend area in both x and y sides.",
"editType": "legend",
"valType": "enumerated",
"values": [
"top",
"left",
"top left"
"top left",
"top center",
"top right"
]
},
"text": {
Expand Down