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 combination
9 * of error bars and fractions options.
10 * @author David Eberlein (david.eberlein@ch.sauter-bc.com)
15 /*global Dygraph:false */
20 * @extends Dygraph.DataHandlers.BarsHandler
22 Dygraph
.DataHandlers
.FractionsBarsHandler
= function() {
25 var FractionsBarsHandler
= Dygraph
.DataHandlers
.FractionsBarsHandler
;
26 FractionsBarsHandler
.prototype = new Dygraph
.DataHandlers
.BarsHandler();
29 FractionsBarsHandler
.prototype.extractSeries
= function(rawData
, i
, options
) {
30 // TODO(danvk): pre-allocate series here.
32 var x
, y
, point
, num
, den
, value
, stddev
, variance
;
34 var sigma
= options
.get("sigma");
35 var logScale
= options
.get('logscale');
36 for ( var j
= 0; j
< rawData
.length
; j
++) {
38 point
= rawData
[j
][i
];
39 if (logScale
&& point
!== null) {
40 // On the log scale, points less than zero do not exist.
41 // This will create a gap in the chart.
42 if (point
[0] <= 0 || point
[1] <= 0) {
46 // Extract to the unified data format.
50 if (num
!== null && !isNaN(num
)) {
51 value
= den
? num
/ den
: 0.0;
52 stddev
= den
? sigma
* Math
.sqrt(value
* (1 - value
) / den
) : 1.0;
53 variance
= mult
* stddev
;
55 // preserve original values in extras for further filtering
56 series
.push([ x
, y
, [ y
- variance
, y
+ variance
, num
, den
] ]);
58 series
.push([ x
, num
, [ num
, num
, num
, den
] ]);
61 series
.push([ x
, null, [ null, null, null, null ] ]);
68 FractionsBarsHandler
.prototype.rollingAverage
=
69 function(originalData
, rollPeriod
, options
) {
70 rollPeriod
= Math
.min(rollPeriod
, originalData
.length
);
72 var sigma
= options
.get("sigma");
73 var wilsonInterval
= options
.get("wilsonInterval");
75 var low
, high
, i
, stddev
;
77 var den
= 0; // numerator/denominator
79 for (i
= 0; i
< originalData
.length
; i
++) {
80 num
+= originalData
[i
][2][2];
81 den
+= originalData
[i
][2][3];
82 if (i
- rollPeriod
>= 0) {
83 num
-= originalData
[i
- rollPeriod
][2][2];
84 den
-= originalData
[i
- rollPeriod
][2][3];
87 var date
= originalData
[i
][0];
88 var value
= den
? num
/ den
: 0.0;
90 // For more details on this confidence interval, see:
91 // http://en.wikipedia.org/wiki
/Binomial_confidence_interval
93 var p
= value
< 0 ? 0 : value
, n
= den
;
94 var pm
= sigma
* Math
.sqrt(p
* (1 - p
) / n + sigma * sigma / (4 * n
* n
));
95 var denom
= 1 + sigma
* sigma
/ den
;
96 low
= (p
+ sigma
* sigma
/ (2 * den) - pm) / denom
;
97 high
= (p
+ sigma
* sigma
/ (2 * den) + pm) / denom
;
98 rollingData
[i
] = [ date
, p
* mult
,
99 [ low
* mult
, high
* mult
] ];
101 rollingData
[i
] = [ date
, 0, [ 0, 0 ] ];
104 stddev
= den
? sigma
* Math
.sqrt(value
* (1 - value
) / den
) : 1.0;
105 rollingData
[i
] = [ date
, mult
* value
,
106 [ mult
* (value
- stddev
), mult
* (value
+ stddev
) ] ];