Skip to content

Commit 89c68e2

Browse files
authoredNov 10, 2016
Merge pull request #1078 from plotly/dates-as-dates
Dates as dates
2 parents ab193b2 + 1b0e133 commit 89c68e2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1803
-978
lines changed
 

‎src/components/annotations/annotation_defaults.js

+32-45
Original file line numberDiff line numberDiff line change
@@ -39,69 +39,56 @@ module.exports = function handleAnnotationDefaults(annIn, fullLayout) {
3939
var borderWidth = coerce('borderwidth');
4040
var showArrow = coerce('showarrow');
4141

42-
if(showArrow) {
43-
coerce('arrowcolor', borderOpacity ? annOut.bordercolor : Color.defaultLine);
44-
coerce('arrowhead');
45-
coerce('arrowsize');
46-
coerce('arrowwidth', ((borderOpacity && borderWidth) || 1) * 2);
47-
coerce('ax');
48-
coerce('ay');
49-
coerce('axref');
50-
coerce('ayref');
51-
52-
// if you have one part of arrow length you should have both
53-
Lib.noneOrAll(annIn, annOut, ['ax', 'ay']);
54-
}
55-
5642
coerce('text', showArrow ? ' ' : 'new text');
5743
coerce('textangle');
5844
Lib.coerceFont(coerce, 'font', fullLayout.font);
5945

6046
// positioning
61-
var axLetters = ['x', 'y'];
47+
var axLetters = ['x', 'y'],
48+
arrowPosDflt = [-10, -30],
49+
gdMock = {_fullLayout: fullLayout};
6250
for(var i = 0; i < 2; i++) {
63-
var axLetter = axLetters[i],
64-
tdMock = {_fullLayout: fullLayout};
51+
var axLetter = axLetters[i];
6552

6653
// xref, yref
67-
var axRef = Axes.coerceRef(annIn, annOut, tdMock, axLetter);
68-
69-
// TODO: should be refactored in conjunction with Axes axref, ayref
70-
var aaxRef = Axes.coerceARef(annIn, annOut, tdMock, axLetter);
54+
var axRef = Axes.coerceRef(annIn, annOut, gdMock, axLetter, '', 'paper');
7155

7256
// x, y
73-
var defaultPosition = 0.5;
74-
if(axRef !== 'paper') {
75-
var ax = Axes.getFromId(tdMock, axRef);
76-
defaultPosition = ax.range[0] + defaultPosition * (ax.range[1] - ax.range[0]);
77-
78-
// convert date or category strings to numbers
79-
if(['date', 'category'].indexOf(ax.type) !== -1 &&
80-
typeof annIn[axLetter] === 'string') {
81-
var newval;
82-
if(ax.type === 'date') {
83-
newval = Lib.dateTime2ms(annIn[axLetter]);
84-
if(newval !== false) annIn[axLetter] = newval;
85-
86-
if(aaxRef === axRef) {
87-
var newvalB = Lib.dateTime2ms(annIn['a' + axLetter]);
88-
if(newvalB !== false) annIn['a' + axLetter] = newvalB;
89-
}
90-
}
91-
else if((ax._categories || []).length) {
92-
newval = ax._categories.indexOf(annIn[axLetter]);
93-
if(newval !== -1) annIn[axLetter] = newval;
94-
}
57+
Axes.coercePosition(annOut, gdMock, coerce, axRef, axLetter, 0.5);
58+
59+
if(showArrow) {
60+
var arrowPosAttr = 'a' + axLetter,
61+
// axref, ayref
62+
aaxRef = Axes.coerceRef(annIn, annOut, gdMock, arrowPosAttr, 'pixel');
63+
64+
// for now the arrow can only be on the same axis or specified as pixels
65+
// TODO: sometime it might be interesting to allow it to be on *any* axis
66+
// but that would require updates to drawing & autorange code and maybe more
67+
if(aaxRef !== 'pixel' && aaxRef !== axRef) {
68+
aaxRef = annOut[arrowPosAttr] = 'pixel';
9569
}
70+
71+
// ax, ay
72+
var aDflt = (aaxRef === 'pixel') ? arrowPosDflt[i] : 0.4;
73+
Axes.coercePosition(annOut, gdMock, coerce, aaxRef, arrowPosAttr, aDflt);
9674
}
97-
coerce(axLetter, defaultPosition);
9875

9976
// xanchor, yanchor
100-
if(!showArrow) coerce(axLetter + 'anchor');
77+
else coerce(axLetter + 'anchor');
10178
}
10279

10380
// if you have one coordinate you should have both
10481
Lib.noneOrAll(annIn, annOut, ['x', 'y']);
10582

83+
if(showArrow) {
84+
coerce('arrowcolor', borderOpacity ? annOut.bordercolor : Color.defaultLine);
85+
coerce('arrowhead');
86+
coerce('arrowsize');
87+
coerce('arrowwidth', ((borderOpacity && borderWidth) || 1) * 2);
88+
89+
// if you have one part of arrow length you should have both
90+
Lib.noneOrAll(annIn, annOut, ['ax', 'ay']);
91+
}
92+
10693
return annOut;
10794
};

‎src/components/annotations/attributes.js

+24-10
Original file line numberDiff line numberDiff line change
@@ -141,27 +141,27 @@ module.exports = {
141141
description: 'Sets the width (in px) of annotation arrow.'
142142
},
143143
ax: {
144-
valType: 'number',
145-
dflt: -10,
144+
valType: 'any',
146145
role: 'info',
147146
description: [
148147
'Sets the x component of the arrow tail about the arrow head.',
149148
'If `axref` is `pixel`, a positive (negative) ',
150149
'component corresponds to an arrow pointing',
151150
'from right to left (left to right).',
152-
'If `axref` is an axis, this is a value on that axis.'
151+
'If `axref` is an axis, this is an absolute value on that axis,',
152+
'like `x`, NOT a relative value.'
153153
].join(' ')
154154
},
155155
ay: {
156-
valType: 'number',
157-
dflt: -30,
156+
valType: 'any',
158157
role: 'info',
159158
description: [
160159
'Sets the y component of the arrow tail about the arrow head.',
161160
'If `ayref` is `pixel`, a positive (negative) ',
162161
'component corresponds to an arrow pointing',
163162
'from bottom to top (top to bottom).',
164-
'If `ayref` is an axis, this is a value on that axis.'
163+
'If `ayref` is an axis, this is an absolute value on that axis,',
164+
'like `y`, NOT a relative value.'
165165
].join(' ')
166166
},
167167
axref: {
@@ -216,11 +216,18 @@ module.exports = {
216216
].join(' ')
217217
},
218218
x: {
219-
valType: 'number',
219+
valType: 'any',
220220
role: 'info',
221221
description: [
222222
'Sets the annotation\'s x position.',
223-
'Note that dates and categories are converted to numbers.'
223+
'If the axis `type` is *log*, then you must take the',
224+
'log of your desired range.',
225+
'If the axis `type` is *date*, it should be date strings,',
226+
'like date data, though Date objects and unix milliseconds',
227+
'will be accepted and converted to strings.',
228+
'If the axis `type` is *category*, it should be numbers,',
229+
'using the scale where each category is assigned a serial',
230+
'number from zero in the order it appears.'
224231
].join(' ')
225232
},
226233
xanchor: {
@@ -259,11 +266,18 @@ module.exports = {
259266
].join(' ')
260267
},
261268
y: {
262-
valType: 'number',
269+
valType: 'any',
263270
role: 'info',
264271
description: [
265272
'Sets the annotation\'s y position.',
266-
'Note that dates and categories are converted to numbers.'
273+
'If the axis `type` is *log*, then you must take the',
274+
'log of your desired range.',
275+
'If the axis `type` is *date*, it should be date strings,',
276+
'like date data, though Date objects and unix milliseconds',
277+
'will be accepted and converted to strings.',
278+
'If the axis `type` is *category*, it should be numbers,',
279+
'using the scale where each category is assigned a serial',
280+
'number from zero in the order it appears.'
267281
].join(' ')
268282
},
269283
yanchor: {

0 commit comments

Comments
 (0)