Skip to content

Commit b89f1ee

Browse files
committed
Fix #1913 by adding pushing overflowed legend items to a new line whilst respecting grouping
1 parent 7e68a2e commit b89f1ee

File tree

2 files changed

+104
-9
lines changed

2 files changed

+104
-9
lines changed

src/components/legend/draw.js

+47-9
Original file line numberDiff line numberDiff line change
@@ -586,23 +586,60 @@ function computeLegendDimensions(gd, groups, traces) {
586586
extraWidth = 40;
587587
}
588588
else if(isGrouped) {
589-
var groupXOffsets = [opts._width];
589+
var maxHeight = 0;
590+
var maxWidth = 0;
590591
var groupData = groups.data();
591592

592-
for(var i = 0, n = groupData.length; i < n; i++) {
593-
var textWidths = groupData[i].map(function(legendItemArray) {
593+
var maxItems = 0;
594+
595+
groupData.forEach(function(group) {
596+
597+
598+
var groupWidths = group.map(function(legendItemArray) {
594599
return legendItemArray[0].width;
595600
});
596601

597-
var groupWidth = 40 + Math.max.apply(null, textWidths);
602+
var groupWidth = Math.max.apply(null, groupWidths);
603+
var groupHeight = group.reduce(function(a, b) {
604+
return a + b[0].height;
605+
}, 0);
606+
607+
maxWidth = Math.max(maxWidth, groupWidth);
608+
maxHeight = Math.max(maxHeight, groupHeight);
609+
maxItems = Math.max(maxItems, group.length);
610+
});
611+
612+
var traceGroupGap = opts.tracegroupgap || 5;
613+
614+
maxHeight += traceGroupGap;
615+
616+
var groupXOffsets = [opts._width];
617+
var rowHeights = [];
618+
var rowNum = 1;
619+
for(var i = 0, n = groupData.length; i < n; i++) {
620+
opts._width += traceGroupGap + maxWidth;
621+
622+
if(fullLayout._size.w < (borderwidth + opts._width)) {
623+
groupXOffsets[groupXOffsets.length - 1] = 0;
624+
opts._width = maxWidth + traceGroupGap;
625+
rowNum++;
626+
}
627+
628+
var currRowHeight = ((rowNum - 1) * maxHeight);
598629

599-
opts._width += opts.tracegroupgap + groupWidth;
630+
var length = groupData[i].length;
631+
var y = ((1 - (length / maxItems)) * maxHeight);
632+
currRowHeight = currRowHeight > (rowNum - 1 * maxHeight) ? (rowNum - 1) * maxHeight : y;
633+
634+
rowHeights.push(currRowHeight);
635+
636+
opts._width += traceGroupGap + maxWidth;
600637

601638
groupXOffsets.push(opts._width);
602639
}
603640

604641
groups.each(function(d, i) {
605-
Drawing.setTranslate(this, groupXOffsets[i], 0);
642+
Drawing.setTranslate(this, groupXOffsets[i], rowHeights[i]);
606643
});
607644

608645
groups.each(function() {
@@ -620,11 +657,12 @@ function computeLegendDimensions(gd, groups, traces) {
620657

621658
groupHeight += textHeight;
622659
});
623-
624-
opts._height = Math.max(opts._height, groupHeight);
625660
});
626661

627-
opts._height += 10 + borderwidth * 2;
662+
opts._height += rowNum * maxHeight;
663+
664+
var totalRows = Math.ceil(groupData.length / rowNum);
665+
opts._width = (totalRows * (40 + maxWidth + traceGroupGap)) + 40;
628666
opts._width += borderwidth * 2;
629667
}
630668
else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"data": [{
3+
"x": [1, 2, 3, 4],
4+
"y": [63.69, 62.55, 61.64, 61.39],
5+
"legendgroup": 1,
6+
"name": "Trace A-1"
7+
}, {
8+
"x": [1, 2, 3, 4],
9+
"y": [58.24, 54.93, 42.11, 50.75],
10+
"legendgroup": 1,
11+
"name":"Trace A-2"
12+
}, {
13+
"x": [1, 2, 3, 4],
14+
"y": [51.49, 49.59, 37.12, 31.45],
15+
"legendgroup":2,
16+
"name":"Trace B-1"
17+
}, {
18+
"x": [1, 2, 3, 4],
19+
"y": [49.09, 58.54, 53.91, 43.12],
20+
"legendgroup":2,
21+
"name":"Trace B-2"
22+
}, {
23+
"x": [1, 2, 3, 4],
24+
"y": [70.53, 72.51, 72.28, 78.65],
25+
"name":"Trace C-1"
26+
}, {
27+
"x": [1, 2, 3, 4],
28+
"y": [62.69, 59.09, 63.82, 62],
29+
"legendgroup":3,
30+
"name":"Trace D-1"
31+
}, {
32+
"x": [1, 2, 3, 4],
33+
"y": [76.27, 71.43, 59.83, 64.34],
34+
"legendgroup":3,
35+
"name":"Trace D-2"
36+
}, {
37+
"x": [1, 2, 3, 4],
38+
"y": [71.15, 81.82, 88.46, 74.29],
39+
"name":"Trace E-1"
40+
}],
41+
"layout": {
42+
"width": 600,
43+
"legend": {
44+
"orientation": "h"
45+
},
46+
"xaxis": {
47+
"type": "linear",
48+
"range": [0.7840236686390533, 4.215976331360947],
49+
"autorange": true
50+
},
51+
"yaxis": {
52+
"type": "linear",
53+
"range": [27.274108280254776, 92.63589171974522],
54+
"autorange": true
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)