Skip to content

Commit 66ce3fa

Browse files
committed
split error bar calc step into a per-axis function
1 parent 9d5f143 commit 66ce3fa

File tree

1 file changed

+30
-38
lines changed

1 file changed

+30
-38
lines changed

src/components/errorbars/calc.js

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,44 +26,36 @@ module.exports = function calc(gd) {
2626

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

29-
var xOpts = trace.error_x || {},
30-
yOpts = trace.error_y || {},
31-
xa = Axes.getFromId(gd, trace.xaxis),
32-
ya = Axes.getFromId(gd, trace.yaxis),
33-
xVis = (xOpts.visible && ['linear', 'log'].indexOf(xa.type) !== -1),
34-
yVis = (yOpts.visible && ['linear', 'log'].indexOf(ya.type) !== -1);
35-
36-
if(!xVis && !yVis) continue;
37-
38-
var xVals = [],
39-
yVals = [];
40-
41-
var computeErrorY = makeComputeError(yOpts),
42-
computeErrorX = makeComputeError(xOpts);
43-
44-
for(var j = 0; j < calcTrace.length; j++) {
45-
var calcPt = calcTrace[j],
46-
calcY = calcPt.y,
47-
calcX = calcPt.x;
48-
49-
if(!isNumeric(ya.c2l(calcY)) || !isNumeric(xa.c2l(calcX))) continue;
50-
51-
var errorY = computeErrorY(calcY, j);
52-
if(isNumeric(errorY[0]) && isNumeric(errorY[1])) {
53-
calcPt.ys = calcY - errorY[0];
54-
calcPt.yh = calcY + errorY[1];
55-
yVals.push(calcPt.ys, calcPt.yh);
56-
}
57-
58-
var errorX = computeErrorX(calcX, j);
59-
if(isNumeric(errorX[0]) && isNumeric(errorX[1])) {
60-
calcPt.xs = calcX - errorX[0];
61-
calcPt.xh = calcX + errorX[1];
62-
xVals.push(calcPt.xs, calcPt.xh);
63-
}
64-
}
29+
var xa = Axes.getFromId(gd, trace.xaxis),
30+
ya = Axes.getFromId(gd, trace.yaxis);
6531

66-
Axes.expand(ya, yVals, {padded: true});
67-
Axes.expand(xa, xVals, {padded: true});
32+
calcOneAxis(calcTrace, trace, xa, 'x');
33+
calcOneAxis(calcTrace, trace, ya, 'y');
6834
}
6935
};
36+
37+
function calcOneAxis(calcTrace, trace, axis, coord) {
38+
var opts = trace['error_' + coord] || {},
39+
isVisible = (opts.visible && ['linear', 'log'].indexOf(axis.type) !== -1),
40+
vals = [];
41+
42+
if(!isVisible) return;
43+
44+
var computeError = makeComputeError(opts);
45+
46+
for(var i = 0; i < calcTrace.length; i++) {
47+
var calcPt = calcTrace[i],
48+
calcCoord = calcPt[coord];
49+
50+
if(!isNumeric(axis.c2l(calcCoord))) continue;
51+
52+
var errors = computeError(calcCoord, i);
53+
if(isNumeric(errors[0]) && isNumeric(errors[1])) {
54+
var shoe = calcPt[coord + 's'] = calcCoord - errors[0],
55+
hat = calcPt[coord + 'h'] = calcCoord + errors[1];
56+
vals.push(shoe, hat);
57+
}
58+
}
59+
60+
Axes.expand(axis, vals, {padded: true});
61+
}

0 commit comments

Comments
 (0)