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