Skip to content

Fix #1913 by pushing overflowed legend items to a new line #3628

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 14 commits into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
58 changes: 48 additions & 10 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,23 +586,60 @@ function computeLegendDimensions(gd, groups, traces) {
extraWidth = 40;
}
else if(isGrouped) {
var groupXOffsets = [opts._width];
var maxHeight = 0;
var maxWidth = 0;
var groupData = groups.data();

for(var i = 0, n = groupData.length; i < n; i++) {
var textWidths = groupData[i].map(function(legendItemArray) {
var maxItems = 0;

groupData.forEach(function(group) {
Copy link
Contributor

Choose a reason for hiding this comment

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

NON-BLOCKING

We could probably clean up these few lines a little:

  • use for-loop here instead of forEach
  • ⚡ that double empty new line
  • use Lib.aggNums instead of Math.max.apply and group.reduce
  • make the for(var i = 0, n = groupData.length; i < n; i++) loops for(var i = 0; i < groupData.length; i++), i.e. no need to define n (I think).

Copy link
Contributor

Choose a reason for hiding this comment

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

Done in a5fa247 except for group.reduce.

Can I really replace group.reduce with Lib.aggNums?

Copy link
Contributor

Choose a reason for hiding this comment

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

Can I really replace group.reduce with Lib.aggNums?

Maybe with something like:

Lib.aggNums(function(a, b) { return a + b[0].height; }, 0, groups)

similar to Lib.mean, but you're right, that's probably an overkill here as all the b[0].height should be defined. 💃



var groupWidths = group.map(function(legendItemArray) {
return legendItemArray[0].width;
});

var groupWidth = 40 + Math.max.apply(null, textWidths);
var groupWidth = Math.max.apply(null, groupWidths);
var groupHeight = group.reduce(function(a, b) {
return a + b[0].height;
}, 0);

opts._width += opts.tracegroupgap + groupWidth;
maxWidth = Math.max(maxWidth, groupWidth);
maxHeight = Math.max(maxHeight, groupHeight);
maxItems = Math.max(maxItems, group.length);
});

maxHeight += opts.tracegroupgap;
var traceGroupGap = 5;
maxWidth += traceGroupGap;

var groupXOffsets = [opts._width];
var rowHeights = [];
var rowNum = 1;
for(var i = 0, n = groupData.length; i < n; i++) {
opts._width += maxWidth ;

if(fullLayout._size.w < (borderwidth + opts._width + maxWidth)) {
groupXOffsets[groupXOffsets.length - 1] = groupXOffsets[0];
opts._width = maxWidth;
rowNum++;
}

var currRowHeight = ((rowNum - 1) * maxHeight);

var length = groupData[i].length;
var y = ((1 - (length / maxItems)) * maxHeight);
currRowHeight = currRowHeight > (rowNum - 1 * maxHeight) ? (rowNum - 1) * maxHeight : y;

rowHeights.push(currRowHeight);

opts._width += maxWidth;

groupXOffsets.push(opts._width);
}

groups.each(function(d, i) {
Drawing.setTranslate(this, groupXOffsets[i], 0);
Drawing.setTranslate(this, groupXOffsets[i], rowHeights[i]);
});

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

groupHeight += textHeight;
});

opts._height = Math.max(opts._height, groupHeight);
});

opts._height += 10 + borderwidth * 2;
opts._height += (borderwidth * 2) + (rowNum * maxHeight);

var maxOffset = Math.max.apply(null, groupXOffsets);
opts._width = maxOffset + maxWidth + 40;
opts._width += borderwidth * 2;
}
else {
Expand All @@ -633,7 +671,7 @@ function computeLegendDimensions(gd, groups, traces) {
var maxTraceWidth = 0;
var offsetX = 0;
var fullTracesWidth = 0;
var traceGap = opts.tracegroupgap || 5;
var traceGap = 5;

// calculate largest width for traces and use for width of all legend items
traces.each(function(d) {
Expand Down
Binary file modified test/image/baselines/legend_horizontal_groups.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions test/image/mocks/legendgroup_horizontal_wrapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
Copy link
Contributor

@etpinard etpinard Mar 18, 2019

Choose a reason for hiding this comment

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

For comparison, this mock looks like:

image

on master.

"data": [{
"x": [1, 2, 3, 4],
"y": [63.69, 62.55, 61.64, 61.39],
"legendgroup": 1,
"name": "Trace A-1"
}, {
"x": [1, 2, 3, 4],
"y": [58.24, 54.93, 42.11, 50.75],
"legendgroup": 1,
"name":"Trace A-2"
}, {
"x": [1, 2, 3, 4],
"y": [51.49, 49.59, 37.12, 31.45],
"legendgroup":2,
"name":"Trace B-1"
}, {
"x": [1, 2, 3, 4],
"y": [49.09, 58.54, 53.91, 43.12],
"legendgroup":2,
"name":"Trace B-2"
}, {
"x": [1, 2, 3, 4],
"y": [70.53, 72.51, 72.28, 78.65],
"name":"Trace C-1"
}, {
"x": [1, 2, 3, 4],
"y": [62.69, 59.09, 63.82, 62],
"legendgroup":3,
"name":"Trace D-1"
}, {
"x": [1, 2, 3, 4],
"y": [76.27, 71.43, 59.83, 64.34],
"legendgroup":3,
"name":"Trace D-2"
}, {
"x": [1, 2, 3, 4],
"y": [71.15, 81.82, 88.46, 74.29],
"name":"Trace E-1"
}],
"layout": {
"width": 600,
"legend": {
"orientation": "h"
},
"xaxis": {
"type": "linear",
"range": [0.7840236686390533, 4.215976331360947],
"autorange": true
},
"yaxis": {
"type": "linear",
"range": [27.274108280254776, 92.63589171974522],
"autorange": true
}
}
}