Migrate most of core dygraphs to ES6 modules.
[dygraphs.git] / src / datahandler / default.js
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 default implementation used for simple line charts.
9 * @author David Eberlein (david.eberlein@ch.sauter-bc.com)
10 */
11
12 /*global Dygraph:false */
13 "use strict";
14
15 import DygraphDataHandler from './datahandler';
16
17 /**
18 * @constructor
19 * @extends Dygraph.DataHandler
20 */
21 var DefaultHandler = function() {
22 };
23
24 DefaultHandler.prototype = new DygraphDataHandler();
25
26 /** @inheritDoc */
27 DefaultHandler.prototype.extractSeries = function(rawData, i, options) {
28 // TODO(danvk): pre-allocate series here.
29 var series = [];
30 var logScale = options.get('logscale');
31 for ( var j = 0; j < rawData.length; j++) {
32 var x = rawData[j][0];
33 var point = rawData[j][i];
34 if (logScale) {
35 // On the log scale, points less than zero do not exist.
36 // This will create a gap in the chart.
37 if (point <= 0) {
38 point = null;
39 }
40 }
41 series.push([ x, point ]);
42 }
43 return series;
44 };
45
46 /** @inheritDoc */
47 DefaultHandler.prototype.rollingAverage = function(originalData, rollPeriod,
48 options) {
49 rollPeriod = Math.min(rollPeriod, originalData.length);
50 var rollingData = [];
51
52 var i, j, y, sum, num_ok;
53 // Calculate the rolling average for the first rollPeriod - 1 points
54 // where
55 // there is not enough data to roll over the full number of points
56 if (rollPeriod == 1) {
57 return originalData;
58 }
59 for (i = 0; i < originalData.length; i++) {
60 sum = 0;
61 num_ok = 0;
62 for (j = Math.max(0, i - rollPeriod + 1); j < i + 1; j++) {
63 y = originalData[j][1];
64 if (y === null || isNaN(y))
65 continue;
66 num_ok++;
67 sum += originalData[j][1];
68 }
69 if (num_ok) {
70 rollingData[i] = [ originalData[i][0], sum / num_ok ];
71 } else {
72 rollingData[i] = [ originalData[i][0], null ];
73 }
74 }
75
76 return rollingData;
77 };
78
79 /** @inheritDoc */
80 DefaultHandler.prototype.getExtremeYValues = function(series, dateWindow,
81 options) {
82 var minY = null, maxY = null, y;
83 var firstIdx = 0, lastIdx = series.length - 1;
84
85 for ( var j = firstIdx; j <= lastIdx; j++) {
86 y = series[j][1];
87 if (y === null || isNaN(y))
88 continue;
89 if (maxY === null || y > maxY) {
90 maxY = y;
91 }
92 if (minY === null || y < minY) {
93 minY = y;
94 }
95 }
96 return [ minY, maxY ];
97 };
98
99 export default DefaultHandler;