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 | |
3ea41d86 DV |
17 | Dygraph.DataHandlers.DefaultFractionHandler = Dygraph.DataHandler(); |
18 | var DefaultFractionHandler = Dygraph.DataHandlers.DefaultFractionHandler; | |
19 | DefaultFractionHandler.prototype = new Dygraph.DataHandlers.DefaultHandler(); | |
20 | ||
21 | DefaultFractionHandler.prototype.extractSeries = function(rawData, i, options) { | |
22 | // TODO(danvk): pre-allocate series here. | |
23 | var series = []; | |
24 | var x, y, point, num, den, value; | |
25 | var mult = 100.0; | |
26 | var logScale = options.get('logscale'); | |
27 | for ( var j = 0; j < rawData.length; j++) { | |
28 | x = rawData[j][0]; | |
29 | point = rawData[j][i]; | |
30 | if (logScale && point !== null) { | |
31 | // On the log scale, points less than zero do not exist. | |
32 | // This will create a gap in the chart. | |
33 | if (point[0] <= 0 || point[1] <= 0) { | |
34 | point = null; | |
a49c164a | 35 | } |
3ea41d86 DV |
36 | } |
37 | // Extract to the unified data format. | |
38 | if (point !== null) { | |
39 | num = point[0]; | |
40 | den = point[1]; | |
41 | if (num !== null && !isNaN(num)) { | |
42 | value = den ? num / den : 0.0; | |
43 | y = mult * value; | |
44 | // preserve original values in extras for further filtering | |
45 | series.push([ x, y, [ num, den ] ]); | |
a49c164a | 46 | } else { |
3ea41d86 | 47 | series.push([ x, num, [ num, den ] ]); |
a49c164a | 48 | } |
3ea41d86 DV |
49 | } else { |
50 | series.push([ x, null, [ null, null ] ]); | |
a49c164a | 51 | } |
3ea41d86 DV |
52 | } |
53 | return series; | |
54 | }; | |
a49c164a | 55 | |
3ea41d86 DV |
56 | DefaultFractionHandler.prototype.rollingAverage = function(originalData, rollPeriod, |
57 | options) { | |
58 | rollPeriod = Math.min(rollPeriod, originalData.length); | |
59 | var rollingData = []; | |
a49c164a | 60 | |
3ea41d86 DV |
61 | var i; |
62 | var num = 0; | |
63 | var den = 0; // numerator/denominator | |
64 | var mult = 100.0; | |
65 | for (i = 0; i < originalData.length; i++) { | |
66 | num += originalData[i][2][0]; | |
67 | den += originalData[i][2][1]; | |
68 | if (i - rollPeriod >= 0) { | |
69 | num -= originalData[i - rollPeriod][2][0]; | |
70 | den -= originalData[i - rollPeriod][2][1]; | |
a49c164a DE |
71 | } |
72 | ||
3ea41d86 DV |
73 | var date = originalData[i][0]; |
74 | var value = den ? num / den : 0.0; | |
75 | rollingData[i] = [ date, mult * value ]; | |
76 | } | |
77 | ||
78 | return rollingData; | |
79 | }; | |
80 | ||
a49c164a | 81 | })(); |