Merge pull request #565 from danvk/gulp
[dygraphs.git] / src / datahandler / bars-error.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 error bars option.
9 * @author David Eberlein (david.eberlein@ch.sauter-bc.com)
10 */
11
12 (function() {
13
14 /*global Dygraph:false */
15 "use strict";
16
17 /**
18 * @constructor
19 * @extends Dygraph.DataHandlers.BarsHandler
20 */
21 Dygraph.DataHandlers.ErrorBarsHandler = function() {
22 };
23
24 var ErrorBarsHandler = Dygraph.DataHandlers.ErrorBarsHandler;
25 ErrorBarsHandler.prototype = new Dygraph.DataHandlers.BarsHandler();
26
27 /** @inheritDoc */
28 ErrorBarsHandler.prototype.extractSeries = function(rawData, i, options) {
29 // TODO(danvk): pre-allocate series here.
30 var series = [];
31 var x, y, variance, point;
32 var sigma = options.get("sigma");
33 var logScale = options.get('logscale');
34 for ( var j = 0; j < rawData.length; j++) {
35 x = rawData[j][0];
36 point = rawData[j][i];
37 if (logScale && point !== null) {
38 // On the log scale, points less than zero do not exist.
39 // This will create a gap in the chart.
40 if (point[0] <= 0 || point[0] - sigma * point[1] <= 0) {
41 point = null;
42 }
43 }
44 // Extract to the unified data format.
45 if (point !== null) {
46 y = point[0];
47 if (y !== null && !isNaN(y)) {
48 variance = sigma * point[1];
49 // preserve original error value in extras for further
50 // filtering
51 series.push([ x, y, [ y - variance, y + variance, point[1] ] ]);
52 } else {
53 series.push([ x, y, [ y, y, y ] ]);
54 }
55 } else {
56 series.push([ x, null, [ null, null, null ] ]);
57 }
58 }
59 return series;
60 };
61
62 /** @inheritDoc */
63 ErrorBarsHandler.prototype.rollingAverage =
64 function(originalData, rollPeriod, options) {
65 rollPeriod = Math.min(rollPeriod, originalData.length);
66 var rollingData = [];
67 var sigma = options.get("sigma");
68
69 var i, j, y, v, sum, num_ok, stddev, variance, value;
70
71 // Calculate the rolling average for the first rollPeriod - 1 points
72 // where there is not enough data to roll over the full number of points
73 for (i = 0; i < originalData.length; i++) {
74 sum = 0;
75 variance = 0;
76 num_ok = 0;
77 for (j = Math.max(0, i - rollPeriod + 1); j < i + 1; j++) {
78 y = originalData[j][1];
79 if (y === null || isNaN(y))
80 continue;
81 num_ok++;
82 sum += y;
83 variance += Math.pow(originalData[j][2][2], 2);
84 }
85 if (num_ok) {
86 stddev = Math.sqrt(variance) / num_ok;
87 value = sum / num_ok;
88 rollingData[i] = [ originalData[i][0], value,
89 [value - sigma * stddev, value + sigma * stddev] ];
90 } else {
91 // This explicitly preserves NaNs to aid with "independent
92 // series".
93 // See testRollingAveragePreservesNaNs.
94 v = (rollPeriod == 1) ? originalData[i][1] : null;
95 rollingData[i] = [ originalData[i][0], v, [ v, v ] ];
96 }
97 }
98
99 return rollingData;
100 };
101
102 })();