UNPKG

2.4 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2013 David Eberlein (david.eberlein@ch.sauter-bc.com)
4 * MIT-licenced: https://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/*global Dygraph:false */
13"use strict";
14
15import DygraphDataHandler from './datahandler';
16import DefaultHandler from './default';
17
18/**
19 * @extends DefaultHandler
20 * @constructor
21 */
22var DefaultFractionHandler = function() {
23};
24
25DefaultFractionHandler.prototype = new DefaultHandler();
26
27DefaultFractionHandler.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 const seriesLabel = options.get("labels")[i];
33 const logScale = options.getForSeries("logscale", seriesLabel);
34 for ( var j = 0; j < rawData.length; j++) {
35 x = rawData[j][0];
36 point = rawData[j][i];
37 if (logScale && point !== null) {
38 // On the log scale, points less than zero do not exist.
39 // This will create a gap in the chart.
40 if (point[0] <= 0 || point[1] <= 0) {
41 point = null;
42 }
43 }
44 // Extract to the unified data format.
45 if (point !== null) {
46 num = point[0];
47 den = point[1];
48 if (num !== null && !isNaN(num)) {
49 value = den ? num / den : 0.0;
50 y = mult * value;
51 // preserve original values in extras for further filtering
52 series.push([ x, y, [ num, den ] ]);
53 } else {
54 series.push([ x, num, [ num, den ] ]);
55 }
56 } else {
57 series.push([ x, null, [ null, null ] ]);
58 }
59 }
60 return series;
61};
62
63DefaultFractionHandler.prototype.rollingAverage = function(originalData, rollPeriod,
64 options, i) {
65 rollPeriod = Math.min(rollPeriod, originalData.length);
66 var rollingData = [];
67
68 var i;
69 var num = 0;
70 var den = 0; // numerator/denominator
71 var mult = 100.0;
72 for (i = 0; i < originalData.length; i++) {
73 num += originalData[i][2][0];
74 den += originalData[i][2][1];
75 if (i - rollPeriod >= 0) {
76 num -= originalData[i - rollPeriod][2][0];
77 den -= originalData[i - rollPeriod][2][1];
78 }
79
80 var date = originalData[i][0];
81 var value = den ? num / den : 0.0;
82 rollingData[i] = [ date, mult * value ];
83 }
84
85 return rollingData;
86};
87
88export default DefaultFractionHandler;