-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathannotation_defaults.js
129 lines (98 loc) · 3.95 KB
/
annotation_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Copyright 2012-2017, 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 Color = require('../color');
var Fx = require('../fx');
var Axes = require('../../plots/cartesian/axes');
var attributes = require('./attributes');
module.exports = function handleAnnotationDefaults(annIn, annOut, fullLayout, opts, itemOpts) {
opts = opts || {};
itemOpts = itemOpts || {};
function coerce(attr, dflt) {
return Lib.coerce(annIn, annOut, attributes, attr, dflt);
}
var visible = coerce('visible', !itemOpts.itemIsNotPlainObject);
var clickToShow = coerce('clicktoshow');
if(!(visible || clickToShow)) return annOut;
coerce('opacity');
var bgColor = coerce('bgcolor');
var borderColor = coerce('bordercolor'),
borderOpacity = Color.opacity(borderColor);
coerce('borderpad');
var borderWidth = coerce('borderwidth');
var showArrow = coerce('showarrow');
coerce('text', showArrow ? ' ' : 'new text');
coerce('textangle');
Lib.coerceFont(coerce, 'font', fullLayout.font);
coerce('width');
coerce('align');
var h = coerce('height');
if(h) coerce('valign');
// 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(showArrow) {
coerce('arrowcolor', borderOpacity ? annOut.bordercolor : Color.defaultLine);
coerce('arrowhead');
coerce('arrowsize');
coerce('arrowwidth', ((borderOpacity && borderWidth) || 1) * 2);
coerce('standoff');
// if you have one part of arrow length you should have both
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 : xClick;
annOut._yclick = (yClick === undefined) ? annOut.y : yClick;
}
var hoverText = coerce('hovertext');
if(hoverText) {
var hoverBG = coerce('hoverlabel.bgcolor',
Color.opacity(bgColor) ? Color.rgb(bgColor) : Color.defaultLine);
var hoverBorder = coerce('hoverlabel.bordercolor', Color.contrast(hoverBG));
Lib.coerceFont(coerce, 'hoverlabel.font', {
family: Fx.constants.HOVERFONT,
size: Fx.constants.HOVERFONTSIZE,
color: hoverBorder
});
}
coerce('captureevents', !!hoverText);
return annOut;
};