-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Some error bar clean ups #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
ae66e10
0a8d444
77ab003
a27cf08
460ee0c
b66b620
3c21918
007b8d1
2d8ed7f
9d5f143
66ce3fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Copyright 2012-2015, 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 Plots = require('../../plots/plots'); | ||
var Axes = require('../../plots/cartesian/axes'); | ||
|
||
var compureError = require('./compute_error'); | ||
|
||
|
||
module.exports = function calc(gd) { | ||
var calcdata = gd.calcdata; | ||
|
||
for(var i = 0; i < calcdata.length; i++) { | ||
var calcTrace = calcdata[i], | ||
trace = calcTrace[0].trace; | ||
|
||
if(!Plots.traceIs(trace, 'errorBarsOK')) continue; | ||
|
||
var xObj = trace.error_x || {}, | ||
yObj = trace.error_y || {}, | ||
xa = Axes.getFromId(gd, trace.xaxis), | ||
ya = Axes.getFromId(gd, trace.yaxis), | ||
xVis = xObj.visible && ['linear', 'log'].indexOf(xa.type)!==-1, | ||
yVis = yObj.visible && ['linear', 'log'].indexOf(ya.type)!==-1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spacing between |
||
|
||
if(!xVis && !yVis) continue; | ||
|
||
var xVals = [], | ||
yVals = []; | ||
|
||
for(var j = 0; j < calcTrace.length; j++) { | ||
var calcPt = calcTrace[j], | ||
calcX = calcPt.x, | ||
calcY = calcPt.y; | ||
|
||
if(!isNumeric(ya.c2l(calcY)) || !isNumeric(xa.c2l(calcX))) continue; | ||
|
||
var errorY = compureError(calcY, j, yObj); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. compureError here too |
||
if(isNumeric(errorY[0]) && isNumeric(errorY[1])) { | ||
calcPt.ys = calcY - errorY[0]; | ||
calcPt.yh = calcY + errorY[1]; | ||
yVals.push(calcPt.ys, calcPt.yh); | ||
} | ||
|
||
var errorX = compureError(calcX, j, xObj); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here. |
||
if(isNumeric(errorX[0]) && isNumeric(errorX[1])) { | ||
calcPt.xs = calcX - errorX[0]; | ||
calcPt.xh = calcX + errorX[1]; | ||
xVals.push(calcPt.xs, calcPt.xh); | ||
} | ||
} | ||
|
||
Axes.expand(ya, yVals, {padded: true}); | ||
Axes.expand(xa, xVals, {padded: true}); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Copyright 2012-2015, 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'; | ||
|
||
|
||
/** | ||
* Compute the positive and negative error bar magnitudes. | ||
* | ||
* N.B. This function does not clean the dataPt entries, non-numeric | ||
* entries result in undefined *error* | ||
* | ||
* @param {numeric} dataPt data point from where to compute the error magnitue | ||
* @param {number} index index of dataPt in its corresponding data array | ||
* @param {object} opts error bar attributes | ||
* | ||
* @return {array of two numbers} | ||
* - error[0] : error magnitude in the negative direction | ||
* - error[1] : " " " " positive " | ||
*/ | ||
module.exports = function computeError(dataPt, index, opts) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reusable error bar data/constant/sqrt/percent logic There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good to me. Don't need to do it now, but it occurs to me we could boost performance by just evaluating
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good call. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all about the 🐎 |
||
var type = opts.type, | ||
error = new Array(2); | ||
|
||
if(type === 'data') { | ||
error[1] = +(opts.array[index]); | ||
error[0] = (opts.symmetric || opts.arrayminus === undefined) ? | ||
error[1] : | ||
+(opts.arrayminus[index]); | ||
} | ||
else { | ||
error[1] = getErrorVal(type, dataPt, opts.value); | ||
error[0] = (opts.symmetric || opts.valueminus === undefined) ? | ||
error[1] : | ||
getErrorVal(type, dataPt, opts.valueminus); | ||
} | ||
|
||
|
||
return error; | ||
}; | ||
|
||
// size the error bar itself (for all types except data) | ||
function getErrorVal(type, dataPt, value) { | ||
if(type === 'percent') return Math.abs(dataPt * value / 100); | ||
if(type === 'constant') return Math.abs(value); | ||
if(type === 'sqrt') return Math.sqrt(Math.abs(dataPt)); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* Copyright 2012-2015, 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 Plots = require('../../plots/plots'); | ||
var Lib = require('../../lib'); | ||
|
||
var attributes = require('./attributes'); | ||
|
||
|
||
module.exports = function(traceIn, traceOut, defaultColor, opts) { | ||
var objName = 'error_' + opts.axis, | ||
containerOut = traceOut[objName] = {}, | ||
containerIn = traceIn[objName] || {}; | ||
|
||
function coerce (attr, dflt) { | ||
return Lib.coerce(containerIn, containerOut, attributes, attr, dflt); | ||
} | ||
|
||
var hasErrorBars = ( | ||
containerIn.array !== undefined || | ||
containerIn.value !== undefined || | ||
containerIn.type === 'sqrt' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
); | ||
|
||
var visible = coerce('visible', hasErrorBars); | ||
|
||
if(visible === false) return; | ||
|
||
var type = coerce('type', 'array' in containerIn ? 'data' : 'percent'), | ||
symmetric = true; | ||
|
||
if(type !== 'sqrt') { | ||
symmetric = coerce('symmetric', | ||
!((type === 'data' ? 'arrayminus' : 'valueminus') in containerIn)); | ||
} | ||
|
||
if(type === 'data') { | ||
var array = coerce('array'); | ||
if(!array) containerOut.array = []; | ||
coerce('traceref'); | ||
if(!symmetric) { | ||
var arrayminus = coerce('arrayminus'); | ||
if(!arrayminus) containerOut.arrayminus = []; | ||
coerce('tracerefminus'); | ||
} | ||
} | ||
else if(type==='percent' || type==='constant') { | ||
coerce('value'); | ||
if(!symmetric) coerce('valueminus'); | ||
} | ||
|
||
var copyAttr = 'copy_'+opts.inherit+'style'; | ||
if(opts.inherit) { | ||
var inheritObj = traceOut['error_' + opts.inherit]; | ||
if((inheritObj||{}).visible) { | ||
coerce(copyAttr, !(containerIn.color || | ||
isNumeric(containerIn.thickness) || | ||
isNumeric(containerIn.width))); | ||
} | ||
} | ||
if(!opts.inherit || !containerOut[copyAttr]) { | ||
coerce('color', defaultColor); | ||
coerce('thickness'); | ||
coerce('width', Plots.traceIs(traceOut, 'gl3d') ? 0 : 4); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
computeError
perhaps?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice catch. Thanks