using changedTouches instead of touches
[dygraphs.git] / src / datahandler / bars-error.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 error bars option.
9 * @author David Eberlein (david.eberlein@ch.sauter-bc.com)
10 */
11
12(function() {
a49c164a 13
3ea41d86
DV
14/*global Dygraph:false */
15"use strict";
16
749281f8
DV
17/**
18 * @constructor
19 * @extends Dygraph.DataHandlers.BarsHandler
20 */
21Dygraph.DataHandlers.ErrorBarsHandler = function() {
22};
23
3ea41d86
DV
24var ErrorBarsHandler = Dygraph.DataHandlers.ErrorBarsHandler;
25ErrorBarsHandler.prototype = new Dygraph.DataHandlers.BarsHandler();
26
749281f8 27/** @inheritDoc */
3ea41d86
DV
28ErrorBarsHandler.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;
a49c164a 42 }
3ea41d86
DV
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] ] ]);
a49c164a 52 } else {
3ea41d86 53 series.push([ x, y, [ y, y, y ] ]);
a49c164a 54 }
3ea41d86
DV
55 } else {
56 series.push([ x, null, [ null, null, null ] ]);
a49c164a 57 }
3ea41d86
DV
58 }
59 return series;
60};
a49c164a 61
749281f8
DV
62/** @inheritDoc */
63ErrorBarsHandler.prototype.rollingAverage =
64 function(originalData, rollPeriod, options) {
3ea41d86
DV
65 rollPeriod = Math.min(rollPeriod, originalData.length);
66 var rollingData = [];
67 var sigma = options.get("sigma");
a49c164a 68
3ea41d86 69 var i, j, y, v, sum, num_ok, stddev, variance, value;
a49c164a 70
3ea41d86
DV
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);
a49c164a 84 }
3ea41d86
DV
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};
a49c164a 101
a49c164a 102})();