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