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