Skip to content

Commit b324f98

Browse files
committed
fix #4132 - fix grouped horizontal legend dimension computations
- which gives better margin pushes, fixes legendgroup_horizontal_wrapping mock
1 parent 208f202 commit b324f98

File tree

4 files changed

+28
-40
lines changed

4 files changed

+28
-40
lines changed

src/components/legend/draw.js

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -550,55 +550,43 @@ function computeLegendDimensions(gd, groups, traces) {
550550
combinedItemWidth += w;
551551
});
552552

553+
var maxRowWidth = 0;
554+
553555
if(isGrouped) {
554-
var groupData = groups.data();
555-
var i;
556+
var maxGroupHeightInRow = 0;
557+
var groupOffsetX = 0;
558+
var groupOffsetY = 0;
559+
groups.each(function() {
560+
var maxWidthInGroup = 0;
561+
var offsetY = 0;
562+
d3.select(this).selectAll('g.traces').each(function(d) {
563+
var h = d[0].height;
564+
Drawing.setTranslate(this, 0, itemGap + bw + h / 2 + offsetY);
565+
offsetY += h;
566+
maxWidthInGroup = Math.max(maxWidthInGroup, textGap + d[0].width);
567+
});
568+
maxGroupHeightInRow = Math.max(maxGroupHeightInRow, offsetY);
556569

557-
var maxGroupHeight = 0;
558-
for(i = 0; i < groupData.length; i++) {
559-
var groupHeight = groupData[i].reduce(function(a, b) { return a + b[0].height; }, 0);
560-
maxGroupHeight = Math.max(maxGroupHeight, groupHeight);
561-
}
570+
var next = maxWidthInGroup + itemGap;
562571

563-
var groupXOffsets = [opts._width];
564-
var groupYOffsets = [];
565-
var rowNum = 0;
566-
for(i = 0; i < groupData.length; i++) {
567-
if((opts._width + itemGap + maxItemWidth + bw) > opts._maxWidth) {
568-
groupXOffsets[groupXOffsets.length - 1] = groupXOffsets[0];
569-
opts._width = maxItemWidth + itemGap;
570-
rowNum++;
571-
} else {
572-
opts._width += maxItemWidth + itemGap;
572+
if((next + bw + groupOffsetX) > opts._maxWidth) {
573+
maxRowWidth = Math.max(maxRowWidth, groupOffsetX);
574+
groupOffsetX = 0;
575+
groupOffsetY += maxGroupHeightInRow + opts.tracegroupgap;
576+
maxGroupHeightInRow = offsetY;
573577
}
574578

575-
groupXOffsets.push(opts._width);
576-
groupYOffsets.push(rowNum * maxGroupHeight + (rowNum > 0 ? opts.tracegroupgap : 0));
577-
}
578-
579-
groups.each(function(d, i) {
580-
Drawing.setTranslate(this, groupXOffsets[i], groupYOffsets[i]);
581-
});
579+
Drawing.setTranslate(this, groupOffsetX, groupOffsetY);
582580

583-
groups.each(function() {
584-
var group = d3.select(this);
585-
var groupTraces = group.selectAll('g.traces');
586-
var groupHeight = 0;
587-
588-
groupTraces.each(function(d) {
589-
var h = d[0].height;
590-
Drawing.setTranslate(this, 0, itemGap + bw + groupHeight + h / 2);
591-
groupHeight += h;
592-
});
581+
groupOffsetX += next;
593582
});
594583

595-
opts._height = groupYOffsets[groupYOffsets.length - 1] + maxGroupHeight + endPad;
596-
opts._width = Math.max.apply(null, groupXOffsets) + maxItemWidth + textGap + bw2;
597584
toggleRectWidth = maxItemWidth;
585+
opts._width = Math.max(maxRowWidth, groupOffsetX) + bw;
586+
opts._height = groupOffsetY + maxGroupHeightInRow + endPad;
598587
} else {
599588
var oneRowLegend = (combinedItemWidth + bw2 + (traces.size() - 1) * itemGap) < opts._maxWidth;
600589

601-
var maxRowWidth = 0;
602590
var maxItemHeightInRow = 0;
603591
var offsetX = 0;
604592
var offsetY = 0;
Loading
Loading

test/jasmine/tests/legend_test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,15 +719,15 @@ describe('legend relayout update', function() {
719719
var mockCopy = Lib.extendDeep({}, mock);
720720
Plotly.newPlot(gd, mockCopy)
721721
.then(function() {
722-
expect(gd._fullLayout._size.b).toBe(130);
722+
expect(gd._fullLayout._size.b).toBe(113);
723723
return Plotly.relayout(gd, 'legend.tracegroupgap', 70);
724724
})
725725
.then(function() {
726-
expect(gd._fullLayout._size.b).toBe(185);
726+
expect(gd._fullLayout._size.b).toBe(167);
727727
return Plotly.relayout(gd, 'legend.tracegroupgap', 10);
728728
})
729729
.then(function() {
730-
expect(gd._fullLayout._size.b).toBe(130);
730+
expect(gd._fullLayout._size.b).toBe(113);
731731
})
732732
.catch(failTest)
733733
.then(done);

0 commit comments

Comments
 (0)