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