-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathannotation_defaults.js
107 lines (82 loc) · 3.22 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
/**
* Copyright 2012-2016, 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 Axes = require('../../plots/cartesian/axes');
var attributes = require('./attributes');
module.exports = function handleAnnotationDefaults(annIn, annOut, fullLayout, opts) {
opts = opts || {};
function coerce(attr, dflt) {
return Lib.coerce(annIn, annOut, attributes, attr, dflt);
}
var visible = coerce('visible', !opts.itemIsNotPlainObject);
if(!visible) return annOut;
coerce('opacity');
coerce('align');
coerce('bgcolor');
var borderColor = coerce('bordercolor'),
borderOpacity = Color.opacity(borderColor);
coerce('borderpad');
var borderWidth = coerce('borderwidth');
var showArrow = coerce('showarrow');
if(showArrow) {
coerce('arrowcolor', borderOpacity ? annOut.bordercolor : Color.defaultLine);
coerce('arrowhead');
coerce('arrowsize');
coerce('arrowwidth', ((borderOpacity && borderWidth) || 1) * 2);
coerce('ax');
coerce('ay');
coerce('axref');
coerce('ayref');
// if you have one part of arrow length you should have both
Lib.noneOrAll(annIn, annOut, ['ax', 'ay']);
}
coerce('text', showArrow ? ' ' : 'new text');
coerce('textangle');
Lib.coerceFont(coerce, 'font', fullLayout.font);
// positioning
var axLetters = ['x', 'y'];
for(var i = 0; i < 2; i++) {
var axLetter = axLetters[i],
tdMock = {_fullLayout: fullLayout};
// xref, yref
var axRef = Axes.coerceRef(annIn, annOut, tdMock, axLetter);
// TODO: should be refactored in conjunction with Axes axref, ayref
var aaxRef = Axes.coerceARef(annIn, annOut, tdMock, axLetter);
// x, y
var defaultPosition = 0.5;
if(axRef !== 'paper') {
var ax = Axes.getFromId(tdMock, axRef);
defaultPosition = ax.range[0] + defaultPosition * (ax.range[1] - ax.range[0]);
// convert date or category strings to numbers
if(['date', 'category'].indexOf(ax.type) !== -1 &&
typeof annIn[axLetter] === 'string') {
var newval;
if(ax.type === 'date') {
newval = Lib.dateTime2ms(annIn[axLetter]);
if(newval !== false) annIn[axLetter] = newval;
if(aaxRef === axRef) {
var newvalB = Lib.dateTime2ms(annIn['a' + axLetter]);
if(newvalB !== false) annIn['a' + axLetter] = newvalB;
}
}
else if((ax._categories || []).length) {
newval = ax._categories.indexOf(annIn[axLetter]);
if(newval !== -1) annIn[axLetter] = newval;
}
}
}
coerce(axLetter, defaultPosition);
// xanchor, yanchor
if(!showArrow) coerce(axLetter + 'anchor');
}
// if you have one coordinate you should have both
Lib.noneOrAll(annIn, annOut, ['x', 'y']);
return annOut;
};