Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b5ccfbe

Browse files
committed
combine annotation defaults files & shape defaults files
1 parent 8ddcfd6 commit b5ccfbe

File tree

4 files changed

+189
-224
lines changed

4 files changed

+189
-224
lines changed

src/components/annotations/annotation_defaults.js

Lines changed: 0 additions & 95 deletions
This file was deleted.

src/components/annotations/defaults.js

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,93 @@
99

1010
'use strict';
1111

12+
var Lib = require('../../lib');
13+
var Axes = require('../../plots/cartesian/axes');
1214
var handleArrayContainerDefaults = require('../../plots/array_container_defaults');
13-
var handleAnnotationDefaults = require('./annotation_defaults');
15+
16+
var handleAnnotationCommonDefaults = require('./common_defaults');
17+
var attributes = require('./attributes');
1418

1519

1620
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) {
17-
var opts = {
21+
handleArrayContainerDefaults(layoutIn, layoutOut, {
1822
name: 'annotations',
1923
handleItemDefaults: handleAnnotationDefaults
20-
};
21-
22-
handleArrayContainerDefaults(layoutIn, layoutOut, opts);
24+
});
2325
};
26+
27+
function handleAnnotationDefaults(annIn, annOut, fullLayout, opts, itemOpts) {
28+
function coerce(attr, dflt) {
29+
return Lib.coerce(annIn, annOut, attributes, attr, dflt);
30+
}
31+
32+
var visible = coerce('visible', !itemOpts.itemIsNotPlainObject);
33+
var clickToShow = coerce('clicktoshow');
34+
35+
if(!(visible || clickToShow)) return annOut;
36+
37+
handleAnnotationCommonDefaults(annIn, annOut, fullLayout, coerce);
38+
39+
var showArrow = annOut.showarrow;
40+
41+
// positioning
42+
var axLetters = ['x', 'y'],
43+
arrowPosDflt = [-10, -30],
44+
gdMock = {_fullLayout: fullLayout};
45+
for(var i = 0; i < 2; i++) {
46+
var axLetter = axLetters[i];
47+
48+
// xref, yref
49+
var axRef = Axes.coerceRef(annIn, annOut, gdMock, axLetter, '', 'paper');
50+
51+
// x, y
52+
Axes.coercePosition(annOut, gdMock, coerce, axRef, axLetter, 0.5);
53+
54+
if(showArrow) {
55+
var arrowPosAttr = 'a' + axLetter,
56+
// axref, ayref
57+
aaxRef = Axes.coerceRef(annIn, annOut, gdMock, arrowPosAttr, 'pixel');
58+
59+
// for now the arrow can only be on the same axis or specified as pixels
60+
// TODO: sometime it might be interesting to allow it to be on *any* axis
61+
// but that would require updates to drawing & autorange code and maybe more
62+
if(aaxRef !== 'pixel' && aaxRef !== axRef) {
63+
aaxRef = annOut[arrowPosAttr] = 'pixel';
64+
}
65+
66+
// ax, ay
67+
var aDflt = (aaxRef === 'pixel') ? arrowPosDflt[i] : 0.4;
68+
Axes.coercePosition(annOut, gdMock, coerce, aaxRef, arrowPosAttr, aDflt);
69+
}
70+
71+
// xanchor, yanchor
72+
coerce(axLetter + 'anchor');
73+
74+
// xshift, yshift
75+
coerce(axLetter + 'shift');
76+
}
77+
78+
// if you have one coordinate you should have both
79+
Lib.noneOrAll(annIn, annOut, ['x', 'y']);
80+
81+
// if you have one part of arrow length you should have both
82+
if(showArrow) {
83+
Lib.noneOrAll(annIn, annOut, ['ax', 'ay']);
84+
}
85+
86+
if(clickToShow) {
87+
var xClick = coerce('xclick');
88+
var yClick = coerce('yclick');
89+
90+
// put the actual click data to bind to into private attributes
91+
// so we don't have to do this little bit of logic on every hover event
92+
annOut._xclick = (xClick === undefined) ?
93+
annOut.x :
94+
Axes.cleanPosition(xClick, gdMock, annOut.xref);
95+
annOut._yclick = (yClick === undefined) ?
96+
annOut.y :
97+
Axes.cleanPosition(yClick, gdMock, annOut.yref);
98+
}
99+
100+
return annOut;
101+
}

src/components/shapes/defaults.js

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,116 @@
99

1010
'use strict';
1111

12+
var Lib = require('../../lib');
13+
var Axes = require('../../plots/cartesian/axes');
1214
var handleArrayContainerDefaults = require('../../plots/array_container_defaults');
13-
var handleShapeDefaults = require('./shape_defaults');
15+
16+
var attributes = require('./attributes');
17+
var helpers = require('./helpers');
1418

