/
default.js
99 lines (87 loc) · 2.39 KB
/
default.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* @license
* Copyright 2013 David Eberlein (david.eberlein@ch.sauter-bc.com)
* MIT-licensed (http://opensource.org/licenses/MIT)
*/
/**
* @fileoverview DataHandler default implementation used for simple line charts.
* @author David Eberlein (david.eberlein@ch.sauter-bc.com)
*/
/*global Dygraph:false */
"use strict";
import DygraphDataHandler from './datahandler';
/**
* @constructor
* @extends Dygraph.DataHandler
*/
var DefaultHandler = function() {
};
DefaultHandler.prototype = new DygraphDataHandler();
/** @inheritDoc */
DefaultHandler.prototype.extractSeries = function(rawData, i, options) {
// TODO(danvk): pre-allocate series here.
var series = [];
var logScale = options.get('logscale');
for ( var j = 0; j < rawData.length; j++) {
var x = rawData[j][0];
var point = rawData[j][i];
if (logScale) {
// On the log scale, points less than zero do not exist.
// This will create a gap in the chart.
if (point <= 0) {
point = null;
}
}
series.push([ x, point ]);
}
return series;
};
/** @inheritDoc */
DefaultHandler.prototype.rollingAverage = function(originalData, rollPeriod,
options) {
rollPeriod = Math.min(rollPeriod, originalData.length);
var rollingData = [];
var i, j, y, sum, num_ok;
// Calculate the rolling average for the first rollPeriod - 1 points
// where
// there is not enough data to roll over the full number of points
if (rollPeriod == 1) {
return originalData;
}
for (i = 0; i < originalData.length; i++) {
sum = 0;
num_ok = 0;
for (j = Math.max(0, i - rollPeriod + 1); j < i + 1; j++) {
y = originalData[j][1];
if (y === null || isNaN(y))
continue;
num_ok++;
sum += originalData[j][1];
}
if (num_ok) {
rollingData[i] = [ originalData[i][0], sum / num_ok ];
} else {
rollingData[i] = [ originalData[i][0], null ];
}
}
return rollingData;
};
/** @inheritDoc */
DefaultHandler.prototype.getExtremeYValues = function(series, dateWindow,
options) {
var minY = null, maxY = null, y;
var firstIdx = 0, lastIdx = series.length - 1;
for ( var j = firstIdx; j <= lastIdx; j++) {
y = series[j][1];
if (y === null || isNaN(y))
continue;
if (maxY === null || y > maxY) {
maxY = y;
}
if (minY === null || y < minY) {
minY = y;
}
}
return [ minY, maxY ];
};
export default DefaultHandler;