-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathdefaults.js
99 lines (76 loc) · 3.12 KB
/
defaults.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* Copyright 2012-2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var Axes = require('../../plots/cartesian/axes');
var handleArrayContainerDefaults = require('../../plots/array_container_defaults');
var handleAnnotationCommonDefaults = require('./common_defaults');
var attributes = require('./attributes');
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) {
handleArrayContainerDefaults(layoutIn, layoutOut, {
name: 'annotations',
handleItemDefaults: handleAnnotationDefaults
});
};
function handleAnnotationDefaults(annIn, annOut, fullLayout) {
function coerce(attr, dflt) {
return Lib.coerce(annIn, annOut, attributes, attr, dflt);
}
var visible = coerce('visible');
var clickToShow = coerce('clicktoshow');
if(!(visible || clickToShow)) return;
handleAnnotationCommonDefaults(annIn, annOut, fullLayout, coerce);
var showArrow = annOut.showarrow;
// positioning
var axLetters = ['x', 'y'],
arrowPosDflt = [-10, -30],
gdMock = {_fullLayout: fullLayout};
for(var i = 0; i < 2; i++) {
var axLetter = axLetters[i];
// xref, yref
var axRef = Axes.coerceRef(annIn, annOut, gdMock, axLetter, '', 'paper');
// x, y
Axes.coercePosition(annOut, gdMock, coerce, axRef, axLetter, 0.5);
if(showArrow) {
var arrowPosAttr = 'a' + axLetter,
// axref, ayref
aaxRef = Axes.coerceRef(annIn, annOut, gdMock, arrowPosAttr, 'pixel');
// for now the arrow can only be on the same axis or specified as pixels
// TODO: sometime it might be interesting to allow it to be on *any* axis
// but that would require updates to drawing & autorange code and maybe more
if(aaxRef !== 'pixel' && aaxRef !== axRef) {
aaxRef = annOut[arrowPosAttr] = 'pixel';
}
// ax, ay
var aDflt = (aaxRef === 'pixel') ? arrowPosDflt[i] : 0.4;
Axes.coercePosition(annOut, gdMock, coerce, aaxRef, arrowPosAttr, aDflt);
}
// xanchor, yanchor
coerce(axLetter + 'anchor');
// xshift, yshift
coerce(axLetter + 'shift');
}
// if you have one coordinate you should have both
Lib.noneOrAll(annIn, annOut, ['x', 'y']);
// if you have one part of arrow length you should have both
if(showArrow) {
Lib.noneOrAll(annIn, annOut, ['ax', 'ay']);
}
if(clickToShow) {
var xClick = coerce('xclick');
var yClick = coerce('yclick');
// put the actual click data to bind to into private attributes
// so we don't have to do this little bit of logic on every hover event
annOut._xclick = (xClick === undefined) ?
annOut.x :
Axes.cleanPosition(xClick, gdMock, annOut.xref);
annOut._yclick = (yClick === undefined) ?
annOut.y :
Axes.cleanPosition(yClick, gdMock, annOut.yref);
}
}