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 /*global Dygraph:false */
42 /*global DygraphLayout:false */
46 * The data handler is responsible for all data specific operations. All of the
47 * series data it receives and returns is always in the unified data format.
48 * Initially the unified data is created by the extractSeries method
51 Dygraph
.DataHandler
= function () {
55 * A collection of functions to create and retrieve data handlers.
56 * @type {Object.<!Dygraph.DataHandler>}
58 Dygraph
.DataHandlers
= {};
64 var handler
= Dygraph
.DataHandler
;
67 * X-value array index constant for unified data samples.
74 * Y-value array index constant for unified data samples.
81 * Extras-value array index constant for unified data samples.
88 * Extracts one series from the raw data (a 2D array) into an array of the
89 * unified data format.
90 * This is where undesirable points (i.e. negative values on log scales and
91 * missing values through which we wish to connect lines) are dropped.
92 * TODO(danvk): the "missing values" bit above doesn't seem right.
94 * @param {!Array.<Array>} rawData The raw data passed into dygraphs where
95 * rawData[i] = [x,ySeries1,...,ySeriesN].
96 * @param {!number} seriesIndex Index of the series to extract. All other
97 * series should be ignored.
98 * @param {!DygraphOptions} options Dygraph options.
99 * @return {Array.<[!number,?number,?]>} The series in the unified data format
100 * where series[i] = [x,y,{extras}].
102 handler
.prototype.extractSeries
= function(rawData
, seriesIndex
, options
) {
106 * Converts a series to a Point array.
108 * @param {!Array.<[!number,?number,?]>} series The series in the unified
109 * data format where series[i] = [x,y,{extras}].
110 * @param {!string} setName Name of the series.
111 * @param {!number} boundaryIdStart Index offset of the first point, equal to the
112 * number of skipped points left of the date window minimum (if any).
113 * @return {!Array.<Dygraph.PointType>} List of points for this series.
115 handler
.prototype.seriesToPoints
= function(series
, setName
, boundaryIdStart
) {
116 // TODO(bhs): these loops are a hot-spot for high-point-count charts. In
118 // on chrome+linux, they are 6 times more expensive than iterating through
120 // points and drawing the lines. The brunt of the cost comes from allocating
121 // the |point| structures.
123 for ( var i
= 0; i
< series
.length
; ++i
) {
124 var item
= series
[i
];
126 var yval
= yraw
=== null ? null : handler
.parseFloat(yraw
);
130 xval
: handler
.parseFloat(item
[0]),
132 name
: setName
, // TODO(danvk): is this really necessary?
133 idx
: i
+ boundaryIdStart
137 this.onPointsCreated_(series
, points
);
142 * Callback called for each series after the series points have been generated
143 * which will later be used by the plotters to draw the graph.
144 * Here data may be added to the seriesPoints which is needed by the plotters.
145 * The indexes of series and points are in sync meaning the original data
146 * sample for series[i] is points[i].
148 * @param {!Array.<[!number,?number,?]>} series The series in the unified
149 * data format where series[i] = [x,y,{extras}].
150 * @param {!Array.<Dygraph.PointType>} points The corresponding points passed
154 handler
.prototype.onPointsCreated_
= function(series
, points
) {
158 * Calculates the rolling average of a data set.
160 * @param {!Array.<[!number,?number,?]>} series The series in the unified
161 * data format where series[i] = [x,y,{extras}].
162 * @param {!number} rollPeriod The number of points over which to average the data
163 * @param {!DygraphOptions} options The dygraph options.
164 * @return {!Array.<[!number,?number,?]>} the rolled series.
166 handler
.prototype.rollingAverage
= function(series
, rollPeriod
, options
) {
170 * Computes the range of the data series (including confidence intervals).
172 * @param {!Array.<[!number,?number,?]>} series The series in the unified
173 * data format where series[i] = [x, y, {extras}].
174 * @param {!Array.<number>} dateWindow The x-value range to display with
175 * the format: [min, max].
176 * @param {!DygraphOptions} options The dygraph options.
177 * @return {Array.<number>} The low and high extremes of the series in the
178 * given window with the format: [low, high].
180 handler
.prototype.getExtremeYValues
= function(series
, dateWindow
, options
) {
184 * Callback called for each series after the layouting data has been
185 * calculated before the series is drawn. Here normalized positioning data
186 * should be calculated for the extras of each point.
188 * @param {!Array.<Dygraph.PointType>} points The points passed to
190 * @param {!Object} axis The axis on which the series will be plotted.
191 * @param {!boolean} logscale Weather or not to use a logscale.
193 handler
.prototype.onLineEvaluated
= function(points
, axis
, logscale
) {
197 * Helper method that computes the y value of a line defined by the points p1
198 * and p2 and a given x value.
200 * @param {!Array.<number>} p1 left point ([x,y]).
201 * @param {!Array.<number>} p2 right point ([x,y]).
202 * @param {!number} xValue The x value to compute the y-intersection for.
203 * @return {number} corresponding y value to x on the line defined by p1 and p2.
206 handler
.prototype.computeYInterpolation_
= function(p1
, p2
, xValue
) {
207 var deltaY
= p2
[1] - p1
[1];
208 var deltaX
= p2
[0] - p1
[0];
209 var gradient
= deltaY
/ deltaX
;
210 var growth
= (xValue
- p1
[0]) * gradient
;
211 return p1
[1] + growth
;
215 * Helper method that returns the first and the last index of the given series
216 * that lie inside the given dateWindow.
218 * @param {!Array.<[!number,?number,?]>} series The series in the unified
219 * data format where series[i] = [x,y,{extras}].
220 * @param {!Array.<number>} dateWindow The x-value range to display with
221 * the format: [min,max].
222 * @return {!Array.<[!number,?number,?]>} The samples of the series that
223 * are in the given date window.
226 handler
.prototype.getIndexesInWindow_
= function(series
, dateWindow
) {
227 var firstIdx
= 0, lastIdx
= series
.length
- 1;
230 var low
= dateWindow
[0];
231 var high
= dateWindow
[1];
233 // Start from each side of the array to minimize the performance
235 while (idx
< series
.length
- 1 && series
[idx
][0] < low
) {
239 idx
= series
.length
- 1;
240 while (idx
> 0 && series
[idx
][0] > high
) {
245 if (firstIdx
<= lastIdx
) {
246 return [ firstIdx
, lastIdx
];
248 return [ 0, series
.length
- 1 ];
253 * Optimized replacement for parseFloat, which was way too slow when almost
254 * all values were type number, with few edge cases, none of which were strings.
255 * @param {?number} val
259 handler
.parseFloat
= function(val
) {
260 // parseFloat(null) is NaN
265 // Assume it's a number or NaN. If it's something else, I'll be shocked.