7e234b07dadedea7714015bca99f6b3d4bb34e42
3 * Copyright 2013 David Eberlein (david.eberlein@ch.sauter-bc.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
8 * @fileoverview DataHandler implementation for the fractions option.
9 * @author David Eberlein (david.eberlein@ch.sauter-bc.com)
13 /*global Dygraph:false */
16 var DefaultFractionHandler
= Dygraph
.DataHandler();
17 DefaultFractionHandler
.prototype = Dygraph
.DataHandlers
.createHandler("default");
18 Dygraph
.DataHandlers
.registerHandler("default-fractions", DefaultFractionHandler
);
20 DefaultFractionHandler
.prototype.extractSeries
= function(rawData
, i
, options
) {
21 // TODO(danvk): pre-allocate series here.
23 var x
, y
, point
, num
, den
, value
;
25 var logScale
= options
.get('logscale');
26 for ( var j
= 0; j
< rawData
.length
; j
++) {
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) {
36 // Extract to the unified data format.
40 if (num
!== null && !isNaN(num
)) {
41 value
= den
? num
/ den
: 0.0;
43 // preserve original values in extras for further filtering
44 series
.push([ x
, y
, [ num
, den
] ]);
46 series
.push([ x
, num
, [ num
, den
] ]);
49 series
.push([ x
, null, [ null, null ] ]);
55 DefaultFractionHandler
.prototype.rollingAverage
= function(originalData
, rollPeriod
,
57 rollPeriod
= Math
.min(rollPeriod
, originalData
.length
);
62 var den
= 0; // numerator/denominator
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];
72 var date
= originalData
[i
][0];
73 var value
= den
? num
/ den
: 0.0;
74 rollingData
[i
] = [ date
, mult
* value
];