typos
[dygraphs.git] / datahandler / bars-custom.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 custom 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 Dygraph.DataHandlers.CustomBarsHandler = Dygraph.DataHandler();
18 var CustomBarsHandler = Dygraph.DataHandlers.CustomBarsHandler;
19 CustomBarsHandler.prototype = new Dygraph.DataHandlers.BarsHandler();
20
21 // customBars
22 CustomBarsHandler.prototype.extractSeries = function(rawData, i, options) {
23 // TODO(danvk): pre-allocate series here.
24 var series = [];
25 var x, y, point;
26 var logScale = options.get('logscale');
27 for ( var j = 0; j < rawData.length; j++) {
28 x = rawData[j][0];
29 point = rawData[j][i];
30 if (logScale && point !== null) {
31 // On the log scale, points less than zero do not exist.
32 // This will create a gap in the chart.
33 if (point[0] <= 0 || point[1] <= 0 || point[2] <= 0) {
34 point = null;
35 }
36 }
37 // Extract to the unified data format.
38 if (point !== null) {
39 y = point[1];
40 if (y !== null && !isNaN(y)) {
41 series.push([ x, y, [ point[0], point[2] ] ]);
42 } else {
43 series.push([ x, y, [ y, y ] ]);
44 }
45 } else {
46 series.push([ x, null, [ null, null ] ]);
47 }
48 }
49 return series;
50 };
51
52 CustomBarsHandler.prototype.rollingAverage = function(originalData, rollPeriod,
53 options) {
54 rollPeriod = Math.min(rollPeriod, originalData.length);
55 var rollingData = [];
56 var y, low, high, mid,count, i, extremes;
57
58 low = 0;
59 mid = 0;
60 high = 0;
61 count = 0;
62 for (i = 0; i < originalData.length; i++) {
63 y = originalData[i][1];
64 extremes = originalData[i][2];
65 rollingData[i] = originalData[i];
66
67 if (y !== null && !isNaN(y)) {
68 low += extremes[0];
69 mid += y;
70 high += extremes[1];
71 count += 1;
72 }
73 if (i - rollPeriod >= 0) {
74 var prev = originalData[i - rollPeriod];
75 if (prev[1] !== null && !isNaN(prev[1])) {
76 low -= prev[2][0];
77 mid -= prev[1];
78 high -= prev[2][1];
79 count -= 1;
80 }
81 }
82 if (count) {
83 rollingData[i] = [
84 originalData[i][0],
85 1.0 * mid / count,
86 [ 1.0 * low / count,
87 1.0 * high / count ] ];
88 } else {
89 rollingData[i] = [ originalData[i][0], null, [ null, null ] ];
90 }
91 }
92
93 return rollingData;
94 };
95
96 })();