Commit | Line | Data |
---|---|---|
88e95c46 DV |
1 | /** |
2 | * @license | |
3 | * Copyright 2011 Dan Vanderkam (danvdk@gmail.com) | |
4 | * MIT-licensed (http://opensource.org/licenses/MIT) | |
5 | */ | |
74a5af31 DV |
6 | |
7 | /** | |
8 | * @fileoverview Based on PlotKitLayout, but modified to meet the needs of | |
9 | * dygraphs. | |
10 | */ | |
11 | ||
758a629f DV |
12 | /*jshint globalstrict: true */ |
13 | /*global Dygraph:false */ | |
c0f54d4f DV |
14 | "use strict"; |
15 | ||
74a5af31 DV |
16 | /** |
17 | * Creates a new DygraphLayout object. | |
18 | * | |
19 | * This class contains all the data to be charted. | |
20 | * It uses data coordinates, but also records the chart range (in data | |
21 | * coordinates) and hence is able to calculate percentage positions ('In this | |
22 | * view, Point A lies 25% down the x-axis.') | |
23 | * | |
24 | * Two things that it does not do are: | |
25 | * 1. Record pixel coordinates for anything. | |
26 | * 2. (oddly) determine anything about the layout of chart elements. | |
27 | * | |
28 | * The naming is a vestige of Dygraph's original PlotKit roots. | |
29 | * | |
30 | * @constructor | |
31 | */ | |
c0f54d4f | 32 | var DygraphLayout = function(dygraph) { |
74a5af31 | 33 | this.dygraph_ = dygraph; |
758a629f | 34 | this.datasets = []; |
82c6fe4d | 35 | this.setNames = []; |
758a629f | 36 | this.annotations = []; |
74a5af31 DV |
37 | this.yAxes_ = null; |
38 | ||
39 | // TODO(danvk): it's odd that xTicks_ and yTicks_ are inputs, but xticks and | |
40 | // yticks are outputs. Clean this up. | |
41 | this.xTicks_ = null; | |
42 | this.yTicks_ = null; | |
43 | }; | |
44 | ||
45 | DygraphLayout.prototype.attr_ = function(name) { | |
46 | return this.dygraph_.attr_(name); | |
47 | }; | |
48 | ||
49 | DygraphLayout.prototype.addDataset = function(setname, set_xy) { | |
82c6fe4d KW |
50 | this.datasets.push(set_xy); |
51 | this.setNames.push(setname); | |
74a5af31 DV |
52 | }; |
53 | ||
70be5ed1 AV |
54 | DygraphLayout.prototype.getPlotArea = function() { |
55 | return this.computePlotArea_(); | |
758a629f | 56 | }; |
70be5ed1 | 57 | |
ccd9d7c2 PF |
58 | // Compute the box which the chart should be drawn in. This is the canvas's |
59 | // box, less space needed for axis and chart labels. | |
60 | DygraphLayout.prototype.computePlotArea_ = function() { | |
61 | var area = { | |
62 | // TODO(danvk): per-axis setting. | |
63 | x: 0, | |
64 | y: 0 | |
65 | }; | |
f8540c66 | 66 | |
ccd9d7c2 PF |
67 | area.w = this.dygraph_.width_ - area.x - this.attr_('rightGap'); |
68 | area.h = this.dygraph_.height_; | |
6dca682f DV |
69 | |
70 | // Let plugins reserve space. | |
71 | var e = { | |
72 | chart_div: this.dygraph_.graphDiv, | |
73 | reserveSpaceLeft: function(px) { | |
74 | var r = { | |
75 | x: area.x, | |
76 | y: area.y, | |
77 | w: px, | |
78 | h: area.h | |
79 | }; | |
80 | area.x += px; | |
81 | area.w -= px; | |
82 | return r; | |
83 | }, | |
1c177b6a DV |
84 | reserveSpaceRight: function(px) { |
85 | var r = { | |
86 | x: area.x + area.w - px, | |
87 | y: area.y, | |
88 | w: px, | |
89 | h: area.h | |
90 | }; | |
91 | area.w -= px; | |
92 | return r; | |
93 | }, | |
6dca682f DV |
94 | reserveSpaceTop: function(px) { |
95 | var r = { | |
96 | x: area.x, | |
97 | y: area.y, | |
98 | w: area.w, | |
99 | h: px | |
100 | }; | |
101 | area.y += px; | |
102 | area.h -= px; | |
103 | return r; | |
104 | }, | |
1c177b6a DV |
105 | reserveSpaceBottom: function(px) { |
106 | var r = { | |
107 | x: area.x, | |
108 | y: area.y + area.h - px, | |
109 | w: area.w, | |
110 | h: px | |
111 | }; | |
112 | area.h -= px; | |
113 | return r; | |
114 | }, | |
6dca682f DV |
115 | chartRect: function() { |
116 | return {x:area.x, y:area.y, w:area.w, h:area.h}; | |
117 | } | |
118 | }; | |
119 | this.dygraph_.cascadeEvents_('layout', e); | |
120 | ||
ccd9d7c2 PF |
121 | // Add space for range selector, if needed. |
122 | if (this.attr_('showRangeSelector')) { | |
123 | area.h -= this.attr_('rangeSelectorHeight') + 4; | |
124 | } | |
125 | ||
126 | return area; | |
127 | }; | |
128 | ||
74a5af31 DV |
129 | DygraphLayout.prototype.setAnnotations = function(ann) { |
130 | // The Dygraph object's annotations aren't parsed. We parse them here and | |
131 | // save a copy. If there is no parser, then the user must be using raw format. | |
132 | this.annotations = []; | |
133 | var parse = this.attr_('xValueParser') || function(x) { return x; }; | |
134 | for (var i = 0; i < ann.length; i++) { | |
135 | var a = {}; | |
136 | if (!ann[i].xval && !ann[i].x) { | |
137 | this.dygraph_.error("Annotations must have an 'x' property"); | |
138 | return; | |
139 | } | |
140 | if (ann[i].icon && | |
141 | !(ann[i].hasOwnProperty('width') && | |
142 | ann[i].hasOwnProperty('height'))) { | |
143 | this.dygraph_.error("Must set width and height when setting " + | |
144 | "annotation.icon property"); | |
145 | return; | |
146 | } | |
147 | Dygraph.update(a, ann[i]); | |
148 | if (!a.xval) a.xval = parse(a.x); | |
149 | this.annotations.push(a); | |
150 | } | |
151 | }; | |
152 | ||
153 | DygraphLayout.prototype.setXTicks = function(xTicks) { | |
154 | this.xTicks_ = xTicks; | |
155 | }; | |
156 | ||
157 | // TODO(danvk): add this to the Dygraph object's API or move it into Layout. | |
158 | DygraphLayout.prototype.setYAxes = function (yAxes) { | |
159 | this.yAxes_ = yAxes; | |
160 | }; | |
161 | ||
162 | DygraphLayout.prototype.setDateWindow = function(dateWindow) { | |
163 | this.dateWindow_ = dateWindow; | |
164 | }; | |
165 | ||
166 | DygraphLayout.prototype.evaluate = function() { | |
167 | this._evaluateLimits(); | |
168 | this._evaluateLineCharts(); | |
169 | this._evaluateLineTicks(); | |
170 | this._evaluateAnnotations(); | |
171 | }; | |
172 | ||
173 | DygraphLayout.prototype._evaluateLimits = function() { | |
174 | this.minxval = this.maxxval = null; | |
175 | if (this.dateWindow_) { | |
176 | this.minxval = this.dateWindow_[0]; | |
177 | this.maxxval = this.dateWindow_[1]; | |
178 | } else { | |
82c6fe4d KW |
179 | for (var setIdx = 0; setIdx < this.datasets.length; ++setIdx) { |
180 | var series = this.datasets[setIdx]; | |
74a5af31 DV |
181 | if (series.length > 1) { |
182 | var x1 = series[0][0]; | |
183 | if (!this.minxval || x1 < this.minxval) this.minxval = x1; | |
ccd9d7c2 | 184 | |
74a5af31 DV |
185 | var x2 = series[series.length - 1][0]; |
186 | if (!this.maxxval || x2 > this.maxxval) this.maxxval = x2; | |
187 | } | |
188 | } | |
189 | } | |
190 | this.xrange = this.maxxval - this.minxval; | |
758a629f | 191 | this.xscale = (this.xrange !== 0 ? 1/this.xrange : 1.0); |
74a5af31 DV |
192 | |
193 | for (var i = 0; i < this.yAxes_.length; i++) { | |
194 | var axis = this.yAxes_[i]; | |
195 | axis.minyval = axis.computedValueRange[0]; | |
196 | axis.maxyval = axis.computedValueRange[1]; | |
197 | axis.yrange = axis.maxyval - axis.minyval; | |
758a629f | 198 | axis.yscale = (axis.yrange !== 0 ? 1.0 / axis.yrange : 1.0); |
74a5af31 DV |
199 | |
200 | if (axis.g.attr_("logscale")) { | |
201 | axis.ylogrange = Dygraph.log10(axis.maxyval) - Dygraph.log10(axis.minyval); | |
758a629f | 202 | axis.ylogscale = (axis.ylogrange !== 0 ? 1.0 / axis.ylogrange : 1.0); |
74a5af31 DV |
203 | if (!isFinite(axis.ylogrange) || isNaN(axis.ylogrange)) { |
204 | axis.g.error('axis ' + i + ' of graph at ' + axis.g + | |
205 | ' can\'t be displayed in log scale for range [' + | |
206 | axis.minyval + ' - ' + axis.maxyval + ']'); | |
207 | } | |
208 | } | |
209 | } | |
210 | }; | |
211 | ||
7f028980 DV |
212 | DygraphLayout._calcYNormal = function(axis, value) { |
213 | if (axis.logscale) { | |
214 | return 1.0 - ((Dygraph.log10(value) - Dygraph.log10(axis.minyval)) * axis.ylogscale); | |
215 | } else { | |
216 | return 1.0 - ((value - axis.minyval) * axis.yscale); | |
217 | } | |
218 | }; | |
219 | ||
74a5af31 | 220 | DygraphLayout.prototype._evaluateLineCharts = function() { |
c3e1495b AR |
221 | // An array to keep track of how many points will be drawn for each set. |
222 | // This will allow for the canvas renderer to not have to check every point | |
fc77253b DV |
223 | // for every data set since the points are added in order of the sets in |
224 | // datasets. | |
758a629f | 225 | this.setPointsLengths = []; |
82c6fe4d | 226 | this.setPointsOffsets = []; |
c3e1495b | 227 | |
04c104d7 | 228 | var connectSeparated = this.attr_('connectSeparatedPoints'); |
b843b52c RK |
229 | // TODO(bhs): these loops are a hot-spot for high-point-count charts. In fact, |
230 | // on chrome+linux, they are 6 times more expensive than iterating through the | |
231 | // points and drawing the lines. The brunt of the cost comes from allocating | |
232 | // the |point| structures. | |
233 | var i = 0; | |
57915b7a | 234 | var setIdx; |
99a77a04 RK |
235 | |
236 | // Preallocating the size of points reduces reallocations, and therefore, | |
237 | // calls to collect garbage. | |
b843b52c | 238 | var totalPoints = 0; |
57915b7a | 239 | for (setIdx = 0; setIdx < this.datasets.length; ++setIdx) { |
b843b52c RK |
240 | totalPoints += this.datasets[setIdx].length; |
241 | } | |
242 | this.points = new Array(totalPoints); | |
243 | ||
57915b7a | 244 | for (setIdx = 0; setIdx < this.datasets.length; ++setIdx) { |
b843b52c | 245 | this.setPointsOffsets.push(i); |
82c6fe4d KW |
246 | var dataset = this.datasets[setIdx]; |
247 | var setName = this.setNames[setIdx]; | |
74a5af31 DV |
248 | var axis = this.dygraph_.axisPropertiesForSeries(setName); |
249 | ||
250 | for (var j = 0; j < dataset.length; j++) { | |
251 | var item = dataset[j]; | |
57915b7a RK |
252 | var xValue = DygraphLayout.parseFloat_(item[0]); |
253 | var yValue = DygraphLayout.parseFloat_(item[1]); | |
ecf9b464 | 254 | |
4ab8db0c | 255 | // Range from 0-1 where 0 represents left and 1 represents right. |
7f028980 DV |
256 | var xNormal = (xValue - this.minxval) * this.xscale; |
257 | // Range from 0-1 where 0 represents top and 1 represents bottom | |
258 | var yNormal = DygraphLayout._calcYNormal(axis, yValue); | |
259 | ||
57915b7a | 260 | if (connectSeparated && item[1] === null) { |
b843b52c RK |
261 | yValue = null; |
262 | } | |
263 | this.points[i] = { | |
f9414b11 DV |
264 | // TODO(danvk): here |
265 | x: xNormal, | |
266 | y: yNormal, | |
267 | xval: xValue, | |
268 | yval: yValue, | |
269 | name: setName | |
270 | }; | |
b843b52c | 271 | i++; |
74a5af31 | 272 | } |
b843b52c | 273 | this.setPointsLengths.push(i - this.setPointsOffsets[setIdx]); |
74a5af31 DV |
274 | } |
275 | }; | |
276 | ||
57915b7a RK |
277 | /** |
278 | * Optimized replacement for parseFloat, which was way too slow when almost | |
279 | * all values were type number, with few edge cases, none of which were strings. | |
280 | */ | |
281 | DygraphLayout.parseFloat_ = function(val) { | |
282 | // parseFloat(null) is NaN | |
283 | if (val === null) { | |
284 | return NaN; | |
285 | } | |
286 | ||
287 | // Assume it's a number or NaN. If it's something else, I'll be shocked. | |
288 | return val; | |
289 | } | |
290 | ||
74a5af31 | 291 | DygraphLayout.prototype._evaluateLineTicks = function() { |
758a629f DV |
292 | var i, tick, label, pos; |
293 | this.xticks = []; | |
294 | for (i = 0; i < this.xTicks_.length; i++) { | |
295 | tick = this.xTicks_[i]; | |
296 | label = tick.label; | |
297 | pos = this.xscale * (tick.v - this.minxval); | |
74a5af31 DV |
298 | if ((pos >= 0.0) && (pos <= 1.0)) { |
299 | this.xticks.push([pos, label]); | |
300 | } | |
301 | } | |
302 | ||
758a629f DV |
303 | this.yticks = []; |
304 | for (i = 0; i < this.yAxes_.length; i++ ) { | |
74a5af31 DV |
305 | var axis = this.yAxes_[i]; |
306 | for (var j = 0; j < axis.ticks.length; j++) { | |
758a629f DV |
307 | tick = axis.ticks[j]; |
308 | label = tick.label; | |
309 | pos = this.dygraph_.toPercentYCoord(tick.v, i); | |
74a5af31 DV |
310 | if ((pos >= 0.0) && (pos <= 1.0)) { |
311 | this.yticks.push([i, pos, label]); | |
312 | } | |
313 | } | |
314 | } | |
315 | }; | |
316 | ||
317 | ||
318 | /** | |
319 | * Behaves the same way as PlotKit.Layout, but also copies the errors | |
320 | * @private | |
321 | */ | |
322 | DygraphLayout.prototype.evaluateWithError = function() { | |
323 | this.evaluate(); | |
324 | if (!(this.attr_('errorBars') || this.attr_('customBars'))) return; | |
325 | ||
326 | // Copy over the error terms | |
758a629f | 327 | var i = 0; // index in this.points |
82c6fe4d | 328 | for (var setIdx = 0; setIdx < this.datasets.length; ++setIdx) { |
74a5af31 | 329 | var j = 0; |
82c6fe4d KW |
330 | var dataset = this.datasets[setIdx]; |
331 | var setName = this.setNames[setIdx]; | |
7f028980 | 332 | var axis = this.dygraph_.axisPropertiesForSeries(setName); |
758a629f | 333 | for (j = 0; j < dataset.length; j++, i++) { |
74a5af31 | 334 | var item = dataset[j]; |
57915b7a RK |
335 | var xv = DygraphLayout.parseFloat_(item[0]); |
336 | var yv = DygraphLayout.parseFloat_(item[1]); | |
74a5af31 DV |
337 | |
338 | if (xv == this.points[i].xval && | |
339 | yv == this.points[i].yval) { | |
57915b7a RK |
340 | var errorMinus = DygraphLayout.parseFloat_(item[2]); |
341 | var errorPlus = DygraphLayout.parseFloat_(item[3]); | |
7f028980 DV |
342 | |
343 | var yv_minus = yv - errorMinus; | |
344 | var yv_plus = yv + errorPlus; | |
345 | this.points[i].y_top = DygraphLayout._calcYNormal(axis, yv_minus); | |
346 | this.points[i].y_bottom = DygraphLayout._calcYNormal(axis, yv_plus); | |
74a5af31 DV |
347 | } |
348 | } | |
349 | } | |
350 | }; | |
351 | ||
352 | DygraphLayout.prototype._evaluateAnnotations = function() { | |
353 | // Add the annotations to the point to which they belong. | |
354 | // Make a map from (setName, xval) to annotation for quick lookups. | |
758a629f | 355 | var i; |
74a5af31 | 356 | var annotations = {}; |
758a629f | 357 | for (i = 0; i < this.annotations.length; i++) { |
74a5af31 DV |
358 | var a = this.annotations[i]; |
359 | annotations[a.xval + "," + a.series] = a; | |
360 | } | |
361 | ||
362 | this.annotated_points = []; | |
d570a072 AR |
363 | |
364 | // Exit the function early if there are no annotations. | |
365 | if (!this.annotations || !this.annotations.length) { | |
366 | return; | |
367 | } | |
ccd9d7c2 | 368 | |
d570a072 | 369 | // TODO(antrob): loop through annotations not points. |
758a629f | 370 | for (i = 0; i < this.points.length; i++) { |
74a5af31 DV |
371 | var p = this.points[i]; |
372 | var k = p.xval + "," + p.name; | |
373 | if (k in annotations) { | |
374 | p.annotation = annotations[k]; | |
375 | this.annotated_points.push(p); | |
376 | } | |
377 | } | |
378 | }; | |
379 | ||
380 | /** | |
381 | * Convenience function to remove all the data sets from a graph | |
382 | */ | |
383 | DygraphLayout.prototype.removeAllDatasets = function() { | |
384 | delete this.datasets; | |
82c6fe4d KW |
385 | delete this.setNames; |
386 | delete this.setPointsLengths; | |
387 | delete this.setPointsOffsets; | |
758a629f | 388 | this.datasets = []; |
82c6fe4d KW |
389 | this.setNames = []; |
390 | this.setPointsLengths = []; | |
391 | this.setPointsOffsets = []; | |
74a5af31 DV |
392 | }; |
393 | ||
394 | /** | |
395 | * Return a copy of the point at the indicated index, with its yval unstacked. | |
396 | * @param int index of point in layout_.points | |
397 | */ | |
398 | DygraphLayout.prototype.unstackPointAtIndex = function(idx) { | |
399 | var point = this.points[idx]; | |
52ca2280 | 400 | // If the point is missing, no unstacking is necessary |
c6bdcd60 AV |
401 | if (!point.yval) { |
402 | return point; | |
403 | } | |
ccd9d7c2 | 404 | |
74a5af31 | 405 | // Clone the point since we modify it |
ccd9d7c2 | 406 | var unstackedPoint = {}; |
758a629f DV |
407 | for (var pt in point) { |
408 | unstackedPoint[pt] = point[pt]; | |
74a5af31 | 409 | } |
ccd9d7c2 | 410 | |
74a5af31 DV |
411 | if (!this.attr_("stackedGraph")) { |
412 | return unstackedPoint; | |
413 | } | |
ccd9d7c2 PF |
414 | |
415 | // The unstacked yval is equal to the current yval minus the yval of the | |
74a5af31 DV |
416 | // next point at the same xval. |
417 | for (var i = idx+1; i < this.points.length; i++) { | |
c6bdcd60 | 418 | if ((this.points[i].xval == point.xval) && this.points[i].yval) { |
ccd9d7c2 | 419 | unstackedPoint.yval -= this.points[i].yval; |
74a5af31 DV |
420 | break; |
421 | } | |
422 | } | |
ccd9d7c2 | 423 | |
74a5af31 | 424 | return unstackedPoint; |
758a629f | 425 | }; |