Skip to content

Commit 275db1d

Browse files
committed
DRY up x/y anchor logic
1 parent b84a788 commit 275db1d

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

src/components/legend/draw.js

+16-25
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,8 @@ module.exports = function draw(gd) {
9898
var gs = fullLayout._size;
9999
var bw = opts.borderwidth;
100100

101-
var lx = gs.l + gs.w * opts.x;
102-
if(Lib.isRightAnchor(opts)) {
103-
lx -= opts._width;
104-
} else if(Lib.isCenterAnchor(opts)) {
105-
lx -= opts._width / 2;
106-
}
101+
var lx = gs.l + gs.w * opts.x - FROM_TL[getXanchor(opts)] * opts._width;
102+
var ly = gs.t + gs.h * (1 - opts.y) - FROM_TL[getYanchor(opts)] * opts._effHeight;
107103

108104

109105
// Make sure the legend left and right sides are visible
@@ -119,12 +115,6 @@ module.exports = function draw(gd) {
119115
legendWidth = Math.min(fullLayout.width - lx, opts._width);
120116
}
121117

122-
var ly = gs.t + gs.h * (1 - opts.y);
123-
if(Lib.isBottomAnchor(opts)) {
124-
ly -= opts._height;
125-
} else if(Lib.isMiddleAnchor(opts)) {
126-
ly -= opts._height / 2;
127-
}
128118

129119
// Make sure the legend top and bottom are visible
130120
// (legends with a scroll bar are not allowed to stretch beyond the extended
@@ -628,20 +618,9 @@ function computeLegendDimensions(gd, groups, traces) {
628618
function expandMargin(gd) {
629619
var fullLayout = gd._fullLayout;
630620
var opts = fullLayout.legend;
621+
var xanchor = getXanchor(opts);
622+
var yanchor = getYanchor(opts);
631623

632-
var xanchor = 'left';
633-
if(Lib.isRightAnchor(opts)) {
634-
xanchor = 'right';
635-
} else if(Lib.isCenterAnchor(opts)) {
636-
xanchor = 'center';
637-
}
638-
639-
var yanchor = 'top';
640-
if(Lib.isBottomAnchor(opts)) {
641-
yanchor = 'bottom';
642-
} else if(Lib.isMiddleAnchor(opts)) {
643-
yanchor = 'middle';
644-
}
645624

646625
Plots.autoMargin(gd, 'legend', {
647626
x: opts.x,
@@ -652,3 +631,15 @@ function expandMargin(gd) {
652631
t: opts._height * (FROM_TL[yanchor])
653632
});
654633
}
634+
635+
function getXanchor(opts) {
636+
return Lib.isRightAnchor(opts) ? 'right' :
637+
Lib.isCenterAnchor(opts) ? 'center' :
638+
'left';
639+
}
640+
641+
function getYanchor(opts) {
642+
return Lib.isBottomAnchor(opts) ? 'bottom' :
643+
Lib.isMiddleAnchor(opts) ? 'middle' :
644+
'top';
645+
}

0 commit comments

Comments
 (0)