lint
[dygraphs.git] / datahandler / default.js
CommitLineData
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 default implementation used for simple line charts.
9 * @author David Eberlein (david.eberlein@ch.sauter-bc.com)
10 */
11
12(function() {
a49c164a 13
3ea41d86
DV
14/*global Dygraph:false */
15"use strict";
a49c164a 16
3ea41d86
DV
17Dygraph.DataHandlers.DefaultHandler = Dygraph.DataHandler();
18var DefaultHandler = Dygraph.DataHandlers.DefaultHandler;
19
20DefaultHandler.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;
a49c164a 32 }
a49c164a 33 }
3ea41d86
DV
34 series.push([ x, point ]);
35 }
36 return series;
37};
a49c164a 38
3ea41d86
DV
39DefaultHandler.prototype.rollingAverage = function(originalData, rollPeriod,
40 options) {
41 rollPeriod = Math.min(rollPeriod, originalData.length);
42 var rollingData = [];
a49c164a 43
3ea41d86
DV
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];
a49c164a 60 }
3ea41d86
DV
61 if (num_ok) {
62 rollingData[i] = [ originalData[i][0], sum / num_ok ];
63 } else {
64 rollingData[i] = [ originalData[i][0], null ];
a49c164a 65 }
3ea41d86 66 }
a49c164a 67
3ea41d86
DV
68 return rollingData;
69};
a49c164a 70
3ea41d86
DV
71DefaultHandler.prototype.getExtremeYValues = function(series, dateWindow,
72 options) {
73 var minY = null, maxY = null, y;
74 var firstIdx = 0, lastIdx = series.length - 1;
a49c164a 75
3ea41d86
DV
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;
a49c164a 82 }
3ea41d86
DV
83 if (minY === null || y < minY) {
84 minY = y;
85 }
86 }
87 return [ minY, maxY ];
88};
89
a49c164a 90})();