6e6697f8b4f3f8eed35faf5c07248c9b0907e525
[dygraphs.git] / 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 (function() {
13
14 /*global Dygraph:false */
15 "use strict";
16
17 Dygraph.DataHandlers.DefaultHandler = Dygraph.DataHandler();
18 var DefaultHandler = Dygraph.DataHandlers.DefaultHandler;
19
20 DefaultHandler.prototype.extractSeries = function(rawData, i, options) {
21 // TODO(danvk): pre-allocate series here.
22 var series = [];
23 var logScale = options.get('logscale');
24 for ( var j = 0; j < rawData.length; j++) {
25 var x = rawData[j][0];
26 var point = rawData[j][i];
27 if (logScale) {
28 // On the log scale, points less than zero do not exist.
29 // This will create a gap in the chart.
30 if (point <= 0) {
31 point = null;
32 }
33 }
34 series.push([ x, point ]);
35 }
36 return series;
37 };
38
39 DefaultHandler.prototype.rollingAverage = function(originalData, rollPeriod,
40 options) {
41 rollPeriod = Math.min(rollPeriod, originalData.length);
42 var rollingData = [];
43
44 var i, j, y, sum, num_ok;
45 // Calculate the rolling average for the first rollPeriod - 1 points
46 // where
47 // there is not enough data to roll over the full number of points
48 if (rollPeriod == 1) {
49 return originalData;
50 }
51 for (i = 0; i < originalData.length; i++) {
52 sum = 0;
53 num_ok = 0;
54 for (j = Math.max(0, i - rollPeriod + 1); j < i + 1; j++) {
55 y = originalData[j][1];
56 if (y === null || isNaN(y))
57 continue;
58 num_ok++;
59 sum += originalData[j][1];
60 }
61 if (num_ok) {
62 rollingData[i] = [ originalData[i][0], sum / num_ok ];
63 } else {
64 rollingData[i] = [ originalData[i][0], null ];
65 }
66 }
67
68 return rollingData;
69 };
70
71 DefaultHandler.prototype.getExtremeYValues = function(series, dateWindow,
72 options) {
73 var minY = null, maxY = null, y;
74 var firstIdx = 0, lastIdx = series.length - 1;
75
76 for ( var j = firstIdx; j <= lastIdx; j++) {
77 y = series[j][1];
78 if (y === null || isNaN(y))
79 continue;
80 if (maxY === null || y > maxY) {
81 maxY = y;
82 }
83 if (minY === null || y < minY) {
84 minY = y;
85 }
86 }
87 return [ minY, maxY ];
88 };
89
90 })();