1313/**
1414 * Error bar computing function generator
1515 *
16- * N.B. This function does not clean the dataPt entries, non -numeric
17- * entries result in undefined * error*
16+ * N.B. The generated function does not clean the dataPt entries. Non -numeric
17+ * entries result in undefined error magnitudes.
1818 *
1919 * @param {object } opts error bar attributes
2020 *
2121 * @return {function } :
22- * @param {numeric } dataVal error magnitude in the negative direction
22+ * @param {numeric } dataPt data point from where to compute the error magnitude
2323 * @param {number } index index of dataPt in its corresponding data array
2424 * @return {array }
2525 * - error[0] : error magnitude in the negative direction
@@ -46,20 +46,20 @@ module.exports = function makeComputeError(opts) {
4646 }
4747 }
4848 else {
49- var value = opts . value ,
50- valueminus = opts . valueminus ;
49+ var computeErrorValue = makeComputeErrorValue ( type , opts . value ) ,
50+ computeErrorValueMinus = makeComputeErrorValue ( type , opts . valueminus ) ;
5151
52- if ( symmetric || valueminus === undefined ) {
52+ if ( symmetric || opts . valueminus === undefined ) {
5353 return function computeError ( dataPt ) {
54- var val = getErrorVal ( type , dataPt , value ) ;
54+ var val = computeErrorValue ( dataPt ) ;
5555 return [ val , val ] ;
5656 } ;
5757 }
5858 else {
5959 return function computeError ( dataPt ) {
6060 return [
61- getErrorVal ( type , dataPt , valueminus ) ,
62- getErrorVal ( type , dataPt , value )
61+ computeErrorValueMinus ( dataPt ) ,
62+ computeErrorValue ( dataPt )
6363 ] ;
6464 } ;
6565 }
@@ -70,13 +70,25 @@ module.exports = function makeComputeError(opts) {
7070 * Compute error bar magnitude (for all types except data)
7171 *
7272 * @param {string } type error bar type
73- * @param {numeric } dataPt
74- * data point from where to compute the error magnitude
75- * @param {numeric } [value] error bar value
73+ * @param {numeric } value error bar value
7674 *
75+ * @return {function } :
76+ * @param {numeric } dataPt
7777 */
78- function getErrorVal ( type , dataPt , value ) {
79- if ( type === 'percent' ) return Math . abs ( dataPt * value / 100 ) ;
80- if ( type === 'constant' ) return Math . abs ( value ) ;
81- if ( type === 'sqrt' ) return Math . sqrt ( Math . abs ( dataPt ) ) ;
78+ function makeComputeErrorValue ( type , value ) {
79+ if ( type === 'percent' ) {
80+ return function ( dataPt ) {
81+ return Math . abs ( dataPt * value / 100 ) ;
82+ } ;
83+ }
84+ if ( type === 'constant' ) {
85+ return function ( ) {
86+ return Math . abs ( value ) ;
87+ } ;
88+ }
89+ if ( type === 'sqrt' ) {
90+ return function ( dataPt ) {
91+ return Math . sqrt ( Math . abs ( dataPt ) ) ;
92+ } ;
93+ }
8294}
0 commit comments