3 * Copyright 2013 David Eberlein (david.eberlein@ch.sauter-bc.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
8 * @fileoverview This file contains the managment of data handlers
9 * @author David Eberlein (david.eberlein@ch.sauter-bc.com)
11 * The idea is to define a common, generic data format that works for all data
12 * structures supported by dygraphs. To make this possible, the DataHandler
13 * interface is introduced. This makes it possible, that dygraph itself can work
14 * with the same logic for every data type independent of the actual format and
15 * the DataHandler takes care of the data format specific jobs.
16 * DataHandlers are implemented for all data types supported by Dygraphs and
17 * return Dygraphs compliant formats.
18 * By default the correct DataHandler is chosen based on the options set.
19 * Optionally the user may use his own DataHandler (similar to the plugin
23 * The unified data format returend by each handler is defined as so:
24 * series[n][point] = [x,y,(extras)]
26 * This format contains the common basis that is needed to draw a simple line
27 * series extended by optional extras for more complex graphing types. It
28 * contains a primitive x value as first array entry, a primitive y value as
29 * second array entry and an optional extras object for additional data needed.
31 * x must always be a number.
32 * y must always be a number, NaN of type number or null.
33 * extras is optional and must be interpreted by the DataHandler. It may be of
36 * In practice this might look something like this:
38 * errorBar / customBar: [x, yVal, [yTopVariance, yBottomVariance] ]
41 /*jshint globalstrict: true */
42 /*global Dygraph:false */
43 /*global DygraphLayout:false */
47 * A collection of functions to create and retrieve data handlers.
49 Dygraph
.DataHandlers
= {};
52 * All registered data handlers are stored here.
56 Dygraph
.DataHandlers
.handlers_
= {};
59 * @param name {!string} The name the data handler should be registered to.
60 * Registers a data handler by the given name and makes it publicly
62 * @param handler {!Dygraph.DataHandler} DataHandler implementation which must be an
63 * instance of Dygraph.DataHandler.
66 Dygraph
.DataHandlers
.registerHandler
= function(name
, handler
) {
67 if (!handler
instanceof Dygraph
.DataHandler
) {
68 throw ("the handler must be a prototype of Dygraph.DataHandler");
70 Dygraph
.DataHandlers
.handlers_
[name
] = handler
;
74 * Returns the data handler registered to the given name.
75 * Note this is the data handler constructor method.
77 * @param name {!string} The name, the handler was registered to.
78 * @returns {Dygraph.DataHandler} The data handler constructor.
81 Dygraph
.DataHandlers
.getHandler
= function(name
) {
82 return Dygraph
.DataHandlers
.handlers_
[name
];
86 * Returns the cunstructed data handler registered to the given name.
88 * @param name {!string} The name, the handler was registered to.
89 * @returns {Dygraph.DataHandler} A constructed instance of the data handler.
92 Dygraph
.DataHandlers
.createHandler
= function(name
) {
93 return new Dygraph
.DataHandlers
.handlers_
[name
]();
98 * The data handler is responsible for all data specific operations. All of the
99 * series data it receives and returns is always in the unified data format.
100 * Initially the unified data is created by the extractSeries method
104 Dygraph
.DataHandler
= function () {
106 * Constructor for all data handlers.
109 var handler
= function() {
114 * X-value array index constant for unified data samples.
121 * Y-value array index constant for unified data samples.
128 * Extras-value array index constant for unified data samples.
135 * Extracts one series from the raw data (a 2D array) into an array of the
136 * unified data format.
137 * This is where undesirable points (i.e. negative values on log scales and
138 * missing values through which we wish to connect lines) are dropped.
139 * TODO(danvk): the "missing values" bit above doesn't seem right.
141 * @param rawData {!Array.<Array>} The raw data passed into dygraphs where
142 * rawData[i] = [x,ySeries1,...,ySeriesN].
143 * @param seriesIndex {!number} Index of the series to extract. All other series should
145 * @param options {!DygraphOptions} Dygraph options.
146 * @returns {Array.<[!number,?number,?]>} The series in the unified data format
147 * where series[i] = [x,y,{extras}].
150 handler
.prototype.extractSeries
= function(rawData
, seriesIndex
, options
) {
154 * Converts a series to a Point array.
156 * @param {!Array.<[!number,?number,?]>} series The series in the unified
157 * data format where series[i] = [x,y,{extras}].
158 * @param {!string} setName Name of the series.
159 * @param {!number} boundaryIdStart Index offset of the first point, equal to the
160 * number of skipped points left of the date window minimum (if any).
161 * @return {!Array.<Dygraph.PointType>} List of points for this series.
164 handler
.prototype.seriesToPoints
= function(series
, setName
, boundaryIdStart
) {
165 // TODO(bhs): these loops are a hot-spot for high-point-count charts. In
167 // on chrome+linux, they are 6 times more expensive than iterating through
169 // points and drawing the lines. The brunt of the cost comes from allocating
170 // the |point| structures.
172 for ( var i
= 0; i
< series
.length
; ++i
) {
173 var item
= series
[i
];
175 var yval
= yraw
=== null ? null : DygraphLayout
.parseFloat_(yraw
);
179 xval
: DygraphLayout
.parseFloat_(item
[0]),
181 name
: setName
, // TODO(danvk): is this really necessary?
182 idx
: i
+ boundaryIdStart
186 handler
.prototype.onPointsCreated_(series
, points
);
191 * Callback called for each series after the series points have been generated
192 * which will later be used by the plotters to draw the graph.
193 * Here data may be added to the seriesPoints which is needed by the plotters.
194 * The indexes of series and points are in sync meaning the original data
195 * sample for series[i] is points[i].
197 * @param {!Array.<[!number,?number,?]>} series The series in the unified
198 * data format where series[i] = [x,y,{extras}].
199 * @param {!Array.<Dygraph.PointType>} points The corresponding points passed
203 handler
.prototype.onPointsCreated_
= function(series
, points
) {
207 * Calculates the rolling average of a data set.
209 * @param {!Array.<[!number,?number,?]>} series The series in the unified
210 * data format where series[i] = [x,y,{extras}].
211 * @param {!number} rollPeriod The number of points over which to average the data
212 * @param {!DygraphOptions} options The dygraph options.
213 * @return the rolled series.
216 handler
.prototype.rollingAverage
= function(series
, rollPeriod
, options
) {
220 * Computes the range of the data series (including confidence intervals).
222 * @param {!Array.<[!number,?number,?]>} series The series in the unified
223 * data format where series[i] = [x,y,{extras}].
224 * @param {!Array.<number>} dateWindow The x-value range to display with
225 * the format: [min,max].
226 * @param {!DygraphOptions} options The dygraph options.
227 * @return {Array.<number>} The low and high extremes of the series in the given window with
228 * the format: [low, high].
231 handler
.prototype.getExtremeYValues
= function(series
, dateWindow
, options
) {
235 * Callback called for each series after the layouting data has been
236 * calculated before the series is drawn. Here normalized positioning data
237 * should be calculated for the extras of each point.
239 * @param {!Array.<Dygraph.PointType>} points The points passed to
241 * @param {!Object} axis The axis on which the series will be plotted.
242 * @param {!boolean} logscale Weather or not to use a logscale.
245 handler
.prototype.onLineEvaluated
= function(points
, axis
, logscale
) {
249 * Helper method that computes the y value of a line defined by the points p1
250 * and p2 and a given x value.
252 * @param {!Array.<number>} p1 left point ([x,y]).
253 * @param {!Array.<number>} p2 right point ([x,y]).
254 * @param {!number} xValue The x value to compute the y-intersection for.
255 * @return {number} corresponding y value to x on the line defined by p1 and p2.
258 handler
.prototype.computeYInterpolation_
= function(p1
, p2
, xValue
) {
259 var deltaY
= p2
[1] - p1
[1];
260 var deltaX
= p2
[0] - p1
[0];
261 var gradient
= deltaY
/ deltaX
;
262 var growth
= (xValue
- p1
[0]) * gradient
;
263 return p1
[1] + growth
;
267 * Helper method that returns the first and the last index of the given series
268 * that lie inside the given dateWindow.
270 * @param {!Array.<[!number,?number,?]>} series The series in the unified
271 * data format where series[i] = [x,y,{extras}].
272 * @param {!Array.<number>} dateWindow The x-value range to display with
273 * the format: [min,max].
274 * @return {!Array.<[!number,?number,?]>} The samples of the series that
275 * are in the given date window.
278 handler
.prototype.getIndexesInWindow_
= function(series
, dateWindow
) {
279 var firstIdx
= 0, lastIdx
= series
.length
- 1;
282 var low
= dateWindow
[0];
283 var high
= dateWindow
[1];
285 // Start from each side of the array to minimize the performance
287 while (idx
< series
.length
- 1 && series
[idx
][0] < low
) {
291 idx
= series
.length
- 1;
292 while (idx
> 0 && series
[idx
][0] > high
) {
297 if (firstIdx
<= lastIdx
) {
298 return [ firstIdx
, lastIdx
];
300 return [ 0, series
.length
- 1 ];