Skip to content

Commit 5388d32

Browse files
authored
Merge pull request #2149 from apalchys/legend_entries_width
Fix legend items aligning
2 parents 2656167 + 3254a20 commit 5388d32

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/components/legend/draw.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -608,17 +608,22 @@ function computeLegendDimensions(gd, groups, traces) {
608608
var rowHeight = 0,
609609
maxTraceHeight = 0,
610610
maxTraceWidth = 0,
611-
offsetX = 0;
611+
offsetX = 0,
612+
fullTracesWidth = 0,
613+
traceGap = opts.tracegroupgap || 5,
614+
oneRowLegend;
612615

613616
// calculate largest width for traces and use for width of all legend items
614617
traces.each(function(d) {
615618
maxTraceWidth = Math.max(40 + d[0].width, maxTraceWidth);
619+
fullTracesWidth += 40 + d[0].width + traceGap;
616620
});
617621

622+
// check if legend fits in one row
623+
oneRowLegend = (fullLayout.width - (fullLayout.margin.r + fullLayout.margin.l)) > borderwidth + fullTracesWidth - traceGap;
618624
traces.each(function(d) {
619625
var legendItem = d[0],
620-
traceWidth = maxTraceWidth,
621-
traceGap = opts.tracegroupgap || 5;
626+
traceWidth = oneRowLegend ? 40 + d[0].width : maxTraceWidth;
622627

623628
if((borderwidth + offsetX + traceGap + traceWidth) > (fullLayout.width - (fullLayout.margin.r + fullLayout.margin.l))) {
624629
offsetX = 0;
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"data": [
3+
{
4+
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8],
5+
"y": [0, 3, 6, 4, 5, 2, 3, 5, 4],
6+
"type": "scatter",
7+
"name": "title"
8+
},
9+
{
10+
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8],
11+
"y": [0, 4, 7, 8, 3, 6, 3, 3, 4],
12+
"type": "scatter",
13+
"name": "this is a really long legend title"
14+
},
15+
{
16+
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8],
17+
"y": [2, 6, 5, 9, 2, 4, 6, 3, 7],
18+
"type": "scatter",
19+
"name": "this is a title"
20+
}
21+
],
22+
"layout": {
23+
"title": "Average Distribution per Month",
24+
"legend": { "orientation": "h" }
25+
}
26+
}

0 commit comments

Comments
 (0)