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