Skip to content

Commit a27cf08

Browse files
committed
split error bar calc step into calc.js and compute_error.js :
- compute_error is the reusable piece of logic that gl3d and gl2d error bars will use.
1 parent 77ab003 commit a27cf08

File tree

3 files changed

+120
-63
lines changed

3 files changed

+120
-63
lines changed

src/components/errorbars/calc.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright 2012-2015, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
var isNumeric = require('fast-isnumeric');
13+
14+
var Plots = require('../../plots/plots');
15+
var Axes = require('../../plots/cartesian/axes');
16+
17+
var compureError = require('./compute_error');
18+
19+
20+
module.exports = function calc(gd) {
21+
var calcdata = gd.calcdata;
22+
23+
for(var i = 0; i < calcdata.length; i++) {
24+
var calcTrace = calcdata[i],
25+
trace = calcTrace[0].trace;
26+
27+
if(!Plots.traceIs(trace, 'errorBarsOK')) continue;
28+
29+
var xObj = trace.error_x || {},
30+
yObj = trace.error_y || {},
31+
xa = Axes.getFromId(gd, trace.xaxis),
32+
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;
35+
36+
if(!xVis && !yVis) continue;
37+
38+
var xVals = [],
39+
yVals = [];
40+
41+
for(var j = 0; j < calcTrace.length; j++) {
42+
var calcPt = calcTrace[j],
43+
calcX = calcPt.x,
44+
calcY = calcPt.y;
45+
46+
if(!isNumeric(ya.c2l(calcY)) || !isNumeric(xa.c2l(calcX))) continue;
47+
48+
var errorY = compureError(calcY, j, yObj);
49+
if(isNumeric(errorY[0]) && isNumeric(errorY[1])) {
50+
calcPt.ys = calcY - errorY[0];
51+
calcPt.yh = calcY + errorY[1];
52+
yVals.push(calcPt.ys, calcPt.yh);
53+
}
54+
55+
var errorX = compureError(calcX, j, xObj);
56+
if(isNumeric(errorX[0]) && isNumeric(errorX[1])) {
57+
calcPt.xs = calcX - errorX[0];
58+
calcPt.xh = calcX + errorX[1];
59+
xVals.push(calcPt.xs, calcPt.xh);
60+
}
61+
}
62+
63+
Axes.expand(ya, yVals, {padded: true});
64+
Axes.expand(xa, xVals, {padded: true});
65+
}
66+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright 2012-2015, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
13+
/**
14+
* Compute the positive and negative error bar magnitudes.
15+
*
16+
* N.B. This function does not clean the dataPt entries, non-numeric
17+
* entries result in undefined *error*
18+
*
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
21+
* @param {object} opts error bar attributes
22+
*
23+
* @return {array of two numbers}
24+
* - error[0] : error magnitude in the negative direction
25+
* - error[1] : " " " " positive "
26+
*/
27+
module.exports = function computeError(dataPt, index, opts) {
28+
var type = opts.type,
29+
error = new Array(2);
30+
31+
if(type === 'data') {
32+
error[1] = +(opts.array[index]);
33+
error[0] = (opts.symmetric || opts.arrayminus === undefined) ?
34+
error[1] :
35+
+(opts.arrayminus[index]);
36+
}
37+
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+
}
43+
44+
45+
return error;
46+
};
47+
48+
// size the error bar itself (for all types except data)
49+
function getErrorVal(type, dataPt, value) {
50+
if(type === 'percent') return Math.abs(dataPt * value / 100);
51+
if(type === 'constant') return Math.abs(value);
52+
if(type === 'sqrt') return Math.sqrt(Math.abs(dataPt));
53+
}

src/components/errorbars/index.js

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,69 +19,7 @@ errorBars.attributes = require('./attributes');
1919

2020
errorBars.supplyDefaults = require('./defaults');
2121

22-
// size the error bar itself (for all types except data)
23-
function errorval(type, dataval, errval) {
24-
if(type === 'percent') return Math.abs(dataval * errval / 100);
25-
if(type === 'constant') return Math.abs(errval);
26-
if(type === 'sqrt') return Math.sqrt(Math.abs(dataval));
27-
28-
return 0;
29-
}
30-
31-
errorBars.calc = function(gd) {
32-
(gd.calcdata||[]).forEach(function(cdi){
33-
var trace = cdi[0].trace;
34-
35-
if(!Plotly.Plots.traceIs(trace, 'errorBarsOK')) return;
36-
37-
var xObj = trace.error_x || {},
38-
yObj = trace.error_y || {},
39-
xa = Plotly.Axes.getFromId(gd, trace.xaxis),
40-
ya = Plotly.Axes.getFromId(gd, trace.yaxis),
41-
xvis = xObj.visible && ['linear', 'log'].indexOf(xa.type)!==-1,
42-
yvis = yObj.visible && ['linear', 'log'].indexOf(ya.type)!==-1;
43-
44-
if(!xvis && !yvis) return;
45-
46-
var xvals = [],
47-
yvals = [];
48-
49-
cdi.forEach(function(d,j) {
50-
try {
51-
if(isNumeric(ya.c2l(d.y)) && isNumeric(xa.c2l(d.x))){
52-
[
53-
{letter:'x', obj: xObj, visible: xvis, vals: xvals},
54-
{letter:'y', obj: yObj, visible: yvis, vals: yvals}
55-
].forEach(function(o){
56-
if(o.visible) {
57-
var dataVal = d[o.letter],
58-
obj = o.obj,
59-
ep, en;
60-
if(o.obj.type==='data') {
61-
ep = Number(obj.array[j]);
62-
en = (obj.symmetric || !('arrayminus' in obj)) ?
63-
ep : Number(obj.arrayminus[j]);
64-
}
65-
else {
66-
ep = errorval(obj.type, dataVal, obj.value);
67-
en = (obj.symmetric || !('valueminus' in obj)) ?
68-
ep : errorval(obj.type, dataVal, obj.valueminus);
69-
}
70-
if(isNumeric(ep) && isNumeric(en)) {
71-
var shoe = d[o.letter + 'h'] = dataVal + ep;
72-
var hat = d[o.letter + 's'] = dataVal - en;
73-
o.vals.push(shoe, hat);
74-
}
75-
}
76-
});
77-
}
78-
}
79-
catch(e) { console.log(e); }
80-
});
81-
Plotly.Axes.expand(ya, yvals, {padded: true});
82-
Plotly.Axes.expand(xa, xvals, {padded: true});
83-
});
84-
};
22+
errorBars.calc = require('./calc');
8523

8624
errorBars.calcFromTrace = function(trace, layout) {
8725
var x = trace.x || [],

0 commit comments

Comments
 (0)