-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathaxis_defaults.js
271 lines (232 loc) · 9.21 KB
/
axis_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
* Copyright 2012-2020, 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 isNumeric = require('fast-isnumeric');
var Registry = require('../../registry');
var Lib = require('../../lib');
var handleArrayContainerDefaults = require('../array_container_defaults');
var layoutAttributes = require('./layout_attributes');
var handleTickValueDefaults = require('./tick_value_defaults');
var handleTickMarkDefaults = require('./tick_mark_defaults');
var handleTickLabelDefaults = require('./tick_label_defaults');
var handleCategoryOrderDefaults = require('./category_order_defaults');
var handleLineGridDefaults = require('./line_grid_defaults');
var setConvert = require('./set_convert');
var DAY_OF_WEEK = require('./constants').WEEKDAY_PATTERN;
var HOUR = require('./constants').HOUR_PATTERN;
/**
* options: object containing:
*
* letter: 'x' or 'y'
* title: name of the axis (ie 'Colorbar') to go in default title
* font: the default font to inherit
* outerTicks: boolean, should ticks default to outside?
* showGrid: boolean, should gridlines be shown by default?
* noHover: boolean, this axis doesn't support hover effects?
* noTickson: boolean, this axis doesn't support 'tickson'
* data: the plot data, used to manage categories
* bgColor: the plot background color, to calculate default gridline colors
* calendar:
* splomStash:
* visibleDflt: boolean
* reverseDflt: boolean
* automargin: boolean
*/
module.exports = function handleAxisDefaults(containerIn, containerOut, coerce, options, layoutOut) {
var letter = options.letter;
var font = options.font || {};
var splomStash = options.splomStash || {};
var visible = coerce('visible', !options.visibleDflt);
var axType = containerOut.type || options.axTemplate.type || '-';
if(axType === 'date') {
var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleDefaults');
handleCalendarDefaults(containerIn, containerOut, 'calendar', options.calendar);
}
setConvert(containerOut, layoutOut);
var autorangeDflt = !containerOut.isValidRange(containerIn.range);
if(autorangeDflt && options.reverseDflt) autorangeDflt = 'reversed';
var autoRange = coerce('autorange', autorangeDflt);
if(autoRange && (axType === 'linear' || axType === '-')) coerce('rangemode');
coerce('range');
containerOut.cleanRange();
handleCategoryOrderDefaults(containerIn, containerOut, coerce, options);
if(axType !== 'category' && !options.noHover) coerce('hoverformat');
var dfltColor = coerce('color');
// if axis.color was provided, use it for fonts too; otherwise,
// inherit from global font color in case that was provided.
// Compare to dflt rather than to containerIn, so we can provide color via
// template too.
var dfltFontColor = (dfltColor !== layoutAttributes.color.dflt) ? dfltColor : font.color;
// try to get default title from splom trace, fallback to graph-wide value
var dfltTitle = splomStash.label || layoutOut._dfltTitle[letter];
handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options, {pass: 1});
if(!visible) return containerOut;
coerce('title.text', dfltTitle);
Lib.coerceFont(coerce, 'title.font', {
family: font.family,
size: Math.round(font.size * 1.2),
color: dfltFontColor
});
handleTickValueDefaults(containerIn, containerOut, coerce, axType);
handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options, {pass: 2});
handleTickMarkDefaults(containerIn, containerOut, coerce, options);
handleLineGridDefaults(containerIn, containerOut, coerce, {
dfltColor: dfltColor,
bgColor: options.bgColor,
showGrid: options.showGrid,
attributes: layoutAttributes
});
if(containerOut.showline || containerOut.ticks) coerce('mirror');
if(options.automargin) coerce('automargin');
var isMultiCategory = axType === 'multicategory';
if(!options.noTickson &&
(axType === 'category' || isMultiCategory) &&
(containerOut.ticks || containerOut.showgrid)
) {
var ticksonDflt;
if(isMultiCategory) ticksonDflt = 'boundaries';
coerce('tickson', ticksonDflt);
}
if(isMultiCategory) {
var showDividers = coerce('showdividers');
if(showDividers) {
coerce('dividercolor');
coerce('dividerwidth');
}
}
if(axType === 'date') {
var fromTemplate = (options.axTemplate || {}).rangebreaks;
var rangebreaks = containerIn.rangebreaks || fromTemplate;
if(Array.isArray(rangebreaks) && rangebreaks.length) {
handleArrayContainerDefaults(containerIn, containerOut, {
name: 'rangebreaks',
inclusionAttr: 'enabled',
handleItemDefaults: rangebreaksDefaults
});
setConvert(containerOut, layoutOut);
if(layoutOut._has('scattergl') || layoutOut._has('splom')) {
for(var i = 0; i < options.data.length; i++) {
var trace = options.data[i];
if(trace.type === 'scattergl' || trace.type === 'splom') {
trace.visible = false;
Lib.warn(trace.type +
' traces do not work on axes with rangebreaks.' +
' Setting trace ' + trace.index + ' to `visible: false`.');
}
}
}
}
}
return containerOut;
};
function rangebreaksDefaults(itemIn, itemOut, containerOut) {
function coerce(attr, dflt) {
return Lib.coerce(itemIn, itemOut, layoutAttributes.rangebreaks, attr, dflt);
}
var enabled = coerce('enabled');
if(enabled) {
var bnds = coerce('bounds');
if(bnds && bnds.length >= 2) {
var dfltPattern = '';
var i, q;
if(bnds.length === 2) {
for(i = 0; i < 2; i++) {
q = indexOfDay(bnds[i]);
if(q) {
dfltPattern = DAY_OF_WEEK;
break;
}
}
}
var pattern = coerce('pattern', dfltPattern);
if(pattern === DAY_OF_WEEK) {
for(i = 0; i < 2; i++) {
q = indexOfDay(bnds[i]);
if(q) {
// convert to integers i.e 'Sunday' --> 0
itemOut.bounds[i] = bnds[i] = q - 1;
}
}
}
if(pattern) {
// ensure types and ranges
for(i = 0; i < 2; i++) {
q = bnds[i];
switch(pattern) {
case DAY_OF_WEEK :
if(!isNumeric(q)) {
itemOut.enabled = false;
return;
}
q = +q;
if(
q !== Math.floor(q) || // don't accept fractional days for mow
q < 0 || q >= 7
) {
itemOut.enabled = false;
return;
}
// use number
itemOut.bounds[i] = bnds[i] = q;
break;
case HOUR :
if(!isNumeric(q)) {
itemOut.enabled = false;
return;
}
q = +q;
if(q < 0 || q > 24) { // accept 24
itemOut.enabled = false;
return;
}
// use number
itemOut.bounds[i] = bnds[i] = q;
break;
}
}
}
if(containerOut.autorange === false) {
var rng = containerOut.range;
// if bounds are bigger than the (set) range, disable break
if(rng[0] < rng[1]) {
if(bnds[0] < rng[0] && bnds[1] > rng[1]) {
itemOut.enabled = false;
return;
}
} else if(bnds[0] > rng[0] && bnds[1] < rng[1]) {
itemOut.enabled = false;
return;
}
}
} else {
var values = coerce('values');
if(values && values.length) {
coerce('dvalue');
} else {
itemOut.enabled = false;
return;
}
}
}
}
// these numbers are one more than what bounds would be mapped to
var dayStrToNum = {
sun: 1,
mon: 2,
tue: 3,
wed: 4,
thu: 5,
fri: 6,
sat: 7
};
function indexOfDay(v) {
if(typeof v !== 'string') return;
return dayStrToNum[
v.substr(0, 3).toLowerCase()
];
}