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