Skip to content

improve legend positioning in edge cases #3024

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 9 additions & 6 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,9 @@ module.exports = function draw(gd) {

// Make sure the legend left and right sides are visible
var legendWidth = opts._width,
legendWidthMax = gs.w;
legendWidthMax = gs.w * Math.max(1 - opts.x, 1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This calculation makes sense for the (default) xanchor: 'left' - seems like there's something similar for 'right' like * Math.max(opts.x, 1), not sure what it should look like for 'center', and 'auto' should presumably depend on which alignment it auto-chose.

(and similarly with legendHeightMax and yanchor)


if(legendWidth > legendWidthMax) {
lx = gs.l;
legendWidth = legendWidthMax;
}
else {
Expand All @@ -186,11 +185,14 @@ module.exports = function draw(gd) {
// (legends with a scroll bar are not allowed to stretch beyond the extended
// margins)
var legendHeight = opts._height,
legendHeightMax = gs.h;
legendHeightMax = gs.h * Math.max(opts.y, 1);

var padY = 12;
if(legendHeight > legendHeightMax) {
ly = gs.t;
legendHeight = legendHeightMax;
if(!helpers.isVertical(opts)) {
ly += padY;
}
}
else {
if(ly + legendHeight > lyMax) ly = lyMax - legendHeight;
Expand Down Expand Up @@ -642,12 +644,13 @@ function computeLegendDimensions(gd, groups, traces) {
});

// check if legend fits in one row
oneRowLegend = fullLayout._size.w > borderwidth + fullTracesWidth - traceGap;
var legendWidthMax = fullLayout._size.w * Math.max(1 - opts.x, 1);
oneRowLegend = legendWidthMax > borderwidth + fullTracesWidth - traceGap;
traces.each(function(d) {
var legendItem = d[0],
traceWidth = oneRowLegend ? 40 + d[0].width : maxTraceWidth;

if((borderwidth + offsetX + traceGap + traceWidth) > fullLayout._size.w) {
if((borderwidth + offsetX + traceGap + traceWidth) > legendWidthMax) {
offsetX = 0;
rowHeight = rowHeight + maxTraceHeight;
opts._height = opts._height + maxTraceHeight;
Expand Down
13 changes: 11 additions & 2 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -1687,8 +1687,17 @@ plots.autoMargin = function(gd, id, o) {

// if the item is too big, just give it enough automargin to
// make sure you can still grab it and bring it back
if(o.l + o.r > fullLayout.width * 0.5) o.l = o.r = 0;
if(o.b + o.t > fullLayout.height * 0.5) o.b = o.t = 0;
var rescaleFactor;
if(o.l + o.r > (fullLayout.width * 0.5)) {
rescaleFactor = (fullLayout.width * 0.5) / (o.l + o.r);
o.l *= rescaleFactor;
o.r *= rescaleFactor;
}
if(o.b + o.t > (fullLayout.height * 0.5)) {
rescaleFactor = (fullLayout.height * 0.5) / (o.b + o.t);
o.b *= rescaleFactor;
o.t *= rescaleFactor;
}

var xl = o.xl !== undefined ? o.xl : o.x;
var xr = o.xr !== undefined ? o.xr : o.x;
Expand Down
Binary file added test/image/baselines/legend_negative_x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/image/baselines/legend_small_horizontal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/image/baselines/legend_small_vertical.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions test/image/mocks/legend_negative_x.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"data": [{
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8],
"y": [0, 3, 6, 4, 5, 2, 3, 5, 4],
"type": "scatter"
},
{
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8],
"y": [0, 4, 7, 8, 3, 6, 3, 3, 4],
"type": "scatter"
},
{
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8],
"y": [0, 4, 7, 8, 3, 6, 3, 3, 4],
"type": "scatter"
},
{
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8],
"y": [0, 4, 7, 8, 3, 6, 3, 3, 4],
"type": "scatter"
},
{
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8],
"y": [0, 4, 7, 8, 3, 6, 3, 3, 4],
"type": "scatter"
},
{
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8],
"y": [0, 4, 7, 8, 3, 6, 3, 3, 4],
"type": "scatter"
}
],
"layout": {
"showlegend": true,
"legend": {
"orientation": "h",
"traceorder": "reversed",
"x": -0.5,
"y": 1.2
},
"margin": {
"l": 125,
"r": 5,
"b": 30,
"t": 20,
"pad": 0
},
"height": 350,
"width": 700
}
}
104 changes: 104 additions & 0 deletions test/image/mocks/legend_small_horizontal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"layout": {
"legend": {
"orientation": "h",
"bordercolor": "#000000",
"borderwidth": 1,
"bgcolor": "#ffffff00"
},
"margin": {
"t": 25,
"b": 25,
"r": 25,
"l": 25
},
"width":500,
"height":200
},
"data": [
{
"x": [1, 2, 3, 4],
"y": [63.69, 62.55, 61.64, 61.39]
}, {
"x": [1, 2, 3, 4],
"y": [58.24, 54.93, 42.11, 50.75]
}, {
"x": [1, 2, 3, 4],
"y": [51.49, 49.59, 37.12, 31.45]
}, {
"x": [1, 2, 3, 4],
"y": [49.09, 58.54, 53.91, 43.12]
}, {
"x": [1, 2, 3, 4],
"y": [70.53, 72.51, 72.28, 78.65]
}, {
"x": [1, 2, 3, 4],
"y": [62.69, 59.09, 63.82, 62]
}, {
"x": [1, 2, 3, 4],
"y": [76.27, 71.43, 59.83, 64.34]
}, {
"x": [1, 2, 3, 4],
"y": [71.15, 81.82, 88.46, 74.29]
}, {
"x": [1, 2, 3, 4],
"y": [57.89, 57.38, 52.08, 63.83]
}, {
"x": [1, 2, 3, 4],
"y": [65.4, 63.27, 65.78, 64.03]
}, {
"x": [1, 2, 3, 4],
"y": [58.24, 54.93, 42.11, 50.75]
}, {
"x": [1, 2, 3, 4],
"y": [51.49, 49.59, 37.12, 31.45]
}, {
"x": [1, 2, 3, 4],
"y": [49.09, 58.54, 53.91, 43.12]
}, {
"x": [1, 2, 3, 4],
"y": [70.53, 72.51, 72.28, 78.65]
}, {
"x": [1, 2, 3, 4],
"y": [62.69, 59.09, 63.82, 62]
}, {
"x": [1, 2, 3, 4],
"y": [76.27, 71.43, 59.83, 64.34]
}, {
"x": [1, 2, 3, 4],
"y": [71.15, 81.82, 88.46, 74.29]
}, {
"x": [1, 2, 3, 4],
"y": [57.89, 57.38, 52.08, 63.83]
}, {
"x": [1, 2, 3, 4],
"y": [65.4, 63.27, 65.78, 64.03]
}, {
"x": [1, 2, 3, 4],
"y": [58.24, 54.93, 42.11, 50.75]
}, {
"x": [1, 2, 3, 4],
"y": [51.49, 49.59, 37.12, 31.45]
}, {
"x": [1, 2, 3, 4],
"y": [49.09, 58.54, 53.91, 43.12]
}, {
"x": [1, 2, 3, 4],
"y": [70.53, 72.51, 72.28, 78.65]
}, {
"x": [1, 2, 3, 4],
"y": [62.69, 59.09, 63.82, 62]
}, {
"x": [1, 2, 3, 4],
"y": [76.27, 71.43, 59.83, 64.34]
}, {
"x": [1, 2, 3, 4],
"y": [71.15, 81.82, 88.46, 74.29]
}, {
"x": [1, 2, 3, 4],
"y": [57.89, 57.38, 52.08, 63.83]
}, {
"x": [1, 2, 3, 4],
"y": [65.4, 63.27, 65.78, 64.03]
}]
}
104 changes: 104 additions & 0 deletions test/image/mocks/legend_small_vertical.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"layout": {
"legend": {
"orientation": "v",
"bordercolor": "#000000",
"borderwidth": 1,
"bgcolor": "#ffffff00"
},
"margin": {
"t": 25,
"b": 25,
"r": 25,
"l": 25
},
"width":200,
"height":500
},
"data": [
{
"x": [1, 2, 3, 4],
"y": [63.69, 62.55, 61.64, 61.39]
}, {
"x": [1, 2, 3, 4],
"y": [58.24, 54.93, 42.11, 50.75]
}, {
"x": [1, 2, 3, 4],
"y": [51.49, 49.59, 37.12, 31.45]
}, {
"x": [1, 2, 3, 4],
"y": [49.09, 58.54, 53.91, 43.12]
}, {
"x": [1, 2, 3, 4],
"y": [70.53, 72.51, 72.28, 78.65]
}, {
"x": [1, 2, 3, 4],
"y": [62.69, 59.09, 63.82, 62]
}, {
"x": [1, 2, 3, 4],
"y": [76.27, 71.43, 59.83, 64.34]
}, {
"x": [1, 2, 3, 4],
"y": [71.15, 81.82, 88.46, 74.29]
}, {
"x": [1, 2, 3, 4],
"y": [57.89, 57.38, 52.08, 63.83]
}, {
"x": [1, 2, 3, 4],
"y": [65.4, 63.27, 65.78, 64.03]
}, {
"x": [1, 2, 3, 4],
"y": [58.24, 54.93, 42.11, 50.75]
}, {
"x": [1, 2, 3, 4],
"y": [51.49, 49.59, 37.12, 31.45]
}, {
"x": [1, 2, 3, 4],
"y": [49.09, 58.54, 53.91, 43.12]
}, {
"x": [1, 2, 3, 4],
"y": [70.53, 72.51, 72.28, 78.65]
}, {
"x": [1, 2, 3, 4],
"y": [62.69, 59.09, 63.82, 62]
}, {
"x": [1, 2, 3, 4],
"y": [76.27, 71.43, 59.83, 64.34]
}, {
"x": [1, 2, 3, 4],
"y": [71.15, 81.82, 88.46, 74.29]
}, {
"x": [1, 2, 3, 4],
"y": [57.89, 57.38, 52.08, 63.83]
}, {
"x": [1, 2, 3, 4],
"y": [65.4, 63.27, 65.78, 64.03]
}, {
"x": [1, 2, 3, 4],
"y": [58.24, 54.93, 42.11, 50.75]
}, {
"x": [1, 2, 3, 4],
"y": [51.49, 49.59, 37.12, 31.45]
}, {
"x": [1, 2, 3, 4],
"y": [49.09, 58.54, 53.91, 43.12]
}, {
"x": [1, 2, 3, 4],
"y": [70.53, 72.51, 72.28, 78.65]
}, {
"x": [1, 2, 3, 4],
"y": [62.69, 59.09, 63.82, 62]
}, {
"x": [1, 2, 3, 4],
"y": [76.27, 71.43, 59.83, 64.34]
}, {
"x": [1, 2, 3, 4],
"y": [71.15, 81.82, 88.46, 74.29]
}, {
"x": [1, 2, 3, 4],
"y": [57.89, 57.38, 52.08, 63.83]
}, {
"x": [1, 2, 3, 4],
"y": [65.4, 63.27, 65.78, 64.03]
}]
}