Commit | Line | Data |
---|---|---|
a49c164a DE |
1 | /** |
2 | * @license | |
3 | * Copyright 2013 David Eberlein (david.eberlein@ch.sauter-bc.com) | |
4 | * MIT-licensed (http://opensource.org/licenses/MIT) | |
5 | */ | |
6 | ||
7 | /** | |
8 | * @fileoverview DataHandler implementation for the combination | |
9 | * of error bars and fractions options. | |
10 | * @author David Eberlein (david.eberlein@ch.sauter-bc.com) | |
11 | */ | |
12 | ||
13 | (function() { | |
a49c164a | 14 | |
3ea41d86 DV |
15 | /*global Dygraph:false */ |
16 | "use strict"; | |
17 | ||
749281f8 DV |
18 | /** |
19 | * @constructor | |
20 | * @extends Dygraph.DataHandlers.BarsHandler | |
21 | */ | |
22 | Dygraph.DataHandlers.FractionsBarsHandler = function() { | |
23 | }; | |
24 | ||
3ea41d86 DV |
25 | var FractionsBarsHandler = Dygraph.DataHandlers.FractionsBarsHandler; |
26 | FractionsBarsHandler.prototype = new Dygraph.DataHandlers.BarsHandler(); | |
27 | ||
749281f8 | 28 | /** @inheritDoc */ |
3ea41d86 DV |
29 | FractionsBarsHandler.prototype.extractSeries = function(rawData, i, options) { |
30 | // TODO(danvk): pre-allocate series here. | |
31 | var series = []; | |
32 | var x, y, point, num, den, value, stddev, variance; | |
33 | var mult = 100.0; | |
34 | var sigma = options.get("sigma"); | |
35 | var logScale = options.get('logscale'); | |
36 | for ( var j = 0; j < rawData.length; j++) { | |
37 | x = rawData[j][0]; | |
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) { | |
43 | point = null; | |
a49c164a | 44 | } |
3ea41d86 DV |
45 | } |
46 | // Extract to the unified data format. | |
47 | if (point !== null) { | |
48 | num = point[0]; | |
49 | den = point[1]; | |
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; | |
54 | y = mult * value; | |
55 | // preserve original values in extras for further filtering | |
56 | series.push([ x, y, [ y - variance, y + variance, num, den ] ]); | |
a49c164a | 57 | } else { |
3ea41d86 | 58 | series.push([ x, num, [ num, num, num, den ] ]); |
a49c164a | 59 | } |
3ea41d86 DV |
60 | } else { |
61 | series.push([ x, null, [ null, null, null, null ] ]); | |
a49c164a | 62 | } |
3ea41d86 DV |
63 | } |
64 | return series; | |
65 | }; | |
a49c164a | 66 | |
749281f8 DV |
67 | /** @inheritDoc */ |
68 | FractionsBarsHandler.prototype.rollingAverage = | |
69 | function(originalData, rollPeriod, options) { | |
3ea41d86 DV |
70 | rollPeriod = Math.min(rollPeriod, originalData.length); |
71 | var rollingData = []; | |
72 | var sigma = options.get("sigma"); | |
73 | var wilsonInterval = options.get("wilsonInterval"); | |
a49c164a | 74 | |
3ea41d86 DV |
75 | var low, high, i, stddev; |
76 | var num = 0; | |
77 | var den = 0; // numerator/denominator | |
78 | var mult = 100.0; | |
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]; | |
85 | } | |
a49c164a | 86 | |
3ea41d86 DV |
87 | var date = originalData[i][0]; |
88 | var value = den ? num / den : 0.0; | |
89 | if (wilsonInterval) { | |
90 | // For more details on this confidence interval, see: | |
91 | // http://en.wikipedia.org/wiki/Binomial_confidence_interval | |
92 | if (den) { | |
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 ] ]; | |
a49c164a | 100 | } else { |
3ea41d86 | 101 | rollingData[i] = [ date, 0, [ 0, 0 ] ]; |
a49c164a | 102 | } |
3ea41d86 DV |
103 | } else { |
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) ] ]; | |
a49c164a | 107 | } |
3ea41d86 DV |
108 | } |
109 | ||
110 | return rollingData; | |
111 | }; | |
a49c164a | 112 | |
a49c164a | 113 | })(); |