Merge pull request #673 from danvk/track-code-size
[dygraphs.git] / src / datahandler / bars.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 base implementation for the "bar"
9 * data formats. This implementation must be extended and the
10 * extractSeries and rollingAverage must be implemented.
11 * @author David Eberlein (david.eberlein@ch.sauter-bc.com)
12 */
13
14 (function() {
15
16 /*global Dygraph:false */
17 /*global DygraphLayout:false */
18 "use strict";
19
20 /**
21 * @constructor
22 * @extends {Dygraph.DataHandler}
23 */
24 Dygraph.DataHandlers.BarsHandler = function() {
25 Dygraph.DataHandler.call(this);
26 };
27 Dygraph.DataHandlers.BarsHandler.prototype = new Dygraph.DataHandler();
28
29 // alias for the rest of the implementation
30 var BarsHandler = Dygraph.DataHandlers.BarsHandler;
31
32 // TODO(danvk): figure out why the jsdoc has to be copy/pasted from superclass.
33 // (I get closure compiler errors if this isn't here.)
34 /**
35 * @override
36 * @param {!Array.<Array>} rawData The raw data passed into dygraphs where
37 * rawData[i] = [x,ySeries1,...,ySeriesN].
38 * @param {!number} seriesIndex Index of the series to extract. All other
39 * series should be ignored.
40 * @param {!DygraphOptions} options Dygraph options.
41 * @return {Array.<[!number,?number,?]>} The series in the unified data format
42 * where series[i] = [x,y,{extras}].
43 */
44 BarsHandler.prototype.extractSeries = function(rawData, seriesIndex, options) {
45 // Not implemented here must be extended
46 };
47
48 /**
49 * @override
50 * @param {!Array.<[!number,?number,?]>} series The series in the unified
51 * data format where series[i] = [x,y,{extras}].
52 * @param {!number} rollPeriod The number of points over which to average the data
53 * @param {!DygraphOptions} options The dygraph options.
54 * TODO(danvk): be more specific than "Array" here.
55 * @return {!Array.<[!number,?number,?]>} the rolled series.
56 */
57 BarsHandler.prototype.rollingAverage =
58 function(series, rollPeriod, options) {
59 // Not implemented here, must be extended.
60 };
61
62 /** @inheritDoc */
63 BarsHandler.prototype.onPointsCreated_ = function(series, points) {
64 for (var i = 0; i < series.length; ++i) {
65 var item = series[i];
66 var point = points[i];
67 point.y_top = NaN;
68 point.y_bottom = NaN;
69 point.yval_minus = Dygraph.DataHandler.parseFloat(item[2][0]);
70 point.yval_plus = Dygraph.DataHandler.parseFloat(item[2][1]);
71 }
72 };
73
74 /** @inheritDoc */
75 BarsHandler.prototype.getExtremeYValues = function(series, dateWindow, options) {
76 var minY = null, maxY = null, y;
77
78 var firstIdx = 0;
79 var lastIdx = series.length - 1;
80
81 for ( var j = firstIdx; j <= lastIdx; j++) {
82 y = series[j][1];
83 if (y === null || isNaN(y)) continue;
84
85 var low = series[j][2][0];
86 var high = series[j][2][1];
87
88 if (low > y) low = y; // this can happen with custom bars,
89 if (high < y) high = y; // e.g. in tests/custom-bars.html
90
91 if (maxY === null || high > maxY) maxY = high;
92 if (minY === null || low < minY) minY = low;
93 }
94
95 return [ minY, maxY ];
96 };
97
98 /** @inheritDoc */
99 BarsHandler.prototype.onLineEvaluated = function(points, axis, logscale) {
100 var point;
101 for (var j = 0; j < points.length; j++) {
102 // Copy over the error terms
103 point = points[j];
104 point.y_top = DygraphLayout.calcYNormal_(axis, point.yval_minus, logscale);
105 point.y_bottom = DygraphLayout.calcYNormal_(axis, point.yval_plus, logscale);
106 }
107 };
108
109 })();