Merge pull request #271 from danvk/connect-separated-points
[dygraphs.git] / datahandler / bars-fractions.js
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() {
14
15 /*global Dygraph:false */
16 "use strict";
17
18 Dygraph.DataHandlers.FractionsBarsHandler = Dygraph.DataHandler();
19 var FractionsBarsHandler = Dygraph.DataHandlers.FractionsBarsHandler;
20 FractionsBarsHandler.prototype = new Dygraph.DataHandlers.BarsHandler();
21
22 // errorBars
23 FractionsBarsHandler.prototype.extractSeries = function(rawData, i, options) {
24 // TODO(danvk): pre-allocate series here.
25 var series = [];
26 var x, y, point, num, den, value, stddev, variance;
27 var mult = 100.0;
28 var sigma = options.get("sigma");
29 var logScale = options.get('logscale');
30 for ( var j = 0; j < rawData.length; j++) {
31 x = rawData[j][0];
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) {
37 point = null;
38 }
39 }
40 // Extract to the unified data format.
41 if (point !== null) {
42 num = point[0];
43 den = point[1];
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;
48 y = mult * value;
49 // preserve original values in extras for further filtering
50 series.push([ x, y, [ y - variance, y + variance, num, den ] ]);
51 } else {
52 series.push([ x, num, [ num, num, num, den ] ]);
53 }
54 } else {
55 series.push([ x, null, [ null, null, null, null ] ]);
56 }
57 }
58 return series;
59 };
60
61 FractionsBarsHandler.prototype.rollingAverage = function(originalData, rollPeriod,
62 options) {
63 rollPeriod = Math.min(rollPeriod, originalData.length);
64 var rollingData = [];
65 var sigma = options.get("sigma");
66 var wilsonInterval = options.get("wilsonInterval");
67
68 var low, high, i, stddev;
69 var num = 0;
70 var den = 0; // numerator/denominator
71 var mult = 100.0;
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];
78 }
79
80 var date = originalData[i][0];
81 var value = den ? num / den : 0.0;
82 if (wilsonInterval) {
83 // For more details on this confidence interval, see:
84 // http://en.wikipedia.org/wiki/Binomial_confidence_interval
85 if (den) {
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 ] ];
93 } else {
94 rollingData[i] = [ date, 0, [ 0, 0 ] ];
95 }
96 } else {
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) ] ];
100 }
101 }
102
103 return rollingData;
104 };
105
106 })();