Skip to content

Commit b84a788

Browse files
committed
trim toggle-rect to more-precise width
.. the current width didn't lead to any bugs, but it was too large in most cases.
1 parent 40ba2ea commit b84a788

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

src/components/legend/draw.js

+16-21
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ function computeTextDimensions(g, gd) {
487487
function computeLegendDimensions(gd, groups, traces) {
488488
var fullLayout = gd._fullLayout;
489489
var opts = fullLayout.legend;
490+
var isVertical = helpers.isVertical(opts);
490491
var isGrouped = helpers.isGrouped(opts);
491492

492493
var bw = opts.borderwidth;
@@ -497,32 +498,28 @@ function computeLegendDimensions(gd, groups, traces) {
497498

498499
opts._maxWidth = fullLayout._size.w;
499500

500-
var extraWidth = 0;
501+
var toggleRectWidth = 0;
501502
opts._width = 0;
502503
opts._height = 0;
503504

504-
if(helpers.isVertical(opts)) {
505-
if(isGrouped) {
506-
groups.each(function(d, i) {
507-
Drawing.setTranslate(this, 0, i * opts.tracegroupgap);
508-
});
509-
}
510-
505+
if(isVertical) {
511506
traces.each(function(d) {
512507
var h = d[0].height;
513508
Drawing.setTranslate(this, bw, itemGap + bw + opts._height + h / 2);
514509
opts._height += h;
515510
opts._width = Math.max(opts._width, d[0].width);
516511
});
517512

513+
toggleRectWidth = textGap + opts._width;
518514
opts._width += itemGap + textGap + bw2;
519515
opts._height += endPad;
520516

521517
if(isGrouped) {
518+
groups.each(function(d, i) {
519+
Drawing.setTranslate(this, 0, i * opts.tracegroupgap);
520+
});
522521
opts._height += (opts._lgroupsLength - 1) * opts.tracegroupgap;
523522
}
524-
525-
extraWidth = textGap;
526523
} else {
527524
var maxItemWidth = 0;
528525
var combinedItemWidth = 0;
@@ -576,6 +573,7 @@ function computeLegendDimensions(gd, groups, traces) {
576573

577574
opts._height = groupYOffsets[groupYOffsets.length - 1] + maxGroupHeight + endPad;
578575
opts._width = Math.max.apply(null, groupXOffsets) + maxItemWidth + textGap + bw2;
576+
toggleRectWidth = maxItemWidth;
579577
} else {
580578
var oneRowLegend = (combinedItemWidth + bw2 + (traces.size() - 1) * itemGap) < opts._maxWidth;
581579

@@ -604,29 +602,26 @@ function computeLegendDimensions(gd, groups, traces) {
604602
if(oneRowLegend) {
605603
opts._width = offsetX + bw2;
606604
opts._height = maxItemHeightInRow + endPad;
605+
toggleRectWidth = null;
607606
} else {
608607
opts._width = Math.max(maxRowWidth, offsetX) + bw;
609608
opts._height += maxItemHeightInRow + endPad;
609+
toggleRectWidth = maxItemWidth;
610610
}
611611
}
612612
}
613613

614614
opts._width = Math.ceil(opts._width);
615615
opts._height = Math.ceil(opts._height);
616616

617-
var isEditable = (
618-
gd._context.edits.legendText ||
619-
gd._context.edits.legendPosition
620-
);
621-
617+
var edits = gd._context.edits;
618+
var isEditable = edits.legendText || edits.legendPosition;
622619
traces.each(function(d) {
620+
var traceToggle = d3.select(this).select('.legendtoggle');
623621
var h = d[0].height;
624-
Drawing.setRect(d3.select(this).select('.legendtoggle'),
625-
0,
626-
-h / 2,
627-
(isEditable ? 0 : opts._width) + extraWidth,
628-
h
629-
);
622+
var w = isEditable ? textGap : (toggleRectWidth || (textGap + d[0].width));
623+
if(!isVertical) w += itemGap / 2;
624+
Drawing.setRect(traceToggle, 0, -h / 2, w, h);
630625
});
631626
}
632627

test/jasmine/tests/legend_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ describe('legend restyle update', function() {
788788
expect(node.attr('height')).toEqual('19');
789789

790790
var w = +node.attr('width');
791-
expect(Math.abs(w - 160)).toBeLessThan(10);
791+
expect(w).toBeWithin(113, 10);
792792
});
793793
}
794794

0 commit comments

Comments
 (0)