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