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)
14 /*global Dygraph:false */
17 Dygraph
.DataHandlers
.ErrorBarsHandler
= Dygraph
.DataHandler();
18 var ErrorBarsHandler
= Dygraph
.DataHandlers
.ErrorBarsHandler
;
19 ErrorBarsHandler
.prototype = new Dygraph
.DataHandlers
.BarsHandler();
22 ErrorBarsHandler
.prototype.extractSeries
= function(rawData
, i
, options
) {
23 // TODO(danvk): pre-allocate series here.
25 var x
, y
, variance
, point
;
26 var sigma
= options
.get("sigma");
27 var logScale
= options
.get('logscale');
28 for ( var j
= 0; j
< rawData
.length
; j
++) {
30 point
= rawData
[j
][i
];
31 if (logScale
&& point
!== null) {
32 // On the log scale, points less than zero do not exist.
33 // This will create a gap in the chart.
34 if (point
[0] <= 0 || point
[0] - sigma
* point
[1] <= 0) {
38 // Extract to the unified data format.
41 if (y
!== null && !isNaN(y
)) {
42 variance
= sigma
* point
[1];
43 // preserve original error value in extras for further
45 series
.push([ x
, y
, [ y
- variance
, y
+ variance
, point
[1] ] ]);
47 series
.push([ x
, y
, [ y
, y
, y
] ]);
50 series
.push([ x
, null, [ null, null, null ] ]);
56 ErrorBarsHandler
.prototype.rollingAverage
= function(originalData
, rollPeriod
,
58 rollPeriod
= Math
.min(rollPeriod
, originalData
.length
);
60 var sigma
= options
.get("sigma");
62 var i
, j
, y
, v
, sum
, num_ok
, stddev
, variance
, value
;
64 // Calculate the rolling average for the first rollPeriod - 1 points
65 // where there is not enough data to roll over the full number of points
66 for (i
= 0; i
< originalData
.length
; i
++) {
70 for (j
= Math
.max(0, i
- rollPeriod
+ 1); j
< i
+ 1; j
++) {
71 y
= originalData
[j
][1];
72 if (y
=== null || isNaN(y
))
76 variance
+= Math
.pow(originalData
[j
][2][2], 2);
79 stddev
= Math
.sqrt(variance
) / num_ok
;
81 rollingData
[i
] = [ originalData
[i
][0], value
,
82 [value
- sigma
* stddev
, value
+ sigma
* stddev
] ];
84 // This explicitly preserves NaNs to aid with "independent
86 // See testRollingAveragePreservesNaNs.
87 v
= (rollPeriod
== 1) ? originalData
[i
][1] : null;
88 rollingData
[i
] = [ originalData
[i
][0], v
, [ v
, v
] ];