3 * Copyright 2013 David Eberlein (david.eberlein@ch.sauter-bc.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
8 * @fileoverview DataHandler implementation for the error bars option.
9 * @author David Eberlein (david.eberlein@ch.sauter-bc.com)
12 /*global Dygraph:false */
15 import BarsHandler from
'./bars';
19 * @extends BarsHandler
21 var ErrorBarsHandler
= function() {
24 ErrorBarsHandler
.prototype = new BarsHandler();
27 ErrorBarsHandler
.prototype.extractSeries
= function(rawData
, i
, options
) {
28 // TODO(danvk): pre-allocate series here.
30 var x
, y
, variance
, point
;
31 var sigma
= options
.get("sigma");
32 var logScale
= options
.get('logscale');
33 for ( var j
= 0; j
< rawData
.length
; j
++) {
35 point
= rawData
[j
][i
];
36 if (logScale
&& point
!== null) {
37 // On the log scale, points less than zero do not exist.
38 // This will create a gap in the chart.
39 if (point
[0] <= 0 || point
[0] - sigma
* point
[1] <= 0) {
43 // Extract to the unified data format.
46 if (y
!== null && !isNaN(y
)) {
47 variance
= sigma
* point
[1];
48 // preserve original error value in extras for further
50 series
.push([ x
, y
, [ y
- variance
, y
+ variance
, point
[1] ] ]);
52 series
.push([ x
, y
, [ y
, y
, y
] ]);
55 series
.push([ x
, null, [ null, null, null ] ]);
62 ErrorBarsHandler
.prototype.rollingAverage
=
63 function(originalData
, rollPeriod
, options
) {
64 rollPeriod
= Math
.min(rollPeriod
, originalData
.length
);
66 var sigma
= options
.get("sigma");
68 var i
, j
, y
, v
, sum
, num_ok
, stddev
, variance
, value
;
70 // Calculate the rolling average for the first rollPeriod - 1 points
71 // where there is not enough data to roll over the full number of points
72 for (i
= 0; i
< originalData
.length
; i
++) {
76 for (j
= Math
.max(0, i
- rollPeriod
+ 1); j
< i
+ 1; j
++) {
77 y
= originalData
[j
][1];
78 if (y
=== null || isNaN(y
))
82 variance
+= Math
.pow(originalData
[j
][2][2], 2);
85 stddev
= Math
.sqrt(variance
) / num_ok
;
87 rollingData
[i
] = [ originalData
[i
][0], value
,
88 [value
- sigma
* stddev
, value
+ sigma
* stddev
] ];
90 // This explicitly preserves NaNs to aid with "independent
92 // See testRollingAveragePreservesNaNs.
93 v
= (rollPeriod
== 1) ? originalData
[i
][1] : null;
94 rollingData
[i
] = [ originalData
[i
][0], v
, [ v
, v
] ];
101 export default ErrorBarsHandler
;