Skip to content

Do not wrap (enter to first column) legend items if already on the first column #5996

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 5 commits into from
Oct 22, 2021
Merged
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
1 change: 1 addition & 0 deletions draftlogs/5996_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Do not wrap legend items if already on the first column [[#5996](https://github.com/plotly/plotly.js/pull/5996)]
13 changes: 11 additions & 2 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
@@ -700,19 +700,28 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
var maxWidthInGroup = 0;
var offsetY = 0;
d3.select(this).selectAll('g.traces').each(function(d) {
var w = d[0].width;
var h = d[0].height;

Drawing.setTranslate(this,
titleSize[0],
titleSize[1] + bw + itemGap + h / 2 + offsetY
);
offsetY += h;
maxWidthInGroup = Math.max(maxWidthInGroup, textGap + d[0].width);
maxWidthInGroup = Math.max(maxWidthInGroup, textGap + w);
});
maxGroupHeightInRow = Math.max(maxGroupHeightInRow, offsetY);

var next = maxWidthInGroup + itemGap;

if((next + bw + groupOffsetX) > legendObj._maxWidth) {
// horizontal_wrapping
if(
// not on the first column already
groupOffsetX > 0 &&

// goes beyound limit
next + bw + groupOffsetX > legendObj._maxWidth
) {
maxRowWidth = Math.max(maxRowWidth, groupOffsetX);
groupOffsetX = 0;
groupOffsetY += maxGroupHeightInRow + traceGroupGap;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions test/image/mocks/wrap_legend_horizontal_first-column.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"data": [
{
"legendgroup": "A",
"name": "trace A1",
"x": [
0,
10
],
"y": [
1,
1
]
},
{
"legendgroup": "A",
"name": "trace A2",
"x": [
10,
20
],
"y": [
2,
2
]
},
{
"legendgroup": "B",
"name": "trace B1",
"x": [
20,
30
],
"y": [
3,
3
]
}
],
"layout": {
"showlegend": true,
"legend": {
"orientation": "h",
"xanchor": "left",
"yanchor": "top",
"bgcolor": "yellow"
},
"paper_bgcolor": "lightblue",
"width": 250,
"height": 250
}
}