Skip to content

Fix wrapped horizontal legends height computations #3446

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 1 commit into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
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
16 changes: 10 additions & 6 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,6 @@ function computeLegendDimensions(gd, groups, traces) {
var offsetX = 0;
var fullTracesWidth = 0;
var traceGap = opts.tracegroupgap || 5;
var oneRowLegend;

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

// check if legend fits in one row
oneRowLegend = fullLayout._size.w > borderwidth + fullTracesWidth - traceGap;
var oneRowLegend = fullLayout._size.w > borderwidth + fullTracesWidth - traceGap;

traces.each(function(d) {
var legendItem = d[0];
var traceWidth = oneRowLegend ? 40 + d[0].width : maxTraceWidth;

if((borderwidth + offsetX + traceGap + traceWidth) > fullLayout._size.w) {
offsetX = 0;
rowHeight = rowHeight + maxTraceHeight;
opts._height = opts._height + maxTraceHeight;
rowHeight += maxTraceHeight;
opts._height += maxTraceHeight;
// reset for next row
maxTraceHeight = 0;
}
Expand All @@ -657,16 +657,20 @@ function computeLegendDimensions(gd, groups, traces) {
(5 + borderwidth + legendItem.height / 2) + rowHeight);

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

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

if(oneRowLegend) {
opts._height = maxTraceHeight;
} else {
opts._height += maxTraceHeight;
}

opts._width += borderwidth * 2;
opts._height += 10 + borderwidth * 2;

}

// make sure we're only getting full pixels
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions test/image/mocks/legend_horizontal_wrap-alll-lines.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"data": [
{
"x": [ 1, 2, 3, 4 ],
"y": [ 3, 5, 1, 7 ],
"name": "Break this and last<br>trace will display properly"
},
{
"x": [ 1, 2, 3, 4 ],
"y": [ 3, 5, 1, 7 ],
"name": "You need to break this trace twice if first one has no break"
},
{
"x": [ 1, 2, 3, 4 ],
"y": [ 3, 5, 1, 7 ],
"name": "You need to break this trace twice if first one has no break"
},
{
"x": [ 1, 2, 3, 4 ],
"y": [ 3, 5, 1, 7 ],
"name": "You need to break this trace twice if first one has no break"
},
{
"x": [ 1, 2, 3, 4 ],
"y": [ 3, 5, 1, 7 ],
"name": "You need to break this trace twice if first one has no break"
},
{
"x": [ 1, 2, 3, 4 ],
"y": [ 3, 5, 1, 7 ],
"name": "You need to break this trace twice if first one has no break"
},
{
"x": [ 1, 2, 3, 4 ],
"y": [ 3, 5, 1, 7 ],
"name": "This<br> contains<br>a break"
}
],
"layout": {
"legend": {
"orientation": "h",
"xanchor": "center",
"yanchor": "top",
"x": 0.5,
"y": -0.1,
"traceorder": "normal"
},
"paper_bgcolor": "rgba(255,0,0,0.5)",
"margin": {
"l": 20,
"r": 20,
"t": 20,
"b": 20
},
"width": 360,
"height": 600
}
}