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 implementation for the fractions 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"; | |
a49c164a | 16 | |
749281f8 DV |
17 | /** |
18 | * @extends Dygraph.DataHandlers.DefaultHandler | |
19 | * @constructor | |
20 | */ | |
21 | Dygraph.DataHandlers.DefaultFractionHandler = function() { | |
22 | }; | |
23 | ||
3ea41d86 DV |
24 | var DefaultFractionHandler = Dygraph.DataHandlers.DefaultFractionHandler; |
25 | DefaultFractionHandler.prototype = new Dygraph.DataHandlers.DefaultHandler(); | |
26 | ||
27 | DefaultFractionHandler.prototype.extractSeries = function(rawData, i, options) { | |
28 | // TODO(danvk): pre-allocate series here. | |
29 | var series = []; | |
30 | var x, y, point, num, den, value; | |
31 | var mult = 100.0; | |
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) { | |
40 | point = null; | |
a49c164a | 41 | } |
3ea41d86 DV |
42 | } |
43 | // Extract to the unified data format. | |
44 | if (point !== null) { | |
45 | num = point[0]; | |
46 | den = point[1]; | |
47 | if (num !== null && !isNaN(num)) { | |
48 | value = den ? num / den : 0.0; | |
49 | y = mult * value; | |
50 | // preserve original values in extras for further filtering | |
51 | series.push([ x, y, [ num, den ] ]); | |
a49c164a | 52 | } else { |
3ea41d86 | 53 | series.push([ x, num, [ num, den ] ]); |
a49c164a | 54 | } |
3ea41d86 DV |
55 | } else { |
56 | series.push([ x, null, [ null, null ] ]); | |
a49c164a | 57 | } |
3ea41d86 DV |
58 | } |
59 | return series; | |
60 | }; | |
a49c164a | 61 | |
3ea41d86 DV |
62 | DefaultFractionHandler.prototype.rollingAverage = function(originalData, rollPeriod, |
63 | options) { | |
64 | rollPeriod = Math.min(rollPeriod, originalData.length); | |
65 | var rollingData = []; | |
a49c164a | 66 | |
3ea41d86 DV |
67 | var i; |
68 | var num = 0; | |
69 | var den = 0; // numerator/denominator | |
70 | var mult = 100.0; | |
71 | for (i = 0; i < originalData.length; i++) { | |
72 | num += originalData[i][2][0]; | |
73 | den += originalData[i][2][1]; | |
74 | if (i - rollPeriod >= 0) { | |
75 | num -= originalData[i - rollPeriod][2][0]; | |
76 | den -= originalData[i - rollPeriod][2][1]; | |
a49c164a DE |
77 | } |
78 | ||
3ea41d86 DV |
79 | var date = originalData[i][0]; |
80 | var value = den ? num / den : 0.0; | |
81 | rollingData[i] = [ date, mult * value ]; | |
82 | } | |
83 | ||
84 | return rollingData; | |
85 | }; | |
86 | ||
a49c164a | 87 | })(); |