-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathxyz_defaults.js
106 lines (82 loc) · 2.81 KB
/
xyz_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
/**
* 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 Lib = require('../../lib');
var Registry = require('../../registry');
module.exports = function handleXYZDefaults(traceIn, traceOut, coerce, layout, xName, yName) {
var z = coerce('z');
xName = xName || 'x';
yName = yName || 'y';
var x, y;
var shapeX = x ? (x.shape ? x.shape[0] : x.length) || 0 : 0;
var shapeY = y ? (y.shape ? y.shape[0] : y.length) || 0 : 0;
var shapeZ = z ? (z.shape ? z.shape[0] : z.length) || 0 : 0;
var zlen = shapeZ || (z && z.length) || 0;
if(z === undefined || !zlen) return 0;
if(Lib.isArray1D(traceIn.z) || (z && z.shape && z.shape.length === 1)) {
x = coerce(xName);
y = coerce(yName);
var xlen = shapeX || Lib.minRowLength(x);
var ylen = shapeY || Lib.minRowLength(y);
// column z must be accompanied by xName and yName arrays
if(xlen === 0 || ylen === 0) return 0;
traceOut._length = Math.min(xlen, ylen, zlen);
} else {
x = coordDefaults(xName, coerce);
y = coordDefaults(yName, coerce);
// TODO put z validation elsewhere
if(!isValidZ(z)) return 0;
coerce('transpose');
traceOut._length = null;
}
if(
traceIn.type === 'heatmapgl' ||
traceIn.type === 'contourgl'
) return true; // skip calendars until we handle them in those traces
var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleTraceDefaults');
handleCalendarDefaults(traceIn, traceOut, [xName, yName], layout);
return true;
};
function coordDefaults(coordStr, coerce) {
var coord = coerce(coordStr);
var coordType = coord ? coerce(coordStr + 'type', 'array') : 'scaled';
if(coordType === 'scaled') {
coerce(coordStr + '0');
coerce('d' + coordStr);
}
return coord;
}
function isValidZ(z) {
var allRowsAreArrays = true;
var oneRowIsFilled = false;
var hasOneNumber = false;
var zi;
/*
* Without this step:
*
* hasOneNumber = false breaks contour but not heatmap
* allRowsAreArrays = false breaks contour but not heatmap
* oneRowIsFilled = false breaks both
*/
for(var i = 0; i < z.length; i++) {
zi = z[i];
if(!Lib.isArrayOrTypedArray(zi)) {
allRowsAreArrays = false;
break;
}
if(zi.length > 0) oneRowIsFilled = true;
for(var j = 0; j < zi.length; j++) {
if(isNumeric(zi[j])) {
hasOneNumber = true;
break;
}
}
}
return (allRowsAreArrays && oneRowIsFilled && hasOneNumber);
}