Skip to content

Commit 9d4b191

Browse files
committed
DRY-up computeLegendDimensions
1 parent 5500120 commit 9d4b191

File tree

1 file changed

+25
-57
lines changed

1 file changed

+25
-57
lines changed

src/components/legend/draw.js

Lines changed: 25 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -597,10 +597,15 @@ function computeTextDimensions(g, gd) {
597597
}
598598

599599
function computeLegendDimensions(gd, groups, traces) {
600-
var fullLayout = gd._fullLayout,
601-
opts = fullLayout.legend,
602-
borderwidth = opts.borderwidth,
603-
isGrouped = helpers.isGrouped(opts);
600+
var fullLayout = gd._fullLayout;
601+
var opts = fullLayout.legend;
602+
var borderwidth = opts.borderwidth;
603+
var isGrouped = helpers.isGrouped(opts);
604+
605+
var extraWidth = 0;
606+
607+
opts.width = 0;
608+
opts.height = 0;
604609

605610
if(helpers.isVertical(opts)) {
606611
if(isGrouped) {
@@ -609,9 +614,6 @@ function computeLegendDimensions(gd, groups, traces) {
609614
});
610615
}
611616

612-
opts.width = 0;
613-
opts.height = 0;
614-
615617
traces.each(function(d) {
616618
var legendItem = d[0],
617619
textHeight = legendItem.height,
@@ -632,26 +634,9 @@ function computeLegendDimensions(gd, groups, traces) {
632634
opts.height += (opts._lgroupsLength - 1) * opts.tracegroupgap;
633635
}
634636

635-
// make sure we're only getting full pixels
636-
opts.width = Math.ceil(opts.width);
637-
opts.height = Math.ceil(opts.height);
638-
639-
traces.each(function(d) {
640-
var legendItem = d[0],
641-
bg = d3.select(this).select('.legendtoggle');
642-
643-
bg.call(Drawing.setRect,
644-
0,
645-
-legendItem.height / 2,
646-
(gd._context.editable ? 0 : opts.width) + 40,
647-
legendItem.height
648-
);
649-
});
637+
extraWidth = 40;
650638
}
651639
else if(isGrouped) {
652-
opts.width = 0;
653-
opts.height = 0;
654-
655640
var groupXOffsets = [opts.width],
656641
groupData = groups.data();
657642

@@ -692,26 +677,8 @@ function computeLegendDimensions(gd, groups, traces) {
692677

693678
opts.height += 10 + borderwidth * 2;
694679
opts.width += borderwidth * 2;
695-
696-
// make sure we're only getting full pixels
697-
opts.width = Math.ceil(opts.width);
698-
opts.height = Math.ceil(opts.height);
699-
700-
traces.each(function(d) {
701-
var legendItem = d[0],
702-
bg = d3.select(this).select('.legendtoggle');
703-
704-
bg.call(Drawing.setRect,
705-
0,
706-
-legendItem.height / 2,
707-
(gd._context.editable ? 0 : opts.width),
708-
legendItem.height
709-
);
710-
});
711680
}
712681
else {
713-
opts.width = 0;
714-
opts.height = 0;
715682
var rowHeight = 0,
716683
maxTraceHeight = 0,
717684
maxTraceWidth = 0,
@@ -750,22 +717,23 @@ function computeLegendDimensions(gd, groups, traces) {
750717
opts.width += borderwidth * 2;
751718
opts.height += 10 + borderwidth * 2;
752719

753-
// make sure we're only getting full pixels
754-
opts.width = Math.ceil(opts.width);
755-
opts.height = Math.ceil(opts.height);
720+
}
756721

757-
traces.each(function(d) {
758-
var legendItem = d[0],
759-
bg = d3.select(this).select('.legendtoggle');
722+
// make sure we're only getting full pixels
723+
opts.width = Math.ceil(opts.width);
724+
opts.height = Math.ceil(opts.height);
760725

761-
bg.call(Drawing.setRect,
762-
0,
763-
-legendItem.height / 2,
764-
(gd._context.editable ? 0 : opts.width),
765-
legendItem.height
766-
);
767-
});
768-
}
726+
traces.each(function(d) {
727+
var legendItem = d[0],
728+
bg = d3.select(this).select('.legendtoggle');
729+
730+
bg.call(Drawing.setRect,
731+
0,
732+
-legendItem.height / 2,
733+
(gd._context.editable ? 0 : opts.width) + extraWidth,
734+
legendItem.height
735+
);
736+
});
769737
}
770738

771739
function expandMargin(gd) {

0 commit comments

Comments
 (0)