1519

1620
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) {
17-
var opts = {
21+
handleArrayContainerDefaults(layoutIn, layoutOut, {
1822
name: 'shapes',
1923
handleItemDefaults: handleShapeDefaults
20-
};
21-
22-
handleArrayContainerDefaults(layoutIn, layoutOut, opts);
24+
});
2325
};
26+
27+
function handleShapeDefaults(shapeIn, shapeOut, fullLayout, opts, itemOpts) {
28+
function coerce(attr, dflt) {
29+
return Lib.coerce(shapeIn, shapeOut, attributes, attr, dflt);
30+
}
31+
32+
var visible = coerce('visible', !itemOpts.itemIsNotPlainObject);
33+
34+
if(!visible) return shapeOut;
35+
36+
coerce('layer');
37+
coerce('opacity');
38+
coerce('fillcolor');
39+
coerce('line.color');
40+
coerce('line.width');
41+
coerce('line.dash');
42+
43+
var dfltType = shapeIn.path ? 'path' : 'rect',
44+
shapeType = coerce('type', dfltType),
45+
xSizeMode = coerce('xsizemode'),
46+
ySizeMode = coerce('ysizemode');
47+
48+
// positioning
49+
var axLetters = ['x', 'y'];
50+
for(var i = 0; i < 2; i++) {
51+
var axLetter = axLetters[i],
52+
attrAnchor = axLetter + 'anchor',
53+
sizeMode = axLetter === 'x' ? xSizeMode : ySizeMode,
54+
gdMock = {_fullLayout: fullLayout},
55+
ax,
56+
pos2r,
57+
r2pos;
58+
59+
// xref, yref
60+
var axRef = Axes.coerceRef(shapeIn, shapeOut, gdMock, axLetter, '', 'paper');
61+
62+
if(axRef !== 'paper') {
63+
ax = Axes.getFromId(gdMock, axRef);
64+
r2pos = helpers.rangeToShapePosition(ax);
65+
pos2r = helpers.shapePositionToRange(ax);
66+
}
67+
else {
68+
pos2r = r2pos = Lib.identity;
69+
}
70+
71+
// Coerce x0, x1, y0, y1
72+
if(shapeType !== 'path') {
73+
var dflt0 = 0.25,
74+
dflt1 = 0.75;
75+
76+
// hack until V2.0 when log has regular range behavior - make it look like other
77+
// ranges to send to coerce, then put it back after
78+
// this is all to give reasonable default position behavior on log axes, which is
79+
// a pretty unimportant edge case so we could just ignore this.
80+
var attr0 = axLetter + '0',
81+
attr1 = axLetter + '1',
82+
in0 = shapeIn[attr0],
83+
in1 = shapeIn[attr1];
84+
shapeIn[attr0] = pos2r(shapeIn[attr0], true);
85+
shapeIn[attr1] = pos2r(shapeIn[attr1], true);
86+
87+
if(sizeMode === 'pixel') {
88+
coerce(attr0, 0);
89+
coerce(attr1, 10);
90+
} else {
91+
Axes.coercePosition(shapeOut, gdMock, coerce, axRef, attr0, dflt0);
92+
Axes.coercePosition(shapeOut, gdMock, coerce, axRef, attr1, dflt1);
93+
}
94+
95+
// hack part 2
96+
shapeOut[attr0] = r2pos(shapeOut[attr0]);
97+
shapeOut[attr1] = r2pos(shapeOut[attr1]);
98+
shapeIn[attr0] = in0;
99+
shapeIn[attr1] = in1;
100+
}
101+
102+
// Coerce xanchor and yanchor
103+
if(sizeMode === 'pixel') {
104+
// Hack for log axis described above
105+
var inAnchor = shapeIn[attrAnchor];
106+
shapeIn[attrAnchor] = pos2r(shapeIn[attrAnchor], true);
107+
108+
Axes.coercePosition(shapeOut, gdMock, coerce, axRef, attrAnchor, 0.25);
109+
110+
// Hack part 2
111+
shapeOut[attrAnchor] = r2pos(shapeOut[attrAnchor]);
112+
shapeIn[attrAnchor] = inAnchor;
113+
}
114+
}
115+
116+
if(shapeType === 'path') {
117+
coerce('path');
118+
}
119+
else {
120+
Lib.noneOrAll(shapeIn, shapeOut, ['x0', 'x1', 'y0', 'y1']);
121+
}
122+
123+
return shapeOut;
124+
}

0 commit comments

Comments
 (0)