Datahandler and Unified Data Format
[dygraphs.git] / 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 /*global Dygraph:false */
16 /*global DygraphLayout:false */
17 "use strict";
18
19 var BarsHandler = Dygraph.DataHandler();
20 Dygraph.DataHandlers.registerHandler("bars", BarsHandler);
21 // errorBars
22 BarsHandler.prototype.extractSeries = function(rawData, i, options) {
23 // Not implemented here must be extended
24 };
25
26 BarsHandler.prototype.rollingAverage = function(originalData, rollPeriod,
27 options) {
28 // Not implemented here, must be extended.
29 };
30
31 BarsHandler.prototype.onPointsCreated_ = function(series, points) {
32 for (var i = 0; i < series.length; ++i) {
33 var item = series[i];
34 var point = points[i];
35 point.y_top = NaN;
36 point.y_bottom = NaN;
37 point.yval_minus = DygraphLayout.parseFloat_(item[2][0]);
38 point.yval_plus = DygraphLayout.parseFloat_(item[2][1]);
39 }
40 };
41
42 BarsHandler.prototype.getExtremeYValues = function(series, dateWindow, options) {
43 var minY = null, maxY = null, y;
44
45 var firstIdx = 0;
46 var lastIdx = series.length - 1;
47
48 for ( var j = firstIdx; j <= lastIdx; j++) {
49 y = series[j][1];
50 if (y === null || isNaN(y)) continue;
51
52 var low = series[j][2][0];
53 var high = series[j][2][1];
54
55 if (low > y) low = y; // this can happen with custom bars,
56 if (high < y) high = y; // e.g. in tests/custom-bars.html
57
58 if (maxY === null || high > maxY) maxY = high;
59 if (minY === null || low < minY) minY = low;
60 }
61
62 return [ minY, maxY ];
63 };
64
65 BarsHandler.prototype.onLineEvaluated = function(points, axis, logscale) {
66 var point;
67 for (var j = 0; j < points.length; j++) {
68 // Copy over the error terms
69 point = points[j];
70 point.y_top = DygraphLayout._calcYNormal(axis, point.yval_minus, logscale);
71 point.y_bottom = DygraphLayout._calcYNormal(axis, point.yval_plus, logscale);
72 }
73 };
74 })();