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 */
18 Dygraph
.DataHandlers
.FractionsBarsHandler
= Dygraph
.DataHandler();
19 var FractionsBarsHandler
= Dygraph
.DataHandlers
.FractionsBarsHandler
;
20 FractionsBarsHandler
.prototype = new Dygraph
.DataHandlers
.BarsHandler();
23 FractionsBarsHandler
.prototype.extractSeries
= function(rawData
, i
, options
) {
24 // TODO(danvk): pre-allocate series here.
26 var x
, y
, point
, num
, den
, value
, stddev
, variance
;
28 var sigma
= options
.get("sigma");
29 var logScale
= options
.get('logscale');
30 for ( var j
= 0; j
< rawData
.length
; j
++) {
32 point
= rawData
[j
][i
];
33 if (logScale
&& point
!== null) {
34 // On the log scale, points less than zero do not exist.
35 // This will create a gap in the chart.
36 if (point
[0] <= 0 || point
[1] <= 0) {
40 // Extract to the unified data format.
44 if (num
!== null && !isNaN(num
)) {
45 value
= den
? num
/ den
: 0.0;
46 stddev
= den
? sigma
* Math
.sqrt(value
* (1 - value
) / den
) : 1.0;
47 variance
= mult
* stddev
;
49 // preserve original values in extras for further filtering
50 series
.push([ x
, y
, [ y
- variance
, y
+ variance
, num
, den
] ]);
52 series
.push([ x
, num
, [ num
, num
, num
, den
] ]);
55 series
.push([ x
, null, [ null, null, null, null ] ]);
61 FractionsBarsHandler
.prototype.rollingAverage
= function(originalData
, rollPeriod
,
63 rollPeriod
= Math
.min(rollPeriod
, originalData
.length
);
65 var sigma
= options
.get("sigma");
66 var wilsonInterval
= options
.get("wilsonInterval");
68 var low
, high
, i
, stddev
;
70 var den
= 0; // numerator/denominator
72 for (i
= 0; i
< originalData
.length
; i
++) {
73 num
+= originalData
[i
][2][2];
74 den
+= originalData
[i
][2][3];
75 if (i
- rollPeriod
>= 0) {
76 num
-= originalData
[i
- rollPeriod
][2][2];
77 den
-= originalData
[i
- rollPeriod
][2][3];
80 var date
= originalData
[i
][0];
81 var value
= den
? num
/ den
: 0.0;
83 // For more details on this confidence interval, see:
84 // http://en.wikipedia.org/wiki
/Binomial_confidence_interval
86 var p
= value
< 0 ? 0 : value
, n
= den
;
87 var pm
= sigma
* Math
.sqrt(p
* (1 - p
) / n + sigma * sigma / (4 * n
* n
));
88 var denom
= 1 + sigma
* sigma
/ den
;
89 low
= (p
+ sigma
* sigma
/ (2 * den) - pm) / denom
;
90 high
= (p
+ sigma
* sigma
/ (2 * den) + pm) / denom
;
91 rollingData
[i
] = [ date
, p
* mult
,
92 [ low
* mult
, high
* mult
] ];
94 rollingData
[i
] = [ date
, 0, [ 0, 0 ] ];
97 stddev
= den
? sigma
* Math
.sqrt(value
* (1 - value
) / den
) : 1.0;
98 rollingData
[i
] = [ date
, mult
* value
,
99 [ mult
* (value
- stddev
), mult
* (value
+ stddev
) ] ];