Skip to content

Commit 0251a0c

Browse files
authored
Merge pull request #3446 from plotly/horiz-legend-wrap-all-lines-fix
Fix wrapped horizontal legends height computations
2 parents 13daed2 + 0ed6d4b commit 0251a0c

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

src/components/legend/draw.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,6 @@ function computeLegendDimensions(gd, groups, traces) {
630630
var offsetX = 0;
631631
var fullTracesWidth = 0;
632632
var traceGap = opts.tracegroupgap || 5;
633-
var oneRowLegend;
634633

635634
// calculate largest width for traces and use for width of all legend items
636635
traces.each(function(d) {
@@ -639,15 +638,16 @@ function computeLegendDimensions(gd, groups, traces) {
639638
});
640639

641640
// check if legend fits in one row
642-
oneRowLegend = fullLayout._size.w > borderwidth + fullTracesWidth - traceGap;
641+
var oneRowLegend = fullLayout._size.w > borderwidth + fullTracesWidth - traceGap;
642+
643643
traces.each(function(d) {
644644
var legendItem = d[0];
645645
var traceWidth = oneRowLegend ? 40 + d[0].width : maxTraceWidth;
646646

647647
if((borderwidth + offsetX + traceGap + traceWidth) > fullLayout._size.w) {
648648
offsetX = 0;
649-
rowHeight = rowHeight + maxTraceHeight;
650-
opts._height = opts._height + maxTraceHeight;
649+
rowHeight += maxTraceHeight;
650+
opts._height += maxTraceHeight;
651651
// reset for next row
652652
maxTraceHeight = 0;
653653
}
@@ -657,16 +657,20 @@ function computeLegendDimensions(gd, groups, traces) {
657657
(5 + borderwidth + legendItem.height / 2) + rowHeight);
658658

659659
opts._width += traceGap + traceWidth;
660-
opts._height = Math.max(opts._height, legendItem.height);
661660

662661
// keep track of tallest trace in group
663662
offsetX += traceGap + traceWidth;
664663
maxTraceHeight = Math.max(legendItem.height, maxTraceHeight);
665664
});
666665

666+
if(oneRowLegend) {
667+
opts._height = maxTraceHeight;
668+
} else {
669+
opts._height += maxTraceHeight;
670+
}
671+
667672
opts._width += borderwidth * 2;
668673
opts._height += 10 + borderwidth * 2;
669-
670674
}
671675

672676
// make sure we're only getting full pixels
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"data": [
3+
{
4+
"x": [ 1, 2, 3, 4 ],
5+
"y": [ 3, 5, 1, 7 ],
6+
"name": "Break this and last<br>trace will display properly"
7+
},
8+
{
9+
"x": [ 1, 2, 3, 4 ],
10+
"y": [ 3, 5, 1, 7 ],
11+
"name": "You need to break this trace twice if first one has no break"
12+
},
13+
{
14+
"x": [ 1, 2, 3, 4 ],
15+
"y": [ 3, 5, 1, 7 ],
16+
"name": "You need to break this trace twice if first one has no break"
17+
},
18+
{
19+
"x": [ 1, 2, 3, 4 ],
20+
"y": [ 3, 5, 1, 7 ],
21+
"name": "You need to break this trace twice if first one has no break"
22+
},
23+
{
24+
"x": [ 1, 2, 3, 4 ],
25+
"y": [ 3, 5, 1, 7 ],
26+
"name": "You need to break this trace twice if first one has no break"
27+
},
28+
{
29+
"x": [ 1, 2, 3, 4 ],
30+
"y": [ 3, 5, 1, 7 ],
31+
"name": "You need to break this trace twice if first one has no break"
32+
},
33+
{
34+
"x": [ 1, 2, 3, 4 ],
35+
"y": [ 3, 5, 1, 7 ],
36+
"name": "This<br> contains<br>a break"
37+
}
38+
],
39+
"layout": {
40+
"legend": {
41+
"orientation": "h",
42+
"xanchor": "center",
43+
"yanchor": "top",
44+
"x": 0.5,
45+
"y": -0.1,
46+
"traceorder": "normal"
47+
},
48+
"paper_bgcolor": "rgba(255,0,0,0.5)",
49+
"margin": {
50+
"l": 20,
51+
"r": 20,
52+
"t": 20,
53+
"b": 20
54+
},
55+
"width": 360,
56+
"height": 600
57+
}
58+
}

0 commit comments

Comments
 (0)