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 | }; | |
66 | if (this.attr_('drawYAxis')) { | |
67 | area.x = this.attr_('yAxisLabelWidth') + 2 * this.attr_('axisTickSize'); | |
68 | } | |
69 | ||
70 | area.w = this.dygraph_.width_ - area.x - this.attr_('rightGap'); | |
71 | area.h = this.dygraph_.height_; | |
72 | if (this.attr_('drawXAxis')) { | |
73 | if (this.attr_('xAxisHeight')) { | |
74 | area.h -= this.attr_('xAxisHeight'); | |
75 | } else { | |
76 | area.h -= this.attr_('axisLabelFontSize') + 2 * this.attr_('axisTickSize'); | |
77 | } | |
78 | } | |
79 | ||
80 | // Shrink the drawing area to accomodate additional y-axes. | |
81 | if (this.dygraph_.numAxes() == 2) { | |
82 | // TODO(danvk): per-axis setting. | |
83 | area.w -= (this.attr_('yAxisLabelWidth') + 2 * this.attr_('axisTickSize')); | |
84 | } else if (this.dygraph_.numAxes() > 2) { | |
85 | this.dygraph_.error("Only two y-axes are supported at this time. (Trying " + | |
86 | "to use " + this.dygraph_.numAxes() + ")"); | |
87 | } | |
88 | ||
89 | // Add space for chart labels: title, xlabel and ylabel. | |
90 | if (this.attr_('title')) { | |
91 | area.h -= this.attr_('titleHeight'); | |
92 | area.y += this.attr_('titleHeight'); | |
93 | } | |
94 | if (this.attr_('xlabel')) { | |
95 | area.h -= this.attr_('xLabelHeight'); | |
96 | } | |
97 | if (this.attr_('ylabel')) { | |
98 | // It would make sense to shift the chart here to make room for the y-axis | |
99 | // label, but the default yAxisLabelWidth is large enough that this results | |
100 | // in overly-padded charts. The y-axis label should fit fine. If it | |
101 | // doesn't, the yAxisLabelWidth option can be increased. | |
102 | } | |
103 | ||
d0c39108 DV |
104 | if (this.attr_('y2label')) { |
105 | // same logic applies here as for ylabel. | |
106 | // TODO(danvk): make yAxisLabelWidth a per-axis property | |
107 | } | |
108 | ||
ccd9d7c2 PF |
109 | // Add space for range selector, if needed. |
110 | if (this.attr_('showRangeSelector')) { | |
111 | area.h -= this.attr_('rangeSelectorHeight') + 4; | |
112 | } | |
113 | ||
114 | return area; | |
115 | }; | |
116 | ||
74a5af31 DV |
117 | DygraphLayout.prototype.setAnnotations = function(ann) { |
118 | // The Dygraph object's annotations aren't parsed. We parse them here and | |
119 | // save a copy. If there is no parser, then the user must be using raw format. | |
120 | this.annotations = []; | |
121 | var parse = this.attr_('xValueParser') || function(x) { return x; }; | |
122 | for (var i = 0; i < ann.length; i++) { | |
123 | var a = {}; | |
124 | if (!ann[i].xval && !ann[i].x) { | |
125 | this.dygraph_.error("Annotations must have an 'x' property"); | |
126 | return; | |
127 | } | |
128 | if (ann[i].icon && | |
129 | !(ann[i].hasOwnProperty('width') && | |
130 | ann[i].hasOwnProperty('height'))) { | |
131 | this.dygraph_.error("Must set width and height when setting " + | |
132 | "annotation.icon property"); | |
133 | return; | |
134 | } | |
135 | Dygraph.update(a, ann[i]); | |
136 | if (!a.xval) a.xval = parse(a.x); | |
137 | this.annotations.push(a); | |
138 | } | |
139 | }; | |
140 | ||
141 | DygraphLayout.prototype.setXTicks = function(xTicks) { | |
142 | this.xTicks_ = xTicks; | |
143 | }; | |
144 | ||
145 | // TODO(danvk): add this to the Dygraph object's API or move it into Layout. | |
146 | DygraphLayout.prototype.setYAxes = function (yAxes) { | |
147 | this.yAxes_ = yAxes; | |
148 | }; | |
149 | ||
150 | DygraphLayout.prototype.setDateWindow = function(dateWindow) { | |
151 | this.dateWindow_ = dateWindow; | |
152 | }; | |
153 | ||
154 | DygraphLayout.prototype.evaluate = function() { | |
155 | this._evaluateLimits(); | |
156 | this._evaluateLineCharts(); | |
157 | this._evaluateLineTicks(); | |
158 | this._evaluateAnnotations(); | |
159 | }; | |
160 | ||
161 | DygraphLayout.prototype._evaluateLimits = function() { | |
162 | this.minxval = this.maxxval = null; | |
163 | if (this.dateWindow_) { | |
164 | this.minxval = this.dateWindow_[0]; | |
165 | this.maxxval = this.dateWindow_[1]; | |
166 | } else { | |
82c6fe4d KW |
167 | for (var setIdx = 0; setIdx < this.datasets.length; ++setIdx) { |
168 | var series = this.datasets[setIdx]; | |
74a5af31 DV |
169 | if (series.length > 1) { |
170 | var x1 = series[0][0]; | |
171 | if (!this.minxval || x1 < this.minxval) this.minxval = x1; | |
ccd9d7c2 | 172 | |
74a5af31 DV |
173 | var x2 = series[series.length - 1][0]; |
174 | if (!this.maxxval || x2 > this.maxxval) this.maxxval = x2; | |
175 | } | |
176 | } | |
177 | } | |
178 | this.xrange = this.maxxval - this.minxval; | |
758a629f | 179 | this.xscale = (this.xrange !== 0 ? 1/this.xrange : 1.0); |
74a5af31 DV |
180 | |
181 | for (var i = 0; i < this.yAxes_.length; i++) { | |
182 | var axis = this.yAxes_[i]; | |
183 | axis.minyval = axis.computedValueRange[0]; | |
184 | axis.maxyval = axis.computedValueRange[1]; | |
185 | axis.yrange = axis.maxyval - axis.minyval; | |
758a629f | 186 | axis.yscale = (axis.yrange !== 0 ? 1.0 / axis.yrange : 1.0); |
74a5af31 DV |
187 | |
188 | if (axis.g.attr_("logscale")) { | |
189 | axis.ylogrange = Dygraph.log10(axis.maxyval) - Dygraph.log10(axis.minyval); | |
758a629f | 190 | axis.ylogscale = (axis.ylogrange !== 0 ? 1.0 / axis.ylogrange : 1.0); |
74a5af31 DV |
191 | if (!isFinite(axis.ylogrange) || isNaN(axis.ylogrange)) { |
192 | axis.g.error('axis ' + i + ' of graph at ' + axis.g + | |
193 | ' can\'t be displayed in log scale for range [' + | |
194 | axis.minyval + ' - ' + axis.maxyval + ']'); | |
195 | } | |
196 | } | |
197 | } | |
198 | }; | |
199 | ||
7f028980 DV |
200 | DygraphLayout._calcYNormal = function(axis, value) { |
201 | if (axis.logscale) { | |
202 | return 1.0 - ((Dygraph.log10(value) - Dygraph.log10(axis.minyval)) * axis.ylogscale); | |
203 | } else { | |
204 | return 1.0 - ((value - axis.minyval) * axis.yscale); | |
205 | } | |
206 | }; | |
207 | ||
74a5af31 | 208 | DygraphLayout.prototype._evaluateLineCharts = function() { |
c3e1495b AR |
209 | // An array to keep track of how many points will be drawn for each set. |
210 | // This will allow for the canvas renderer to not have to check every point | |
fc77253b DV |
211 | // for every data set since the points are added in order of the sets in |
212 | // datasets. | |
758a629f | 213 | this.setPointsLengths = []; |
82c6fe4d | 214 | this.setPointsOffsets = []; |
c3e1495b | 215 | |
04c104d7 | 216 | var connectSeparated = this.attr_('connectSeparatedPoints'); |
b843b52c RK |
217 | // TODO(bhs): these loops are a hot-spot for high-point-count charts. In fact, |
218 | // on chrome+linux, they are 6 times more expensive than iterating through the | |
219 | // points and drawing the lines. The brunt of the cost comes from allocating | |
220 | // the |point| structures. | |
221 | var i = 0; | |
57915b7a | 222 | var setIdx; |
99a77a04 RK |
223 | |
224 | // Preallocating the size of points reduces reallocations, and therefore, | |
225 | // calls to collect garbage. | |
b843b52c | 226 | var totalPoints = 0; |
57915b7a | 227 | for (setIdx = 0; setIdx < this.datasets.length; ++setIdx) { |
b843b52c RK |
228 | totalPoints += this.datasets[setIdx].length; |
229 | } | |
230 | this.points = new Array(totalPoints); | |
231 | ||
57915b7a | 232 | for (setIdx = 0; setIdx < this.datasets.length; ++setIdx) { |
b843b52c | 233 | this.setPointsOffsets.push(i); |
82c6fe4d KW |
234 | var dataset = this.datasets[setIdx]; |
235 | var setName = this.setNames[setIdx]; | |
74a5af31 DV |
236 | var axis = this.dygraph_.axisPropertiesForSeries(setName); |
237 | ||
238 | for (var j = 0; j < dataset.length; j++) { | |
239 | var item = dataset[j]; | |
57915b7a RK |
240 | var xValue = DygraphLayout.parseFloat_(item[0]); |
241 | var yValue = DygraphLayout.parseFloat_(item[1]); | |
ecf9b464 | 242 | |
4ab8db0c | 243 | // Range from 0-1 where 0 represents left and 1 represents right. |
7f028980 DV |
244 | var xNormal = (xValue - this.minxval) * this.xscale; |
245 | // Range from 0-1 where 0 represents top and 1 represents bottom | |
246 | var yNormal = DygraphLayout._calcYNormal(axis, yValue); | |
247 | ||
57915b7a | 248 | if (connectSeparated && item[1] === null) { |
b843b52c RK |
249 | yValue = null; |
250 | } | |
251 | this.points[i] = { | |
f9414b11 DV |
252 | // TODO(danvk): here |
253 | x: xNormal, | |
254 | y: yNormal, | |
255 | xval: xValue, | |
256 | yval: yValue, | |
257 | name: setName | |
258 | }; | |
b843b52c | 259 | i++; |
74a5af31 | 260 | } |
b843b52c | 261 | this.setPointsLengths.push(i - this.setPointsOffsets[setIdx]); |
74a5af31 DV |
262 | } |
263 | }; | |
264 | ||
57915b7a RK |
265 | /** |
266 | * Optimized replacement for parseFloat, which was way too slow when almost | |
267 | * all values were type number, with few edge cases, none of which were strings. | |
268 | */ | |
269 | DygraphLayout.parseFloat_ = function(val) { | |
270 | // parseFloat(null) is NaN | |
271 | if (val === null) { | |
272 | return NaN; | |
273 | } | |
274 | ||
275 | // Assume it's a number or NaN. If it's something else, I'll be shocked. | |
276 | return val; | |
277 | } | |
278 | ||
74a5af31 | 279 | DygraphLayout.prototype._evaluateLineTicks = function() { |
758a629f DV |
280 | var i, tick, label, pos; |
281 | this.xticks = []; | |
282 | for (i = 0; i < this.xTicks_.length; i++) { | |
283 | tick = this.xTicks_[i]; | |
284 | label = tick.label; | |
285 | pos = this.xscale * (tick.v - this.minxval); | |
74a5af31 DV |
286 | if ((pos >= 0.0) && (pos <= 1.0)) { |
287 | this.xticks.push([pos, label]); | |
288 | } | |
289 | } | |
290 | ||
758a629f DV |
291 | this.yticks = []; |
292 | for (i = 0; i < this.yAxes_.length; i++ ) { | |
74a5af31 DV |
293 | var axis = this.yAxes_[i]; |
294 | for (var j = 0; j < axis.ticks.length; j++) { | |
758a629f DV |
295 | tick = axis.ticks[j]; |
296 | label = tick.label; | |
297 | pos = this.dygraph_.toPercentYCoord(tick.v, i); | |
74a5af31 DV |
298 | if ((pos >= 0.0) && (pos <= 1.0)) { |
299 | this.yticks.push([i, pos, label]); | |
300 | } | |
301 | } | |
302 | } | |
303 | }; | |
304 | ||
305 | ||
306 | /** | |
307 | * Behaves the same way as PlotKit.Layout, but also copies the errors | |
308 | * @private | |
309 | */ | |
310 | DygraphLayout.prototype.evaluateWithError = function() { | |
311 | this.evaluate(); | |
312 | if (!(this.attr_('errorBars') || this.attr_('customBars'))) return; | |
313 | ||
314 | // Copy over the error terms | |
758a629f | 315 | var i = 0; // index in this.points |
82c6fe4d | 316 | for (var setIdx = 0; setIdx < this.datasets.length; ++setIdx) { |
74a5af31 | 317 | var j = 0; |
82c6fe4d KW |
318 | var dataset = this.datasets[setIdx]; |
319 | var setName = this.setNames[setIdx]; | |
7f028980 | 320 | var axis = this.dygraph_.axisPropertiesForSeries(setName); |
758a629f | 321 | for (j = 0; j < dataset.length; j++, i++) { |
74a5af31 | 322 | var item = dataset[j]; |
57915b7a RK |
323 | var xv = DygraphLayout.parseFloat_(item[0]); |
324 | var yv = DygraphLayout.parseFloat_(item[1]); | |
74a5af31 DV |
325 | |
326 | if (xv == this.points[i].xval && | |
327 | yv == this.points[i].yval) { | |
57915b7a RK |
328 | var errorMinus = DygraphLayout.parseFloat_(item[2]); |
329 | var errorPlus = DygraphLayout.parseFloat_(item[3]); | |
7f028980 DV |
330 | |
331 | var yv_minus = yv - errorMinus; | |
332 | var yv_plus = yv + errorPlus; | |
333 | this.points[i].y_top = DygraphLayout._calcYNormal(axis, yv_minus); | |
334 | this.points[i].y_bottom = DygraphLayout._calcYNormal(axis, yv_plus); | |
74a5af31 DV |
335 | } |
336 | } | |
337 | } | |
338 | }; | |
339 | ||
340 | DygraphLayout.prototype._evaluateAnnotations = function() { | |
341 | // Add the annotations to the point to which they belong. | |
342 | // Make a map from (setName, xval) to annotation for quick lookups. | |
758a629f | 343 | var i; |
74a5af31 | 344 | var annotations = {}; |
758a629f | 345 | for (i = 0; i < this.annotations.length; i++) { |
74a5af31 DV |
346 | var a = this.annotations[i]; |
347 | annotations[a.xval + "," + a.series] = a; | |
348 | } | |
349 | ||
350 | this.annotated_points = []; | |
d570a072 AR |
351 | |
352 | // Exit the function early if there are no annotations. | |
353 | if (!this.annotations || !this.annotations.length) { | |
354 | return; | |
355 | } | |
ccd9d7c2 | 356 | |
d570a072 | 357 | // TODO(antrob): loop through annotations not points. |
758a629f | 358 | for (i = 0; i < this.points.length; i++) { |
74a5af31 DV |
359 | var p = this.points[i]; |
360 | var k = p.xval + "," + p.name; | |
361 | if (k in annotations) { | |
362 | p.annotation = annotations[k]; | |
363 | this.annotated_points.push(p); | |
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 | */ | |
386 | DygraphLayout.prototype.unstackPointAtIndex = function(idx) { | |
387 | var point = this.points[idx]; | |
52ca2280 | 388 | // If the point is missing, no unstacking is necessary |
c6bdcd60 AV |
389 | if (!point.yval) { |
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 DV |
404 | // next point at the same xval. |
405 | for (var i = idx+1; i < this.points.length; i++) { | |
c6bdcd60 | 406 | if ((this.points[i].xval == point.xval) && this.points[i].yval) { |
ccd9d7c2 | 407 | unstackedPoint.yval -= this.points[i].yval; |
74a5af31 DV |
408 | break; |
409 | } | |
410 | } | |
ccd9d7c2 | 411 | |
74a5af31 | 412 | return unstackedPoint; |
758a629f | 413 | }; |