Remove some dead data handler coder (#787)
[dygraphs.git] / src / datahandler / bars-fractions.js
... / ...
CommitLineData
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/*global Dygraph:false */
14"use strict";
15
16import BarsHandler from './bars';
17
18/**
19 * @constructor
20 * @extends Dygraph.DataHandlers.BarsHandler
21 */
22var FractionsBarsHandler = function() {
23};
24
25FractionsBarsHandler.prototype = new BarsHandler();
26
27/** @inheritDoc */
28FractionsBarsHandler.prototype.extractSeries = function(rawData, i, options) {
29 // TODO(danvk): pre-allocate series here.
30 var series = [];
31 var x, y, point, num, den, value, stddev, variance;
32 var mult = 100.0;
33 var sigma = options.get("sigma");
34 var logScale = options.get('logscale');
35 for ( var j = 0; j < rawData.length; j++) {
36 x = rawData[j][0];
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) {
42 point = null;
43 }
44 }
45 // Extract to the unified data format.
46 if (point !== null) {
47 num = point[0];
48 den = point[1];
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;
53 y = mult * value;
54 // preserve original values in extras for further filtering
55 series.push([ x, y, [ y - variance, y + variance, num, den ] ]);
56 } else {
57 series.push([ x, num, [ num, num, num, den ] ]);
58 }
59 } else {
60 series.push([ x, null, [ null, null, null, null ] ]);
61 }
62 }
63 return series;
64};
65
66/** @inheritDoc */
67FractionsBarsHandler.prototype.rollingAverage =
68 function(originalData, rollPeriod, options) {
69 rollPeriod = Math.min(rollPeriod, originalData.length);
70 var rollingData = [];
71 var sigma = options.get("sigma");
72 var wilsonInterval = options.get("wilsonInterval");
73
74 var low, high, i, stddev;
75 var num = 0;
76 var den = 0; // numerator/denominator
77 var mult = 100.0;
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];
84 }
85
86 var date = originalData[i][0];
87 var value = den ? num / den : 0.0;
88 if (wilsonInterval) {
89 // For more details on this confidence interval, see:
90 // http://en.wikipedia.org/wiki/Binomial_confidence_interval
91 if (den) {
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 ] ];
99 } else {
100 rollingData[i] = [ date, 0, [ 0, 0 ] ];
101 }
102 } else {
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) ] ];
106 }
107 }
108
109 return rollingData;
110};
111
112export default FractionsBarsHandler;