lint
[dygraphs.git] / datahandler / bars-fractions.js
CommitLineData
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
18Dygraph.DataHandlers.FractionsBarsHandler = Dygraph.DataHandler();
19var FractionsBarsHandler = Dygraph.DataHandlers.FractionsBarsHandler;
20FractionsBarsHandler.prototype = new Dygraph.DataHandlers.BarsHandler();
21
22// errorBars
23FractionsBarsHandler.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;
a49c164a 38 }
3ea41d86
DV
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 ] ]);
a49c164a 51 } else {
3ea41d86 52 series.push([ x, num, [ num, num, num, den ] ]);
a49c164a 53 }
3ea41d86
DV
54 } else {
55 series.push([ x, null, [ null, null, null, null ] ]);
a49c164a 56 }
3ea41d86
DV
57 }
58 return series;
59};
a49c164a 60
3ea41d86
DV
61FractionsBarsHandler.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");
a49c164a 67
3ea41d86
DV
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 }
a49c164a 79
3ea41d86
DV
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 ] ];
a49c164a 93 } else {
3ea41d86 94 rollingData[i] = [ date, 0, [ 0, 0 ] ];
a49c164a 95 }
3ea41d86
DV
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) ] ];
a49c164a 100 }
3ea41d86
DV
101 }
102
103 return rollingData;
104};
a49c164a 105
a49c164a 106})();