Skip to content

Commit 007b8d1

Browse files
committed
make compute_error export a compute-error generating function,
- so that the opts object is destructure only once once per trace instead of on every data pt.
1 parent 3c21918 commit 007b8d1

File tree

3 files changed

+58
-31
lines changed

3 files changed

+58
-31
lines changed

src/components/errorbars/calc.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var isNumeric = require('fast-isnumeric');
1414
var Plots = require('../../plots/plots');
1515
var Axes = require('../../plots/cartesian/axes');
1616

17-
var computeError = require('./compute_error');
17+
var makeComputeError = require('./compute_error');
1818

1919

2020
module.exports = function calc(gd) {
@@ -26,33 +26,36 @@ module.exports = function calc(gd) {
2626

2727
if(!Plots.traceIs(trace, 'errorBarsOK')) continue;
2828

29-
var xObj = trace.error_x || {},
30-
yObj = trace.error_y || {},
29+
var xOpts = trace.error_x || {},
30+
yOpts = trace.error_y || {},
3131
xa = Axes.getFromId(gd, trace.xaxis),
3232
ya = Axes.getFromId(gd, trace.yaxis),
33-
xVis = (xObj.visible && ['linear', 'log'].indexOf(xa.type) !== -1),
34-
yVis = (yObj.visible && ['linear', 'log'].indexOf(ya.type) !== -1);
33+
xVis = (xOpts.visible && ['linear', 'log'].indexOf(xa.type) !== -1),
34+
yVis = (yOpts.visible && ['linear', 'log'].indexOf(ya.type) !== -1);
3535

3636
if(!xVis && !yVis) continue;
3737

3838
var xVals = [],
3939
yVals = [];
4040

41+
var computeErrorY = makeComputeError(yOpts),
42+
computeErrorX = makeComputeError(xOpts);
43+
4144
for(var j = 0; j < calcTrace.length; j++) {
4245
var calcPt = calcTrace[j],
43-
calcX = calcPt.x,
44-
calcY = calcPt.y;
46+
calcY = calcPt.y,
47+
calcX = calcPt.x;
4548

4649
if(!isNumeric(ya.c2l(calcY)) || !isNumeric(xa.c2l(calcX))) continue;
4750

48-
var errorY = computeError(calcY, j, yObj);
51+
var errorY = computeErrorY(calcY, j);
4952
if(isNumeric(errorY[0]) && isNumeric(errorY[1])) {
5053
calcPt.ys = calcY - errorY[0];
5154
calcPt.yh = calcY + errorY[1];
5255
yVals.push(calcPt.ys, calcPt.yh);
5356
}
5457

55-
var errorX = computeError(calcX, j, xObj);
58+
var errorX = computeErrorX(calcX, j);
5659
if(isNumeric(errorX[0]) && isNumeric(errorX[1])) {
5760
calcPt.xs = calcX - errorX[0];
5861
calcPt.xh = calcX + errorX[1];

src/components/errorbars/compute_error.js

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,64 @@
1111

1212

1313
/**
14-
* Compute the positive and negative error bar magnitudes.
14+
* Error bar computing function generator
1515
*
1616
* N.B. This function does not clean the dataPt entries, non-numeric
1717
* entries result in undefined *error*
1818
*
19-
* @param {numeric} dataPt data point from where to compute the error magnitue
20-
* @param {number} index index of dataPt in its corresponding data array
2119
* @param {object} opts error bar attributes
2220
*
23-
* @return {array of two numbers}
24-
* - error[0] : error magnitude in the negative direction
25-
* - error[1] : " " " " positive "
21+
* @return {function} :
22+
* @param {numeric} dataVal error magnitude in the negative direction
23+
* @param {number} index index of dataPt in its corresponding data array
24+
* @return {array}
25+
* - error[0] : error magnitude in the negative direction
26+
* - error[1] : " " " " positive "
2627
*/
27-
module.exports = function computeError(dataPt, index, opts) {
28+
module.exports = function makeComputeError(opts) {
2829
var type = opts.type,
29-
error = new Array(2);
30+
symmetric = opts.symmetric;
3031

3132
if(type === 'data') {
32-
error[1] = +(opts.array[index]);
33-
error[0] = (opts.symmetric || opts.arrayminus === undefined) ?
34-
error[1] :
35-
+(opts.arrayminus[index]);
33+
var array = opts.array,
34+
arrayminus = opts.arrayminus;
35+
36+
return (symmetric || arrayminus === undefined) ?
37+
function computeError(dataPt, index) {
38+
var val = +(array[index]);
39+
return [val, val];
40+
} :
41+
function computeError(dataPt, index) {
42+
return [+arrayminus[index], +array[index]];
43+
};
3644
}
3745
else {
38-
error[1] = getErrorVal(type, dataPt, opts.value);
39-
error[0] = (opts.symmetric || opts.valueminus === undefined) ?
40-
error[1] :
41-
getErrorVal(type, dataPt, opts.valueminus);
42-
}
46+
var value = opts.value,
47+
valueminus = opts.valueminus;
4348

44-
45-
return error;
49+
return (symmetric || valueminus === undefined) ?
50+
function computeError(dataPt) {
51+
var val = getErrorVal(type, dataPt, value);
52+
return [val, val];
53+
} :
54+
function computeError(dataPt) {
55+
return [
56+
getErrorVal(type, dataPt, valueminus),
57+
getErrorVal(type, dataPt, value)
58+
];
59+
};
60+
}
4661
};
4762

48-
// size the error bar itself (for all types except data)
63+
/**
64+
* Compute error bar magnitude (for all types except data)
65+
*
66+
* @param {string} type error bar type
67+
* @param {numeric} dataPt
68+
* data point from where to compute the error magnitude
69+
* @param {numeric} [value] error bar value
70+
*
71+
*/
4972
function getErrorVal(type, dataPt, value) {
5073
if(type === 'percent') return Math.abs(dataPt * value / 100);
5174
if(type === 'constant') return Math.abs(value);

src/traces/scatter3d/calc_errors.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99

1010
'use strict';
1111

12-
var computeError = require('../../components/errorbars/compute_error');
12+
var makeComputeError = require('../../components/errorbars/compute_error');
1313

1414

1515
function calculateAxisErrors(data, params, scaleFactor) {
1616
if(!params || !params.visible) return null;
1717

18+
var computeError = makeComputeError(params);
1819
var result = new Array(data.length);
1920

2021
for(var i = 0; i < data.length; i++) {
21-
var errors = computeError(+data[i], i, params);
22+
var errors = computeError(+data[i], i);
2223

2324
result[i] = [
2425
-errors[0] * scaleFactor,

0 commit comments

Comments
 (0)