-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcalc_errors.js
67 lines (51 loc) · 1.58 KB
/
calc_errors.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
/**
* Copyright 2012-2018, 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 Registry = require('../../registry');
function calculateAxisErrors(data, params, scaleFactor) {
if(!params || !params.visible) return null;
var computeError = Registry.getComponentMethod('errorbars', 'makeComputeError')(params);
var result = new Array(data.length);
for(var i = 0; i < data.length; i++) {
var errors = computeError(+data[i], i);
result[i] = [
-errors[0] * scaleFactor,
errors[1] * scaleFactor
];
}
return result;
}
function dataLength(array) {
for(var i = 0; i < array.length; i++) {
if(array[i]) return array[i].length;
}
return 0;
}
function calculateErrors(data, scaleFactor) {
var errors = [
calculateAxisErrors(data.x, data.error_x, scaleFactor[0]),
calculateAxisErrors(data.y, data.error_y, scaleFactor[1]),
calculateAxisErrors(data.z, data.error_z, scaleFactor[2])
];
var n = dataLength(errors);
if(n === 0) return null;
var errorBounds = new Array(n);
for(var i = 0; i < n; i++) {
var bound = [[0, 0, 0], [0, 0, 0]];
for(var j = 0; j < 3; j++) {
if(errors[j]) {
for(var k = 0; k < 2; k++) {
bound[k][j] = errors[j][i][k];
}
}
}
errorBounds[i] = bound;
}
return errorBounds;
}
module.exports = calculateErrors;