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)
13 /*global Dygraph:false */
16 import BarsHandler from
'./bars';
20 * @extends Dygraph.DataHandlers.BarsHandler
22 var FractionsBarsHandler
= function() {
25 FractionsBarsHandler
.prototype = new BarsHandler();
28 FractionsBarsHandler
.prototype.extractSeries
= function(rawData
, i
, options
) {
29 // TODO(danvk): pre-allocate series here.
31 var x
, y
, point
, num
, den
, value
, stddev
, variance
;
33 var sigma
= options
.get("sigma");
34 var logScale
= options
.get('logscale');
35 for ( var j
= 0; j
< rawData
.length
; j
++) {
37 point
= rawData
[j
][i
];
38 if (logScale
&& point
!== null) {
39 // On the log scale, points less than zero do not exist.
40 // This will create a gap in the chart.
41 if (point
[0] <= 0 || point
[1] <= 0) {
45 // Extract to the unified data format.
49 if (num
!== null && !isNaN(num
)) {
50 value
= den
? num
/ den
: 0.0;
51 stddev
= den
? sigma
* Math
.sqrt(value
* (1 - value
) / den
) : 1.0;
52 variance
= mult
* stddev
;
54 // preserve original values in extras for further filtering
55 series
.push([ x
, y
, [ y
- variance
, y
+ variance
, num
, den
] ]);
57 series
.push([ x
, num
, [ num
, num
, num
, den
] ]);
60 series
.push([ x
, null, [ null, null, null, null ] ]);
67 FractionsBarsHandler
.prototype.rollingAverage
=
68 function(originalData
, rollPeriod
, options
) {
69 rollPeriod
= Math
.min(rollPeriod
, originalData
.length
);
71 var sigma
= options
.get("sigma");
72 var wilsonInterval
= options
.get("wilsonInterval");
74 var low
, high
, i
, stddev
;
76 var den
= 0; // numerator/denominator
78 for (i
= 0; i
< originalData
.length
; i
++) {
79 num
+= originalData
[i
][2][2];
80 den
+= originalData
[i
][2][3];
81 if (i
- rollPeriod
>= 0) {
82 num
-= originalData
[i
- rollPeriod
][2][2];
83 den
-= originalData
[i
- rollPeriod
][2][3];
86 var date
= originalData
[i
][0];
87 var value
= den
? num
/ den
: 0.0;
89 // For more details on this confidence interval, see:
90 // http://en.wikipedia.org/wiki
/Binomial_confidence_interval
92 var p
= value
< 0 ? 0 : value
, n
= den
;
93 var pm
= sigma
* Math
.sqrt(p
* (1 - p
) / n + sigma * sigma / (4 * n
* n
));
94 var denom
= 1 + sigma
* sigma
/ den
;
95 low
= (p
+ sigma
* sigma
/ (2 * den) - pm) / denom
;
96 high
= (p
+ sigma
* sigma
/ (2 * den) + pm) / denom
;
97 rollingData
[i
] = [ date
, p
* mult
,
98 [ low
* mult
, high
* mult
] ];
100 rollingData
[i
] = [ date
, 0, [ 0, 0 ] ];
103 stddev
= den
? sigma
* Math
.sqrt(value
* (1 - value
) / den
) : 1.0;
104 rollingData
[i
] = [ date
, mult
* value
,
105 [ mult
* (value
- stddev
), mult
* (value
+ stddev
) ] ];
112 export default FractionsBarsHandler
;