-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathaxis_defaults.js
259 lines (211 loc) · 7.87 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
/**
* 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 isNumeric = require('fast-isnumeric');
var Lib = require('../../lib');
var Plots = require('../plots');
var layoutAttributes = require('./layout_attributes');
var handleTickValueDefaults = require('./tick_value_defaults');
var handleTickDefaults = require('./tick_defaults');
var setConvert = require('./set_convert');
var cleanDatum = require('./clean_datum');
var axisIds = require('./axis_ids');
/**
* options: object containing:
*
* letter: 'x' or 'y'
* title: name of the axis (ie 'Colorbar') to go in default title
* name: axis object name (ie 'xaxis') if one should be stored
* 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?
* data: the plot data to use in choosing auto type
*/
module.exports = function handleAxisDefaults(containerIn, containerOut, coerce, options) {
var letter = options.letter,
font = options.font || {},
defaultTitle = 'Click to enter ' +
(options.title || (letter.toUpperCase() + ' axis')) +
' title';
// set up some private properties
if(options.name) {
containerOut._name = options.name;
containerOut._id = axisIds.name2id(options.name);
}
// now figure out type and do some more initialization
var axType = coerce('type');
if(axType === '-') {
setAutoType(containerOut, options.data);
if(containerOut.type === '-') {
containerOut.type = 'linear';
}
else {
// copy autoType back to input axis
// note that if this object didn't exist
// in the input layout, we have to put it in
// this happens in the main supplyDefaults function
axType = containerIn.type = containerOut.type;
}
}
setConvert(containerOut);
coerce('title', defaultTitle);
Lib.coerceFont(coerce, 'titlefont', {
family: font.family,
size: Math.round(font.size * 1.2),
color: font.color
});
var validRange = (
(containerIn.range || []).length === 2 &&
isNumeric(containerIn.range[0]) &&
isNumeric(containerIn.range[1])
);
var autoRange = coerce('autorange', !validRange);
if(autoRange) coerce('rangemode');
var range = coerce('range', [-1, letter === 'x' ? 6 : 4]);
if(range[0] === range[1]) {
containerOut.range = [range[0] - 1, range[0] + 1];
}
Lib.noneOrAll(containerIn.range, containerOut.range, [0, 1]);
coerce('fixedrange');
handleTickValueDefaults(containerIn, containerOut, coerce, axType);
handleTickDefaults(containerIn, containerOut, coerce, axType, options);
var lineColor = Lib.coerce2(containerIn, containerOut, layoutAttributes, 'linecolor'),
lineWidth = Lib.coerce2(containerIn, containerOut, layoutAttributes, 'linewidth'),
showLine = coerce('showline', !!lineColor || !!lineWidth);
if(!showLine) {
delete containerOut.linecolor;
delete containerOut.linewidth;
}
if(showLine || containerOut.ticks) coerce('mirror');
var gridColor = Lib.coerce2(containerIn, containerOut, layoutAttributes, 'gridcolor'),
gridWidth = Lib.coerce2(containerIn, containerOut, layoutAttributes, 'gridwidth'),
showGridLines = coerce('showgrid', options.showGrid || !!gridColor || !!gridWidth);
if(!showGridLines) {
delete containerOut.gridcolor;
delete containerOut.gridwidth;
}
var zeroLineColor = Lib.coerce2(containerIn, containerOut, layoutAttributes, 'zerolinecolor'),
zeroLineWidth = Lib.coerce2(containerIn, containerOut, layoutAttributes, 'zerolinewidth'),
showZeroLine = coerce('zeroline', options.showGrid || !!zeroLineColor || !!zeroLineWidth);
if(!showZeroLine) {
delete containerOut.zerolinecolor;
delete containerOut.zerolinewidth;
}
return containerOut;
};
function setAutoType(ax, data) {
// new logic: let people specify any type they want,
// only autotype if type is '-'
if(ax.type!=='-') return;
var id = ax._id,
axLetter = id.charAt(0);
// support 3d
if(id.indexOf('scene') !== -1) id = axLetter;
var d0 = getFirstNonEmptyTrace(data, id, axLetter);
if(!d0) return;
// first check for histograms, as the count direction
// should always default to a linear axis
if(d0.type==='histogram' &&
axLetter === {v: 'y', h: 'x'}[d0.orientation || 'v']) {
ax.type='linear';
return;
}
// check all boxes on this x axis to see
// if they're dates, numbers, or categories
if(isBoxWithoutPositionCoords(d0, axLetter)) {
var posLetter = getBoxPosLetter(d0),
boxPositions = [],
trace;
for(var i = 0; i < data.length; i++) {
trace = data[i];
if(!Plots.traceIs(trace, 'box') ||
(trace[axLetter + 'axis'] || axLetter) !== id) continue;
if(trace[posLetter] !== undefined) boxPositions.push(trace[posLetter][0]);
else if(trace.name !== undefined) boxPositions.push(trace.name);
else boxPositions.push('text');
}
ax.type = autoType(boxPositions);
}
else {
ax.type = autoType(d0[axLetter] || [d0[axLetter+'0']]);
}
}
function getBoxPosLetter(trace) {
return {v: 'x', h: 'y'}[trace.orientation || 'v'];
}
function isBoxWithoutPositionCoords(trace, axLetter) {
var posLetter = getBoxPosLetter(trace);
return (
Plots.traceIs(trace, 'box') &&
axLetter === posLetter &&
trace[posLetter] === undefined &&
trace[posLetter + '0'] === undefined
);
}
function autoType(array) {
if(moreDates(array)) return 'date';
if(category(array)) return 'category';
if(linearOK(array)) return 'linear';
else return '-';
}
function getFirstNonEmptyTrace(data, id, axLetter) {
for(var i = 0; i < data.length; i++) {
var trace = data[i];
if((trace[axLetter + 'axis'] || axLetter) === id) {
if(isBoxWithoutPositionCoords(trace, axLetter)) {
return trace;
}
else if((trace[axLetter] || []).length || trace[axLetter + '0']) {
return trace;
}
}
}
}
// is there at least one number in array? If not, we should leave
// ax.type empty so it can be autoset later
function linearOK(array) {
if(!array) return false;
for(var i = 0; i < array.length; i++) {
if(isNumeric(array[i])) return true;
}
return false;
}
// does the array a have mostly dates rather than numbers?
// note: some values can be neither (such as blanks, text)
// 2- or 4-digit integers can be both, so require twice as many
// dates as non-dates, to exclude cases with mostly 2 & 4 digit
// numbers and a few dates
function moreDates(a) {
var dcnt = 0,
ncnt = 0,
// test at most 1000 points, evenly spaced
inc = Math.max(1, (a.length - 1) / 1000),
ai;
for(var i = 0; i < a.length; i += inc) {
ai = a[Math.round(i)];
if(Lib.isDateTime(ai)) dcnt += 1;
if(isNumeric(ai)) ncnt += 1;
}
return (dcnt > ncnt * 2);
}
// are the (x,y)-values in td.data mostly text?
// require twice as many categories as numbers
function category(a) {
// test at most 1000 points
var inc = Math.max(1, (a.length - 1) / 1000),
curvenums = 0,
curvecats = 0,
ai;
for(var i = 0; i < a.length; i += inc) {
ai = cleanDatum(a[Math.round(i)]);
if(isNumeric(ai)) curvenums++;
else if(typeof ai === 'string' && ai !== '' && ai !== 'None') curvecats++;
}
return curvecats > curvenums * 2;
}