Skip to content

Commit ed3823b

Browse files
authoredOct 22, 2021
Merge pull request #5996 from plotly/legend-dont-wrap-1st-col
Do not wrap (enter to first column) legend items if already on the first column
2 parents aef563b + 7bdca3e commit ed3823b

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed
 

‎draftlogs/5996_fix.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Do not wrap legend items if already on the first column [[#5996](https://github.com/plotly/plotly.js/pull/5996)]

‎src/components/legend/draw.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -700,19 +700,28 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
700700
var maxWidthInGroup = 0;
701701
var offsetY = 0;
702702
d3.select(this).selectAll('g.traces').each(function(d) {
703+
var w = d[0].width;
703704
var h = d[0].height;
705+
704706
Drawing.setTranslate(this,
705707
titleSize[0],
706708
titleSize[1] + bw + itemGap + h / 2 + offsetY
707709
);
708710
offsetY += h;
709-
maxWidthInGroup = Math.max(maxWidthInGroup, textGap + d[0].width);
711+
maxWidthInGroup = Math.max(maxWidthInGroup, textGap + w);
710712
});
711713
maxGroupHeightInRow = Math.max(maxGroupHeightInRow, offsetY);
712714

713715
var next = maxWidthInGroup + itemGap;
714716

715-
if((next + bw + groupOffsetX) > legendObj._maxWidth) {
717+
// horizontal_wrapping
718+
if(
719+
// not on the first column already
720+
groupOffsetX > 0 &&
721+
722+
// goes beyound limit
723+
next + bw + groupOffsetX > legendObj._maxWidth
724+
) {
716725
maxRowWidth = Math.max(maxRowWidth, groupOffsetX);
717726
groupOffsetX = 0;
718727
groupOffsetY += maxGroupHeightInRow + traceGroupGap;
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"data": [
3+
{
4+
"legendgroup": "A",
5+
"name": "trace A1",
6+
"x": [
7+
0,
8+
10
9+
],
10+
"y": [
11+
1,
12+
1
13+
]
14+
},
15+
{
16+
"legendgroup": "A",
17+
"name": "trace A2",
18+
"x": [
19+
10,
20+
20
21+
],
22+
"y": [
23+
2,
24+
2
25+
]
26+
},
27+
{
28+
"legendgroup": "B",
29+
"name": "trace B1",
30+
"x": [
31+
20,
32+
30
33+
],
34+
"y": [
35+
3,
36+
3
37+
]
38+
}
39+
],
40+
"layout": {
41+
"showlegend": true,
42+
"legend": {
43+
"orientation": "h",
44+
"xanchor": "left",
45+
"yanchor": "top",
46+
"bgcolor": "yellow"
47+
},
48+
"paper_bgcolor": "lightblue",
49+
"width": 250,
50+
"height": 250
51+
}
52+
}

0 commit comments

Comments
 (0)
Please sign in to comment.