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