Skip to content

Commit 143328d

Browse files
MFedMFed
MFed
authored and
MFed
committed
Not hardcoding axis references but using regex and coerce. Purposeful duplication of code per github PR 610
1 parent d07b676 commit 143328d

File tree

3 files changed

+55
-21
lines changed

3 files changed

+55
-21
lines changed

src/components/annotations/attributes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ module.exports = {
160160
dflt: 'pixel',
161161
values: [
162162
'pixel',
163-
'x'
163+
cartesianConstants.idRegex.x.toString()
164164
],
165165
role: 'info',
166166
description: [
@@ -177,7 +177,7 @@ module.exports = {
177177
dflt: 'pixel',
178178
values: [
179179
'pixel',
180-
'y'
180+
cartesianConstants.idRegex.y.toString()
181181
],
182182
role: 'info',
183183
description: [

src/components/annotations/index.js

+33-19
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ function handleAnnotationDefaults(annIn, fullLayout) {
7878
// xref, yref
7979
var axRef = Axes.coerceRef(annIn, annOut, tdMock, axLetter);
8080

81+
//todo: should be refactored in conjunction with Axes
82+
// axref, ayref
83+
var aaxRef = Axes.coerceARef(annIn, annOut, tdMock, axLetter);
84+
8185
// x, y
8286
var defaultPosition = 0.5;
8387
if(axRef !== 'paper') {
@@ -92,7 +96,7 @@ function handleAnnotationDefaults(annIn, fullLayout) {
9296
newval = Lib.dateTime2ms(annIn[axLetter]);
9397
if(newval !== false) annIn[axLetter] = newval;
9498

95-
if(annIn['a' + axLetter + 'ref'] === axRef) {
99+
if(aaxRef === axRef) {
96100
var newvalB = Lib.dateTime2ms(annIn['a' + axLetter]);
97101
if(newvalB !== false) annIn['a' + axLetter] = newvalB;
98102
}
@@ -487,15 +491,17 @@ annotations.draw = function(gd, index, opt, value) {
487491
// make sure the arrowhead (if there is one)
488492
// and the annotation center are visible
489493
if(options.showarrow) {
490-
if(options.axref === options.xref)
494+
if(options.axref === options.xref) {
491495
arrowX = Lib.constrain(annPosPx.x, 1, fullLayout.width - 1);
492-
else
496+
} else {
493497
arrowX = Lib.constrain(annPosPx.x - options.ax, 1, fullLayout.width - 1);
498+
}
494499

495-
if(options.ayref === options.yref)
500+
if(options.ayref === options.yref) {
496501
arrowY = Lib.constrain(annPosPx.y, 1, fullLayout.height - 1);
497-
else
502+
} else {
498503
arrowY = Lib.constrain(annPosPx.y - options.ay, 1, fullLayout.height - 1);
504+
}
499505
}
500506
annPosPx.x = Lib.constrain(annPosPx.x, 1, fullLayout.width - 1);
501507
annPosPx.y = Lib.constrain(annPosPx.y, 1, fullLayout.height - 1);
@@ -515,15 +521,17 @@ annotations.draw = function(gd, index, opt, value) {
515521
outerwidth - borderwidth, outerheight - borderwidth);
516522

517523
var annX = 0, annY = 0;
518-
if(options.axref === options.xref)
524+
if(options.axref === options.xref) {
519525
annX = Math.round(annPosPx.aax - outerwidth / 2);
520-
else
526+
} else {
521527
annX = Math.round(annPosPx.x - outerwidth / 2);
528+
}
522529

523-
if(options.ayref === options.yref)
530+
if(options.ayref === options.yref) {
524531
annY = Math.round(annPosPx.aay - outerheight / 2);
525-
else
532+
} else {
526533
annY = Math.round(annPosPx.y - outerheight / 2);
534+
}
527535

528536
ann.call(Lib.setTranslate, annX, annY);
529537

@@ -544,15 +552,17 @@ annotations.draw = function(gd, index, opt, value) {
544552
// how-to-get-the-width-of-an-svg-tspan-element
545553
var arrowX0, arrowY0;
546554

547-
if(options.axref === options.xref)
555+
if(options.axref === options.xref) {
548556
arrowX0 = annPosPx.aax + dx;
549-
else
557+
} else {
550558
arrowX0 = annPosPx.x + dx;
559+
}
551560

552-
if(options.ayref === options.yref)
561+
if(options.ayref === options.yref) {
553562
arrowY0 = annPosPx.aay + dy;
554-
else
563+
} else {
555564
arrowY0 = annPosPx.y + dy;
565+
}
556566

557567
// create transform matrix and related functions
558568
var transform =
@@ -654,15 +664,17 @@ annotations.draw = function(gd, index, opt, value) {
654664
(options.y + dy / ya._m) :
655665
(1 - ((arrowY + dy - gs.t) / gs.h));
656666

657-
if(options.axref === options.xref)
667+
if(options.axref === options.xref) {
658668
update[annbase + '.ax'] = xa ?
659669
(options.ax + dx / xa._m) :
660670
((arrowX + dx - gs.l) / gs.w);
671+
}
661672

662-
if(options.ayref === options.yref)
673+
if(options.ayref === options.yref) {
663674
update[annbase + '.ay'] = ya ?
664675
(options.ay + dy / ya._m) :
665676
(1 - ((arrowY + dy - gs.t) / gs.h));
677+
}
666678

667679
anng.attr({
668680
transform: 'rotate(' + textangle + ',' +
@@ -706,15 +718,17 @@ annotations.draw = function(gd, index, opt, value) {
706718
ann.call(Lib.setTranslate, x0 + dx, y0 + dy);
707719
var csr = 'pointer';
708720
if(options.showarrow) {
709-
if(options.axref === options.xref)
721+
if(options.axref === options.xref) {
710722
update[annbase + '.ax'] = xa.p2l(xa.l2p(options.ax) + dx);
711-
else
723+
} else {
712724
update[annbase + '.ax'] = options.ax + dx;
725+
}
713726

714-
if(options.ayref === options.yref)
727+
if(options.ayref === options.yref) {
715728
update[annbase + '.ay'] = ya.p2l(ya.l2p(options.ay) + dy);
716-
else
729+
} else {
717730
update[annbase + '.ay'] = options.ay + dy;
731+
}
718732

719733
drawArrow(dx, dy);
720734
}

src/plots/cartesian/axes.js

+20
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ axes.coerceRef = function(containerIn, containerOut, gd, axLetter, dflt) {
5555
return Lib.coerce(containerIn, containerOut, attrDef, refAttr);
5656
};
5757

58+
//todo: duplicated per github PR 610. Should be consolidated with axes.coerceRef.
59+
// find the list of possible axes to reference with an axref or ayref attribute
60+
// and coerce it to that list
61+
axes.coerceARef = function(containerIn, containerOut, gd, axLetter, dflt) {
62+
var axlist = gd._fullLayout._has('gl2d') ? [] : axes.listIds(gd, axLetter),
63+
refAttr = 'a' + axLetter + 'ref',
64+
attrDef = {};
65+
66+
// data-ref annotations are not supported in gl2d yet
67+
68+
attrDef[refAttr] = {
69+
valType: 'enumerated',
70+
values: axlist.concat(['pixel']),
71+
dflt: dflt || 'pixel' || axlist[0]
72+
};
73+
74+
// axref, ayref
75+
return Lib.coerce(containerIn, containerOut, attrDef, refAttr);
76+
};
77+
5878
// empty out types for all axes containing these traces
5979
// so we auto-set them again
6080
axes.clearTypes = function(gd, traces) {

0 commit comments

Comments
 (0)