Skip to content

Commit 6eba309

Browse files
Made shiftPosition clearer and more modular
1 parent 93890d0 commit 6eba309

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/components/annotations/draw.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,12 @@ function drawOne(gd, index) {
7878
// the plot.
7979
// axDomainRef: if true and axa defined, draws relative to axis domain,
8080
// otherwise draws relative to data (if axa defined) or paper (if not).
81-
function shiftPosition(axa, optAx, dAx, gsDim, vertical, axRef) {
81+
function shiftPosition(axa, options, dAx, axLetter, gs) {
82+
var optAx = options[axLetter];
83+
var axRef = options[axLetter + 'ref'];
84+
var vertical = axLetter.includes('y');
8285
var axDomainRef = Axes.getRefType(axRef) === 'domain';
86+
var gsDim = vertical ? gs.h : gs.w;
8387
if(axa) {
8488
if(axDomainRef) {
8589
// here optAx normalized to length of axis (e.g., normally in range
@@ -611,21 +615,19 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
611615
annTextGroupInner.call(Drawing.setTranslate, xcenter, ycenter);
612616

613617
modifyItem('x',
614-
shiftPosition(xa, options.x, dx, gs.w, false, options.xref));
618+
shiftPosition(xa, options, dx, 'x', gs));
615619
modifyItem('y',
616-
shiftPosition(ya, options.y, dy, gs.h, true, options.yref));
620+
shiftPosition(ya, options, dy, 'y', gs));
617621

618622
// for these 2 calls to shiftPosition, it is assumed xa, ya are
619623
// defined, so gsDim will not be used, but we put it in
620624
// anyways for consistency
621625
if(options.axref === options.xref) {
622-
modifyItem('ax', shiftPosition(xa, options.ax, dx, gs.w, false,
623-
options.xref));
626+
modifyItem('ax', shiftPosition(xa, options, dx, 'ax', gs));
624627
}
625628

626629
if(options.ayref === options.yref) {
627-
modifyItem('ay', shiftPosition(ya, options.ay, dy, gs.h, true,
628-
options.yref));
630+
modifyItem('ay', shiftPosition(ya, options, dy, 'ay', gs));
629631
}
630632

631633
arrowGroup.attr('transform', 'translate(' + dx + ',' + dy + ')');
@@ -664,15 +666,13 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
664666
// defined, so gsDim will not be used, but we put it in
665667
// anyways for consistency
666668
if(options.axref === options.xref) {
667-
modifyItem('ax', shiftPosition(xa, options.ax, dx, gs.h, false,
668-
options.xref));
669+
modifyItem('ax', shiftPosition(xa, options, dx, 'ax', gs));
669670
} else {
670671
modifyItem('ax', options.ax + dx);
671672
}
672673

673674
if(options.ayref === options.yref) {
674-
modifyItem('ay', shiftPosition(ya, options.ay, dy, gs.w, true,
675-
options.yref));
675+
modifyItem('ay', shiftPosition(ya, options, dy, 'ay', gs.w));
676676
} else {
677677
modifyItem('ay', options.ay + dy);
678678
}
@@ -683,8 +683,7 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
683683
if(xa) {
684684
// shiftPosition will not execute code where xa was
685685
// undefined, so we use to calculate xUpdate too
686-
xUpdate = shiftPosition(xa, options.x, dx, gs.h, false,
687-
options.xref);
686+
xUpdate = shiftPosition(xa, options, dx, 'x', gs);
688687
} else {
689688
var widthFraction = options._xsize / gs.w;
690689
var xLeft = options.x + (options._xshift - options.xshift) / gs.w - widthFraction / 2;
@@ -696,8 +695,7 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
696695
if(ya) {
697696
// shiftPosition will not execute code where ya was
698697
// undefined, so we use to calculate yUpdate too
699-
yUpdate = shiftPosition(ya, options.y, dy, gs.w, true,
700-
options.yref);
698+
yUpdate = shiftPosition(ya, options, dy, 'y', gs);
701699
} else {
702700
var heightFraction = options._ysize / gs.h;
703701
var yBottom = options.y - (options._yshift + options.yshift) / gs.h - heightFraction / 2;

0 commit comments

Comments
 (0)