add test of plugins/legend.js
[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
749281f8
DV
17/**
18 * @constructor
19 * @extends Dygraph.DataHandler
20 */
21Dygraph.DataHandlers.DefaultHandler = function() {
22};
23
3ea41d86 24var DefaultHandler = Dygraph.DataHandlers.DefaultHandler;
749281f8 25DefaultHandler.prototype = new Dygraph.DataHandler();
3ea41d86 26
749281f8 27/** @inheritDoc */
3ea41d86
DV
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;
a49c164a 40 }
a49c164a 41 }
3ea41d86
DV
42 series.push([ x, point ]);
43 }
44 return series;
45};
a49c164a 46
749281f8 47/** @inheritDoc */
3ea41d86
DV
48DefaultHandler.prototype.rollingAverage = function(originalData, rollPeriod,
49 options) {
50 rollPeriod = Math.min(rollPeriod, originalData.length);
51 var rollingData = [];
a49c164a 52
3ea41d86
DV
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];
a49c164a 69 }
3ea41d86
DV
70 if (num_ok) {
71 rollingData[i] = [ originalData[i][0], sum / num_ok ];
72 } else {
73 rollingData[i] = [ originalData[i][0], null ];
a49c164a 74 }
3ea41d86 75 }
a49c164a 76
3ea41d86
DV
77 return rollingData;
78};
a49c164a 79
749281f8 80/** @inheritDoc */
3ea41d86
DV
81DefaultHandler.prototype.getExtremeYValues = function(series, dateWindow,
82 options) {
83 var minY = null, maxY = null, y;
84 var firstIdx = 0, lastIdx = series.length - 1;
a49c164a 85
3ea41d86
DV
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;
a49c164a 92 }
3ea41d86
DV
93 if (minY === null || y < minY) {
94 minY = y;
95 }
96 }
97 return [ minY, maxY ];
98};
99
a49c164a 100})();