Commit | Line | Data |
---|---|---|
6a1aa64f DV |
1 | // Copyright 2006 Dan Vanderkam (danvdk@gmail.com) |
2 | // All Rights Reserved. | |
3 | ||
4 | /** | |
3df0ccf0 DV |
5 | * @fileoverview Based on PlotKit, but modified to meet the needs of dygraphs. |
6 | * In particular, support for: | |
0abfbd7e | 7 | * - grid overlays |
3df0ccf0 DV |
8 | * - error bars |
9 | * - dygraphs attribute system | |
6a1aa64f DV |
10 | */ |
11 | ||
6a1aa64f | 12 | /** |
3df0ccf0 | 13 | * Creates a new DygraphLayout object. |
6a1aa64f | 14 | * @param {Object} options Options for PlotKit.Layout |
285a6bda | 15 | * @return {Object} The DygraphLayout object |
6a1aa64f | 16 | */ |
efe0829a DV |
17 | DygraphLayout = function(dygraph, options) { |
18 | this.dygraph_ = dygraph; | |
19 | this.options = {}; // TODO(danvk): remove, use attr_ instead. | |
fc80a396 | 20 | Dygraph.update(this.options, options ? options : {}); |
efe0829a | 21 | this.datasets = new Array(); |
937029df | 22 | this.annotations = new Array(); |
6a1aa64f | 23 | }; |
efe0829a DV |
24 | |
25 | DygraphLayout.prototype.attr_ = function(name) { | |
26 | return this.dygraph_.attr_(name); | |
27 | }; | |
28 | ||
29 | DygraphLayout.prototype.addDataset = function(setname, set_xy) { | |
30 | this.datasets[setname] = set_xy; | |
31 | }; | |
32 | ||
5c528fa2 DV |
33 | DygraphLayout.prototype.setAnnotations = function(ann) { |
34 | // The Dygraph object's annotations aren't parsed. We parse them here and | |
35 | // save a copy. | |
973e2b79 | 36 | this.annotations = []; |
5c528fa2 DV |
37 | var parse = this.attr_('xValueParser'); |
38 | for (var i = 0; i < ann.length; i++) { | |
39 | var a = {}; | |
a685723c | 40 | if (!ann[i].xval && !ann[i].x) { |
5c528fa2 DV |
41 | this.dygraph_.error("Annotations must have an 'x' property"); |
42 | return; | |
43 | } | |
ce5e8d36 | 44 | if (ann[i].icon && |
33030f33 DV |
45 | !(ann[i].hasOwnProperty('width') && |
46 | ann[i].hasOwnProperty('height'))) { | |
47 | this.dygraph_.error("Must set width and height when setting " + | |
ce5e8d36 DV |
48 | "annotation.icon property"); |
49 | return; | |
50 | } | |
5c528fa2 | 51 | Dygraph.update(a, ann[i]); |
a685723c | 52 | if (!a.xval) a.xval = parse(a.x); |
5c528fa2 | 53 | this.annotations.push(a); |
ce49c2fa | 54 | } |
ce49c2fa DV |
55 | }; |
56 | ||
efe0829a DV |
57 | DygraphLayout.prototype.evaluate = function() { |
58 | this._evaluateLimits(); | |
59 | this._evaluateLineCharts(); | |
60 | this._evaluateLineTicks(); | |
ce49c2fa | 61 | this._evaluateAnnotations(); |
efe0829a DV |
62 | }; |
63 | ||
64 | DygraphLayout.prototype._evaluateLimits = function() { | |
65 | this.minxval = this.maxxval = null; | |
f6401bf6 DV |
66 | if (this.options.dateWindow) { |
67 | this.minxval = this.options.dateWindow[0]; | |
68 | this.maxxval = this.options.dateWindow[1]; | |
69 | } else { | |
70 | for (var name in this.datasets) { | |
71 | if (!this.datasets.hasOwnProperty(name)) continue; | |
72 | var series = this.datasets[name]; | |
48841144 NN |
73 | if (series.length > 1) { |
74 | var x1 = series[0][0]; | |
75 | if (!this.minxval || x1 < this.minxval) this.minxval = x1; | |
76 | ||
77 | var x2 = series[series.length - 1][0]; | |
78 | if (!this.maxxval || x2 > this.maxxval) this.maxxval = x2; | |
79 | } | |
f6401bf6 | 80 | } |
efe0829a DV |
81 | } |
82 | this.xrange = this.maxxval - this.minxval; | |
83 | this.xscale = (this.xrange != 0 ? 1/this.xrange : 1.0); | |
84 | ||
ea4942ed DV |
85 | for (var i = 0; i < this.options.yAxes.length; i++) { |
86 | var axis = this.options.yAxes[i]; | |
26ca7938 DV |
87 | axis.minyval = axis.computedValueRange[0]; |
88 | axis.maxyval = axis.computedValueRange[1]; | |
ea4942ed DV |
89 | axis.yrange = axis.maxyval - axis.minyval; |
90 | axis.yscale = (axis.yrange != 0 ? 1.0 / axis.yrange : 1.0); | |
ff022deb | 91 | |
0037b2a4 | 92 | axis.ylogrange = Dygraph.log10(axis.maxyval) - Dygraph.log10(axis.minyval); |
ff022deb | 93 | axis.ylogscale = (axis.ylogrange != 0 ? 1.0 / axis.ylogrange : 1.0); |
d03e78ed RK |
94 | if (axis.g.attr_("logscale") && isNaN(axis.ylogrange)) { |
95 | axis.g.error('axis ' + i + ' can\'t be displayed in log scale for range [' + | |
96 | axis.minyval + ' - ' + axis.maxyval + ']'); | |
97 | } | |
ea4942ed | 98 | } |
efe0829a DV |
99 | }; |
100 | ||
101 | DygraphLayout.prototype._evaluateLineCharts = function() { | |
102 | // add all the rects | |
103 | this.points = new Array(); | |
104 | for (var setName in this.datasets) { | |
85b99f0b DV |
105 | if (!this.datasets.hasOwnProperty(setName)) continue; |
106 | ||
107 | var dataset = this.datasets[setName]; | |
ea4942ed DV |
108 | var axis = this.options.yAxes[this.options.seriesToAxisMap[setName]]; |
109 | ||
85b99f0b DV |
110 | for (var j = 0; j < dataset.length; j++) { |
111 | var item = dataset[j]; | |
d03e78ed | 112 | |
3637724f | 113 | var yval; |
7d0e7a0d | 114 | if (axis.logscale) { |
0037b2a4 | 115 | yval = 1.0 - ((Dygraph.log10(parseFloat(item[1])) - Dygraph.log10(axis.minyval)) * axis.ylogscale); // really should just be yscale. |
ff022deb | 116 | } else { |
3637724f | 117 | yval = 1.0 - ((parseFloat(item[1]) - axis.minyval) * axis.yscale); |
ff022deb | 118 | } |
85b99f0b | 119 | var point = { |
ff00d3e2 | 120 | // TODO(danvk): here |
85b99f0b | 121 | x: ((parseFloat(item[0]) - this.minxval) * this.xscale), |
3637724f | 122 | y: yval, |
85b99f0b DV |
123 | xval: parseFloat(item[0]), |
124 | yval: parseFloat(item[1]), | |
125 | name: setName | |
126 | }; | |
127 | ||
1a26f3fb | 128 | this.points.push(point); |
85b99f0b | 129 | } |
efe0829a DV |
130 | } |
131 | }; | |
132 | ||
133 | DygraphLayout.prototype._evaluateLineTicks = function() { | |
134 | this.xticks = new Array(); | |
135 | for (var i = 0; i < this.options.xTicks.length; i++) { | |
136 | var tick = this.options.xTicks[i]; | |
137 | var label = tick.label; | |
138 | var pos = this.xscale * (tick.v - this.minxval); | |
139 | if ((pos >= 0.0) && (pos <= 1.0)) { | |
140 | this.xticks.push([pos, label]); | |
141 | } | |
142 | } | |
143 | ||
144 | this.yticks = new Array(); | |
9012dd21 | 145 | for (var i = 0; i < this.options.yAxes.length; i++ ) { |
67076b22 DV |
146 | var axis = this.options.yAxes[i]; |
147 | for (var j = 0; j < axis.ticks.length; j++) { | |
148 | var tick = axis.ticks[j]; | |
149 | var label = tick.label; | |
ff022deb | 150 | var pos = this.dygraph_.toPercentYCoord(tick.v, i); |
67076b22 | 151 | if ((pos >= 0.0) && (pos <= 1.0)) { |
9012dd21 | 152 | this.yticks.push([i, pos, label]); |
67076b22 | 153 | } |
efe0829a DV |
154 | } |
155 | } | |
156 | }; | |
157 | ||
6a1aa64f DV |
158 | |
159 | /** | |
160 | * Behaves the same way as PlotKit.Layout, but also copies the errors | |
161 | * @private | |
162 | */ | |
285a6bda | 163 | DygraphLayout.prototype.evaluateWithError = function() { |
6a1aa64f DV |
164 | this.evaluate(); |
165 | if (!this.options.errorBars) return; | |
166 | ||
167 | // Copy over the error terms | |
168 | var i = 0; // index in this.points | |
169 | for (var setName in this.datasets) { | |
85b99f0b DV |
170 | if (!this.datasets.hasOwnProperty(setName)) continue; |
171 | var j = 0; | |
172 | var dataset = this.datasets[setName]; | |
173 | for (var j = 0; j < dataset.length; j++, i++) { | |
174 | var item = dataset[j]; | |
175 | var xv = parseFloat(item[0]); | |
176 | var yv = parseFloat(item[1]); | |
177 | ||
178 | if (xv == this.points[i].xval && | |
179 | yv == this.points[i].yval) { | |
180 | this.points[i].errorMinus = parseFloat(item[2]); | |
181 | this.points[i].errorPlus = parseFloat(item[3]); | |
182 | } | |
183 | } | |
6a1aa64f DV |
184 | } |
185 | }; | |
186 | ||
ce49c2fa DV |
187 | DygraphLayout.prototype._evaluateAnnotations = function() { |
188 | // Add the annotations to the point to which they belong. | |
189 | // Make a map from (setName, xval) to annotation for quick lookups. | |
190 | var annotations = {}; | |
191 | for (var i = 0; i < this.annotations.length; i++) { | |
192 | var a = this.annotations[i]; | |
193 | annotations[a.xval + "," + a.series] = a; | |
194 | } | |
195 | ||
196 | this.annotated_points = []; | |
197 | for (var i = 0; i < this.points.length; i++) { | |
198 | var p = this.points[i]; | |
199 | var k = p.xval + "," + p.name; | |
200 | if (k in annotations) { | |
201 | p.annotation = annotations[k]; | |
202 | this.annotated_points.push(p); | |
203 | } | |
204 | } | |
205 | }; | |
206 | ||
6a1aa64f DV |
207 | /** |
208 | * Convenience function to remove all the data sets from a graph | |
209 | */ | |
285a6bda | 210 | DygraphLayout.prototype.removeAllDatasets = function() { |
6a1aa64f DV |
211 | delete this.datasets; |
212 | this.datasets = new Array(); | |
213 | }; | |
214 | ||
215 | /** | |
216 | * Change the values of various layout options | |
217 | * @param {Object} new_options an associative array of new properties | |
218 | */ | |
285a6bda | 219 | DygraphLayout.prototype.updateOptions = function(new_options) { |
fc80a396 | 220 | Dygraph.update(this.options, new_options ? new_options : {}); |
6a1aa64f DV |
221 | }; |
222 | ||
667d510b | 223 | /** |
224 | * Return a copy of the point at the indicated index, with its yval unstacked. | |
225 | * @param int index of point in layout_.points | |
226 | */ | |
8c03ba63 | 227 | DygraphLayout.prototype.unstackPointAtIndex = function(idx) { |
667d510b | 228 | var point = this.points[idx]; |
229 | ||
230 | // Clone the point since we modify it | |
231 | var unstackedPoint = {}; | |
232 | for (var i in point) { | |
233 | unstackedPoint[i] = point[i]; | |
234 | } | |
235 | ||
236 | if (!this.attr_("stackedGraph")) { | |
237 | return unstackedPoint; | |
238 | } | |
239 | ||
240 | // The unstacked yval is equal to the current yval minus the yval of the | |
241 | // next point at the same xval. | |
242 | for (var i = idx+1; i < this.points.length; i++) { | |
243 | if (this.points[i].xval == point.xval) { | |
244 | unstackedPoint.yval -= this.points[i].yval; | |
245 | break; | |
246 | } | |
247 | } | |
248 | ||
249 | return unstackedPoint; | |
250 | } | |
251 | ||
6a1aa64f DV |
252 | // Subclass PlotKit.CanvasRenderer to add: |
253 | // 1. X/Y grid overlay | |
254 | // 2. Ability to draw error bars (if required) | |
255 | ||
256 | /** | |
257 | * Sets some PlotKit.CanvasRenderer options | |
258 | * @param {Object} element The canvas to attach to | |
285a6bda | 259 | * @param {Layout} layout The DygraphLayout object for this graph. |
6a1aa64f DV |
260 | * @param {Object} options Options to pass on to CanvasRenderer |
261 | */ | |
9317362d DV |
262 | DygraphCanvasRenderer = function(dygraph, element, layout, options) { |
263 | // TODO(danvk): remove options, just use dygraph.attr_. | |
9317362d | 264 | this.dygraph_ = dygraph; |
fbe31dc8 DV |
265 | |
266 | // default options | |
267 | this.options = { | |
f474c2a3 DV |
268 | "strokeWidth": 0.5, |
269 | "drawXAxis": true, | |
270 | "drawYAxis": true, | |
271 | "axisLineColor": "black", | |
272 | "axisLineWidth": 0.5, | |
273 | "axisTickSize": 3, | |
274 | "axisLabelColor": "black", | |
275 | "axisLabelFont": "Arial", | |
276 | "axisLabelFontSize": 9, | |
277 | "axisLabelWidth": 50, | |
278 | "drawYGrid": true, | |
279 | "drawXGrid": true, | |
43af96e7 | 280 | "gridLineColor": "rgb(128,128,128)", |
e7746234 EC |
281 | "fillAlpha": 0.15, |
282 | "underlayCallback": null | |
fbe31dc8 | 283 | }; |
fc80a396 | 284 | Dygraph.update(this.options, options); |
6a1aa64f | 285 | |
fbe31dc8 | 286 | this.layout = layout; |
b0c3b730 | 287 | this.element = element; |
fbe31dc8 DV |
288 | this.container = this.element.parentNode; |
289 | ||
fbe31dc8 DV |
290 | this.height = this.element.height; |
291 | this.width = this.element.width; | |
292 | ||
293 | // --- check whether everything is ok before we return | |
294 | if (!this.isIE && !(DygraphCanvasRenderer.isSupported(this.element))) | |
295 | throw "Canvas is not supported."; | |
296 | ||
297 | // internal state | |
298 | this.xlabels = new Array(); | |
299 | this.ylabels = new Array(); | |
ce49c2fa | 300 | this.annotations = new Array(); |
fbe31dc8 | 301 | |
ea4942ed | 302 | // TODO(danvk): consider all axes in this computation. |
fbe31dc8 | 303 | this.area = { |
9012dd21 | 304 | // TODO(danvk): per-axis setting. |
fbe31dc8 DV |
305 | x: this.options.yAxisLabelWidth + 2 * this.options.axisTickSize, |
306 | y: 0 | |
307 | }; | |
308 | this.area.w = this.width - this.area.x - this.options.rightGap; | |
309 | this.area.h = this.height - this.options.axisLabelFontSize - | |
310 | 2 * this.options.axisTickSize; | |
311 | ||
26ca7938 DV |
312 | // Shrink the drawing area to accomodate additional y-axes. |
313 | if (this.dygraph_.numAxes() == 2) { | |
314 | // TODO(danvk): per-axis setting. | |
315 | this.area.w -= (this.options.yAxisLabelWidth + 2 * this.options.axisTickSize); | |
316 | } else if (this.dygraph_.numAxes() > 2) { | |
317 | this.dygraph_.error("Only two y-axes are supported at this time. (Trying " + | |
34de8b76 | 318 | "to use " + this.dygraph_.numAxes() + ")"); |
26ca7938 DV |
319 | } |
320 | ||
b0c3b730 DV |
321 | this.container.style.position = "relative"; |
322 | this.container.style.width = this.width + "px"; | |
26ca7938 DV |
323 | |
324 | // Set up a clipping area for the canvas (and the interaction canvas). | |
325 | // This ensures that we don't overdraw. | |
8c21adcf | 326 | var ctx = this.dygraph_.canvas_.getContext("2d"); |
26ca7938 DV |
327 | ctx.beginPath(); |
328 | ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h); | |
329 | ctx.clip(); | |
330 | ||
8c21adcf | 331 | ctx = this.dygraph_.hidden_.getContext("2d"); |
26ca7938 DV |
332 | ctx.beginPath(); |
333 | ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h); | |
334 | ctx.clip(); | |
fbe31dc8 DV |
335 | }; |
336 | ||
44c6bc29 DV |
337 | DygraphCanvasRenderer.prototype.attr_ = function(x) { |
338 | return this.dygraph_.attr_(x); | |
339 | }; | |
340 | ||
fbe31dc8 DV |
341 | DygraphCanvasRenderer.prototype.clear = function() { |
342 | if (this.isIE) { | |
343 | // VML takes a while to start up, so we just poll every this.IEDelay | |
344 | try { | |
345 | if (this.clearDelay) { | |
346 | this.clearDelay.cancel(); | |
347 | this.clearDelay = null; | |
348 | } | |
349 | var context = this.element.getContext("2d"); | |
350 | } | |
351 | catch (e) { | |
76171648 | 352 | // TODO(danvk): this is broken, since MochiKit.Async is gone. |
fbe31dc8 DV |
353 | this.clearDelay = MochiKit.Async.wait(this.IEDelay); |
354 | this.clearDelay.addCallback(bind(this.clear, this)); | |
355 | return; | |
356 | } | |
357 | } | |
358 | ||
359 | var context = this.element.getContext("2d"); | |
360 | context.clearRect(0, 0, this.width, this.height); | |
361 | ||
2160ed4a | 362 | for (var i = 0; i < this.xlabels.length; i++) { |
b0c3b730 | 363 | var el = this.xlabels[i]; |
b304aaec | 364 | if (el.parentNode) el.parentNode.removeChild(el); |
2160ed4a DV |
365 | } |
366 | for (var i = 0; i < this.ylabels.length; i++) { | |
b0c3b730 | 367 | var el = this.ylabels[i]; |
b304aaec | 368 | if (el.parentNode) el.parentNode.removeChild(el); |
2160ed4a | 369 | } |
ce49c2fa DV |
370 | for (var i = 0; i < this.annotations.length; i++) { |
371 | var el = this.annotations[i]; | |
b304aaec | 372 | if (el.parentNode) el.parentNode.removeChild(el); |
ce49c2fa | 373 | } |
fbe31dc8 DV |
374 | this.xlabels = new Array(); |
375 | this.ylabels = new Array(); | |
ce49c2fa | 376 | this.annotations = new Array(); |
fbe31dc8 DV |
377 | }; |
378 | ||
379 | ||
380 | DygraphCanvasRenderer.isSupported = function(canvasName) { | |
381 | var canvas = null; | |
382 | try { | |
21d3323f | 383 | if (typeof(canvasName) == 'undefined' || canvasName == null) |
b0c3b730 | 384 | canvas = document.createElement("canvas"); |
fbe31dc8 | 385 | else |
b0c3b730 | 386 | canvas = canvasName; |
fbe31dc8 DV |
387 | var context = canvas.getContext("2d"); |
388 | } | |
389 | catch (e) { | |
390 | var ie = navigator.appVersion.match(/MSIE (\d\.\d)/); | |
391 | var opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1); | |
392 | if ((!ie) || (ie[1] < 6) || (opera)) | |
393 | return false; | |
394 | return true; | |
395 | } | |
396 | return true; | |
6a1aa64f | 397 | }; |
6a1aa64f DV |
398 | |
399 | /** | |
400 | * Draw an X/Y grid on top of the existing plot | |
401 | */ | |
285a6bda | 402 | DygraphCanvasRenderer.prototype.render = function() { |
528ce7e5 DV |
403 | // Draw the new X/Y grid. Lines appear crisper when pixels are rounded to |
404 | // half-integers. This prevents them from drawing in two rows/cols. | |
6a1aa64f | 405 | var ctx = this.element.getContext("2d"); |
528ce7e5 DV |
406 | function halfUp(x){return Math.round(x)+0.5}; |
407 | function halfDown(y){return Math.round(y)-0.5}; | |
e7746234 EC |
408 | |
409 | if (this.options.underlayCallback) { | |
1e41bd2d DV |
410 | // NOTE: we pass the dygraph object to this callback twice to avoid breaking |
411 | // users who expect a deprecated form of this callback. | |
412 | this.options.underlayCallback(ctx, this.area, this.dygraph_, this.dygraph_); | |
e7746234 EC |
413 | } |
414 | ||
6a1aa64f DV |
415 | if (this.options.drawYGrid) { |
416 | var ticks = this.layout.yticks; | |
417 | ctx.save(); | |
f2f24402 | 418 | ctx.strokeStyle = this.options.gridLineColor; |
6a1aa64f DV |
419 | ctx.lineWidth = this.options.axisLineWidth; |
420 | for (var i = 0; i < ticks.length; i++) { | |
880a574f DV |
421 | // TODO(danvk): allow secondary axes to draw a grid, too. |
422 | if (ticks[i][0] != 0) continue; | |
528ce7e5 DV |
423 | var x = halfUp(this.area.x); |
424 | var y = halfDown(this.area.y + ticks[i][1] * this.area.h); | |
6a1aa64f DV |
425 | ctx.beginPath(); |
426 | ctx.moveTo(x, y); | |
427 | ctx.lineTo(x + this.area.w, y); | |
428 | ctx.closePath(); | |
429 | ctx.stroke(); | |
430 | } | |
431 | } | |
432 | ||
433 | if (this.options.drawXGrid) { | |
434 | var ticks = this.layout.xticks; | |
435 | ctx.save(); | |
f2f24402 | 436 | ctx.strokeStyle = this.options.gridLineColor; |
6a1aa64f DV |
437 | ctx.lineWidth = this.options.axisLineWidth; |
438 | for (var i=0; i<ticks.length; i++) { | |
528ce7e5 DV |
439 | var x = halfUp(this.area.x + ticks[i][0] * this.area.w); |
440 | var y = halfDown(this.area.y + this.area.h); | |
6a1aa64f | 441 | ctx.beginPath(); |
880a574f | 442 | ctx.moveTo(x, y); |
6a1aa64f DV |
443 | ctx.lineTo(x, this.area.y); |
444 | ctx.closePath(); | |
445 | ctx.stroke(); | |
446 | } | |
447 | } | |
2ce09b19 DV |
448 | |
449 | // Do the ordinary rendering, as before | |
2ce09b19 | 450 | this._renderLineChart(); |
fbe31dc8 | 451 | this._renderAxis(); |
ce49c2fa | 452 | this._renderAnnotations(); |
fbe31dc8 DV |
453 | }; |
454 | ||
455 | ||
456 | DygraphCanvasRenderer.prototype._renderAxis = function() { | |
457 | if (!this.options.drawXAxis && !this.options.drawYAxis) | |
458 | return; | |
459 | ||
528ce7e5 DV |
460 | // Round pixels to half-integer boundaries for crisper drawing. |
461 | function halfUp(x){return Math.round(x)+0.5}; | |
462 | function halfDown(y){return Math.round(y)-0.5}; | |
463 | ||
fbe31dc8 DV |
464 | var context = this.element.getContext("2d"); |
465 | ||
34fedff8 DV |
466 | var labelStyle = { |
467 | "position": "absolute", | |
468 | "fontSize": this.options.axisLabelFontSize + "px", | |
469 | "zIndex": 10, | |
f474c2a3 | 470 | "color": this.options.axisLabelColor, |
34fedff8 DV |
471 | "width": this.options.axisLabelWidth + "px", |
472 | "overflow": "hidden" | |
473 | }; | |
474 | var makeDiv = function(txt) { | |
475 | var div = document.createElement("div"); | |
476 | for (var name in labelStyle) { | |
85b99f0b DV |
477 | if (labelStyle.hasOwnProperty(name)) { |
478 | div.style[name] = labelStyle[name]; | |
479 | } | |
fbe31dc8 | 480 | } |
34fedff8 DV |
481 | div.appendChild(document.createTextNode(txt)); |
482 | return div; | |
fbe31dc8 DV |
483 | }; |
484 | ||
485 | // axis lines | |
486 | context.save(); | |
f474c2a3 | 487 | context.strokeStyle = this.options.axisLineColor; |
fbe31dc8 DV |
488 | context.lineWidth = this.options.axisLineWidth; |
489 | ||
fbe31dc8 | 490 | if (this.options.drawYAxis) { |
8b7a0cc3 | 491 | if (this.layout.yticks && this.layout.yticks.length > 0) { |
2160ed4a DV |
492 | for (var i = 0; i < this.layout.yticks.length; i++) { |
493 | var tick = this.layout.yticks[i]; | |
fbe31dc8 DV |
494 | if (typeof(tick) == "function") return; |
495 | var x = this.area.x; | |
880a574f DV |
496 | var sgn = 1; |
497 | if (tick[0] == 1) { // right-side y-axis | |
498 | x = this.area.x + this.area.w; | |
499 | sgn = -1; | |
9012dd21 DV |
500 | } |
501 | var y = this.area.y + tick[1] * this.area.h; | |
fbe31dc8 | 502 | context.beginPath(); |
528ce7e5 DV |
503 | context.moveTo(halfUp(x), halfDown(y)); |
504 | context.lineTo(halfUp(x - sgn * this.options.axisTickSize), halfDown(y)); | |
fbe31dc8 DV |
505 | context.closePath(); |
506 | context.stroke(); | |
507 | ||
9012dd21 | 508 | var label = makeDiv(tick[2]); |
fbe31dc8 DV |
509 | var top = (y - this.options.axisLabelFontSize / 2); |
510 | if (top < 0) top = 0; | |
511 | ||
512 | if (top + this.options.axisLabelFontSize + 3 > this.height) { | |
513 | label.style.bottom = "0px"; | |
514 | } else { | |
515 | label.style.top = top + "px"; | |
516 | } | |
9012dd21 DV |
517 | if (tick[0] == 0) { |
518 | label.style.left = "0px"; | |
519 | label.style.textAlign = "right"; | |
520 | } else if (tick[0] == 1) { | |
521 | label.style.left = (this.area.x + this.area.w + | |
522 | this.options.axisTickSize) + "px"; | |
523 | label.style.textAlign = "left"; | |
524 | } | |
fbe31dc8 | 525 | label.style.width = this.options.yAxisLabelWidth + "px"; |
b0c3b730 | 526 | this.container.appendChild(label); |
fbe31dc8 | 527 | this.ylabels.push(label); |
2160ed4a | 528 | } |
fbe31dc8 DV |
529 | |
530 | // The lowest tick on the y-axis often overlaps with the leftmost | |
531 | // tick on the x-axis. Shift the bottom tick up a little bit to | |
532 | // compensate if necessary. | |
533 | var bottomTick = this.ylabels[0]; | |
534 | var fontSize = this.options.axisLabelFontSize; | |
535 | var bottom = parseInt(bottomTick.style.top) + fontSize; | |
536 | if (bottom > this.height - fontSize) { | |
537 | bottomTick.style.top = (parseInt(bottomTick.style.top) - | |
538 | fontSize / 2) + "px"; | |
539 | } | |
540 | } | |
541 | ||
528ce7e5 | 542 | // draw a vertical line on the left to separate the chart from the labels. |
fbe31dc8 | 543 | context.beginPath(); |
528ce7e5 DV |
544 | context.moveTo(halfUp(this.area.x), halfDown(this.area.y)); |
545 | context.lineTo(halfUp(this.area.x), halfDown(this.area.y + this.area.h)); | |
fbe31dc8 DV |
546 | context.closePath(); |
547 | context.stroke(); | |
c1dbeb10 | 548 | |
528ce7e5 | 549 | // if there's a secondary y-axis, draw a vertical line for that, too. |
c1dbeb10 DV |
550 | if (this.dygraph_.numAxes() == 2) { |
551 | context.beginPath(); | |
528ce7e5 DV |
552 | context.moveTo(halfDown(this.area.x + this.area.w), halfDown(this.area.y)); |
553 | context.lineTo(halfDown(this.area.x + this.area.w), halfDown(this.area.y + this.area.h)); | |
c1dbeb10 DV |
554 | context.closePath(); |
555 | context.stroke(); | |
556 | } | |
fbe31dc8 DV |
557 | } |
558 | ||
559 | if (this.options.drawXAxis) { | |
560 | if (this.layout.xticks) { | |
2160ed4a DV |
561 | for (var i = 0; i < this.layout.xticks.length; i++) { |
562 | var tick = this.layout.xticks[i]; | |
fbe31dc8 DV |
563 | if (typeof(dataset) == "function") return; |
564 | ||
565 | var x = this.area.x + tick[0] * this.area.w; | |
566 | var y = this.area.y + this.area.h; | |
567 | context.beginPath(); | |
528ce7e5 DV |
568 | context.moveTo(halfUp(x), halfDown(y)); |
569 | context.lineTo(halfUp(x), halfDown(y + this.options.axisTickSize)); | |
fbe31dc8 DV |
570 | context.closePath(); |
571 | context.stroke(); | |
572 | ||
34fedff8 | 573 | var label = makeDiv(tick[1]); |
fbe31dc8 DV |
574 | label.style.textAlign = "center"; |
575 | label.style.bottom = "0px"; | |
576 | ||
577 | var left = (x - this.options.axisLabelWidth/2); | |
578 | if (left + this.options.axisLabelWidth > this.width) { | |
579 | left = this.width - this.options.xAxisLabelWidth; | |
580 | label.style.textAlign = "right"; | |
581 | } | |
582 | if (left < 0) { | |
583 | left = 0; | |
584 | label.style.textAlign = "left"; | |
585 | } | |
586 | ||
587 | label.style.left = left + "px"; | |
588 | label.style.width = this.options.xAxisLabelWidth + "px"; | |
b0c3b730 | 589 | this.container.appendChild(label); |
fbe31dc8 | 590 | this.xlabels.push(label); |
2160ed4a | 591 | } |
fbe31dc8 DV |
592 | } |
593 | ||
594 | context.beginPath(); | |
528ce7e5 DV |
595 | context.moveTo(halfUp(this.area.x), halfDown(this.area.y + this.area.h)); |
596 | context.lineTo(halfUp(this.area.x + this.area.w), halfDown(this.area.y + this.area.h)); | |
fbe31dc8 DV |
597 | context.closePath(); |
598 | context.stroke(); | |
599 | } | |
600 | ||
601 | context.restore(); | |
6a1aa64f DV |
602 | }; |
603 | ||
fbe31dc8 | 604 | |
ce49c2fa DV |
605 | DygraphCanvasRenderer.prototype._renderAnnotations = function() { |
606 | var annotationStyle = { | |
607 | "position": "absolute", | |
608 | "fontSize": this.options.axisLabelFontSize + "px", | |
609 | "zIndex": 10, | |
3bf2fa91 | 610 | "overflow": "hidden" |
ce49c2fa DV |
611 | }; |
612 | ||
ab5e5c75 DV |
613 | var bindEvt = function(eventName, classEventName, p, self) { |
614 | return function(e) { | |
615 | var a = p.annotation; | |
616 | if (a.hasOwnProperty(eventName)) { | |
617 | a[eventName](a, p, self.dygraph_, e); | |
618 | } else if (self.dygraph_.attr_(classEventName)) { | |
619 | self.dygraph_.attr_(classEventName)(a, p, self.dygraph_,e ); | |
620 | } | |
621 | }; | |
622 | } | |
623 | ||
ce49c2fa DV |
624 | // Get a list of point with annotations. |
625 | var points = this.layout.annotated_points; | |
626 | for (var i = 0; i < points.length; i++) { | |
627 | var p = points[i]; | |
e6d53148 DV |
628 | if (p.canvasx < this.area.x || p.canvasx > this.area.x + this.area.w) { |
629 | continue; | |
630 | } | |
631 | ||
ce5e8d36 DV |
632 | var a = p.annotation; |
633 | var tick_height = 6; | |
634 | if (a.hasOwnProperty("tickHeight")) { | |
635 | tick_height = a.tickHeight; | |
9a40897e DV |
636 | } |
637 | ||
ce49c2fa DV |
638 | var div = document.createElement("div"); |
639 | for (var name in annotationStyle) { | |
640 | if (annotationStyle.hasOwnProperty(name)) { | |
641 | div.style[name] = annotationStyle[name]; | |
642 | } | |
643 | } | |
ce5e8d36 DV |
644 | if (!a.hasOwnProperty('icon')) { |
645 | div.className = "dygraphDefaultAnnotation"; | |
646 | } | |
647 | if (a.hasOwnProperty('cssClass')) { | |
648 | div.className += " " + a.cssClass; | |
649 | } | |
650 | ||
a5ad69cc DV |
651 | var width = a.hasOwnProperty('width') ? a.width : 16; |
652 | var height = a.hasOwnProperty('height') ? a.height : 16; | |
ce5e8d36 DV |
653 | if (a.hasOwnProperty('icon')) { |
654 | var img = document.createElement("img"); | |
655 | img.src = a.icon; | |
33030f33 DV |
656 | img.width = width; |
657 | img.height = height; | |
ce5e8d36 DV |
658 | div.appendChild(img); |
659 | } else if (p.annotation.hasOwnProperty('shortText')) { | |
660 | div.appendChild(document.createTextNode(p.annotation.shortText)); | |
5c528fa2 | 661 | } |
ce5e8d36 | 662 | div.style.left = (p.canvasx - width / 2) + "px"; |
d14b9eed DV |
663 | if (a.attachAtBottom) { |
664 | div.style.top = (this.area.h - height - tick_height) + "px"; | |
665 | } else { | |
666 | div.style.top = (p.canvasy - height - tick_height) + "px"; | |
667 | } | |
ce5e8d36 DV |
668 | div.style.width = width + "px"; |
669 | div.style.height = height + "px"; | |
ce49c2fa DV |
670 | div.title = p.annotation.text; |
671 | div.style.color = this.colors[p.name]; | |
672 | div.style.borderColor = this.colors[p.name]; | |
e6d53148 | 673 | a.div = div; |
ab5e5c75 | 674 | |
9a40897e DV |
675 | Dygraph.addEvent(div, 'click', |
676 | bindEvt('clickHandler', 'annotationClickHandler', p, this)); | |
677 | Dygraph.addEvent(div, 'mouseover', | |
678 | bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p, this)); | |
679 | Dygraph.addEvent(div, 'mouseout', | |
680 | bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p, this)); | |
681 | Dygraph.addEvent(div, 'dblclick', | |
682 | bindEvt('dblClickHandler', 'annotationDblClickHandler', p, this)); | |
ab5e5c75 | 683 | |
ce49c2fa DV |
684 | this.container.appendChild(div); |
685 | this.annotations.push(div); | |
9a40897e DV |
686 | |
687 | var ctx = this.element.getContext("2d"); | |
688 | ctx.strokeStyle = this.colors[p.name]; | |
689 | ctx.beginPath(); | |
d14b9eed DV |
690 | if (!a.attachAtBottom) { |
691 | ctx.moveTo(p.canvasx, p.canvasy); | |
692 | ctx.lineTo(p.canvasx, p.canvasy - 2 - tick_height); | |
693 | } else { | |
694 | ctx.moveTo(p.canvasx, this.area.h); | |
695 | ctx.lineTo(p.canvasx, this.area.h - 2 - tick_height); | |
696 | } | |
9a40897e DV |
697 | ctx.closePath(); |
698 | ctx.stroke(); | |
ce49c2fa DV |
699 | } |
700 | }; | |
701 | ||
702 | ||
6a1aa64f DV |
703 | /** |
704 | * Overrides the CanvasRenderer method to draw error bars | |
705 | */ | |
285a6bda | 706 | DygraphCanvasRenderer.prototype._renderLineChart = function() { |
44c6bc29 | 707 | // TODO(danvk): use this.attr_ for many of these. |
6a1aa64f DV |
708 | var context = this.element.getContext("2d"); |
709 | var colorCount = this.options.colorScheme.length; | |
710 | var colorScheme = this.options.colorScheme; | |
43af96e7 | 711 | var fillAlpha = this.options.fillAlpha; |
6a1aa64f | 712 | var errorBars = this.layout.options.errorBars; |
44c6bc29 | 713 | var fillGraph = this.attr_("fillGraph"); |
354e15ab | 714 | var stackedGraph = this.layout.options.stackedGraph; |
afdc483f | 715 | var stepPlot = this.layout.options.stepPlot; |
21d3323f DV |
716 | |
717 | var setNames = []; | |
ca43052c | 718 | for (var name in this.layout.datasets) { |
85b99f0b DV |
719 | if (this.layout.datasets.hasOwnProperty(name)) { |
720 | setNames.push(name); | |
721 | } | |
ca43052c | 722 | } |
21d3323f | 723 | var setCount = setNames.length; |
6a1aa64f | 724 | |
f032c51d AV |
725 | this.colors = {} |
726 | for (var i = 0; i < setCount; i++) { | |
727 | this.colors[setNames[i]] = colorScheme[i % colorCount]; | |
728 | } | |
729 | ||
ff00d3e2 DV |
730 | // Update Points |
731 | // TODO(danvk): here | |
2160ed4a DV |
732 | for (var i = 0; i < this.layout.points.length; i++) { |
733 | var point = this.layout.points[i]; | |
6a1aa64f DV |
734 | point.canvasx = this.area.w * point.x + this.area.x; |
735 | point.canvasy = this.area.h * point.y + this.area.y; | |
736 | } | |
6a1aa64f DV |
737 | |
738 | // create paths | |
9317362d | 739 | var isOK = function(x) { return x && !isNaN(x); }; |
6a1aa64f | 740 | |
80aaae18 DV |
741 | var ctx = context; |
742 | if (errorBars) { | |
6a834bbb DV |
743 | if (fillGraph) { |
744 | this.dygraph_.warn("Can't use fillGraph option with error bars"); | |
745 | } | |
746 | ||
6a1aa64f DV |
747 | for (var i = 0; i < setCount; i++) { |
748 | var setName = setNames[i]; | |
ea4942ed DV |
749 | var axis = this.layout.options.yAxes[ |
750 | this.layout.options.seriesToAxisMap[setName]]; | |
f032c51d | 751 | var color = this.colors[setName]; |
6a1aa64f DV |
752 | |
753 | // setup graphics context | |
80aaae18 | 754 | ctx.save(); |
56623f3b | 755 | var prevX = NaN; |
afdc483f | 756 | var prevY = NaN; |
6a1aa64f | 757 | var prevYs = [-1, -1]; |
ea4942ed | 758 | var yscale = axis.yscale; |
f474c2a3 DV |
759 | // should be same color as the lines but only 15% opaque. |
760 | var rgb = new RGBColor(color); | |
43af96e7 NK |
761 | var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + |
762 | fillAlpha + ')'; | |
f474c2a3 | 763 | ctx.fillStyle = err_color; |
05c9d0c4 DV |
764 | ctx.beginPath(); |
765 | for (var j = 0; j < this.layout.points.length; j++) { | |
766 | var point = this.layout.points[j]; | |
6a1aa64f | 767 | if (point.name == setName) { |
5954ef32 | 768 | if (!isOK(point.y)) { |
56623f3b | 769 | prevX = NaN; |
ae85914a | 770 | continue; |
5011e7a1 | 771 | } |
ce49c2fa | 772 | |
3637724f | 773 | // TODO(danvk): here |
afdc483f NN |
774 | if (stepPlot) { |
775 | var newYs = [ prevY - point.errorPlus * yscale, | |
47600757 | 776 | prevY + point.errorMinus * yscale ]; |
afdc483f NN |
777 | prevY = point.y; |
778 | } else { | |
779 | var newYs = [ point.y - point.errorPlus * yscale, | |
47600757 | 780 | point.y + point.errorMinus * yscale ]; |
afdc483f | 781 | } |
6a1aa64f DV |
782 | newYs[0] = this.area.h * newYs[0] + this.area.y; |
783 | newYs[1] = this.area.h * newYs[1] + this.area.y; | |
56623f3b | 784 | if (!isNaN(prevX)) { |
afdc483f | 785 | if (stepPlot) { |
47600757 | 786 | ctx.moveTo(prevX, newYs[0]); |
afdc483f | 787 | } else { |
47600757 | 788 | ctx.moveTo(prevX, prevYs[0]); |
afdc483f | 789 | } |
5954ef32 DV |
790 | ctx.lineTo(point.canvasx, newYs[0]); |
791 | ctx.lineTo(point.canvasx, newYs[1]); | |
afdc483f | 792 | if (stepPlot) { |
47600757 | 793 | ctx.lineTo(prevX, newYs[1]); |
afdc483f | 794 | } else { |
47600757 | 795 | ctx.lineTo(prevX, prevYs[1]); |
afdc483f | 796 | } |
5954ef32 DV |
797 | ctx.closePath(); |
798 | } | |
354e15ab | 799 | prevYs = newYs; |
5954ef32 DV |
800 | prevX = point.canvasx; |
801 | } | |
802 | } | |
803 | ctx.fill(); | |
804 | } | |
805 | } else if (fillGraph) { | |
354e15ab DE |
806 | var baseline = [] // for stacked graphs: baseline for filling |
807 | ||
808 | // process sets in reverse order (needed for stacked graphs) | |
809 | for (var i = setCount - 1; i >= 0; i--) { | |
5954ef32 | 810 | var setName = setNames[i]; |
f032c51d | 811 | var color = this.colors[setName]; |
ea4942ed DV |
812 | var axis = this.layout.options.yAxes[ |
813 | this.layout.options.seriesToAxisMap[setName]]; | |
814 | var axisY = 1.0 + axis.minyval * axis.yscale; | |
815 | if (axisY < 0.0) axisY = 0.0; | |
816 | else if (axisY > 1.0) axisY = 1.0; | |
817 | axisY = this.area.h * axisY + this.area.y; | |
5954ef32 DV |
818 | |
819 | // setup graphics context | |
820 | ctx.save(); | |
56623f3b | 821 | var prevX = NaN; |
5954ef32 | 822 | var prevYs = [-1, -1]; |
ea4942ed | 823 | var yscale = axis.yscale; |
5954ef32 DV |
824 | // should be same color as the lines but only 15% opaque. |
825 | var rgb = new RGBColor(color); | |
43af96e7 NK |
826 | var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + |
827 | fillAlpha + ')'; | |
5954ef32 DV |
828 | ctx.fillStyle = err_color; |
829 | ctx.beginPath(); | |
830 | for (var j = 0; j < this.layout.points.length; j++) { | |
831 | var point = this.layout.points[j]; | |
5954ef32 DV |
832 | if (point.name == setName) { |
833 | if (!isOK(point.y)) { | |
56623f3b | 834 | prevX = NaN; |
5954ef32 DV |
835 | continue; |
836 | } | |
354e15ab DE |
837 | var newYs; |
838 | if (stackedGraph) { | |
839 | lastY = baseline[point.canvasx]; | |
840 | if (lastY === undefined) lastY = axisY; | |
841 | baseline[point.canvasx] = point.canvasy; | |
842 | newYs = [ point.canvasy, lastY ]; | |
843 | } else { | |
844 | newYs = [ point.canvasy, axisY ]; | |
845 | } | |
56623f3b | 846 | if (!isNaN(prevX)) { |
05c9d0c4 | 847 | ctx.moveTo(prevX, prevYs[0]); |
afdc483f | 848 | if (stepPlot) { |
47600757 | 849 | ctx.lineTo(point.canvasx, prevYs[0]); |
afdc483f | 850 | } else { |
47600757 | 851 | ctx.lineTo(point.canvasx, newYs[0]); |
afdc483f | 852 | } |
05c9d0c4 DV |
853 | ctx.lineTo(point.canvasx, newYs[1]); |
854 | ctx.lineTo(prevX, prevYs[1]); | |
855 | ctx.closePath(); | |
6a1aa64f | 856 | } |
354e15ab | 857 | prevYs = newYs; |
6a1aa64f DV |
858 | prevX = point.canvasx; |
859 | } | |
05c9d0c4 | 860 | } |
6a1aa64f DV |
861 | ctx.fill(); |
862 | } | |
80aaae18 DV |
863 | } |
864 | ||
865 | for (var i = 0; i < setCount; i++) { | |
866 | var setName = setNames[i]; | |
f032c51d | 867 | var color = this.colors[setName]; |
227b93cc | 868 | var strokeWidth = this.dygraph_.attr_("strokeWidth", setName); |
80aaae18 DV |
869 | |
870 | // setup graphics context | |
871 | context.save(); | |
872 | var point = this.layout.points[0]; | |
227b93cc | 873 | var pointSize = this.dygraph_.attr_("pointSize", setName); |
80aaae18 | 874 | var prevX = null, prevY = null; |
227b93cc | 875 | var drawPoints = this.dygraph_.attr_("drawPoints", setName); |
80aaae18 DV |
876 | var points = this.layout.points; |
877 | for (var j = 0; j < points.length; j++) { | |
878 | var point = points[j]; | |
879 | if (point.name == setName) { | |
880 | if (!isOK(point.canvasy)) { | |
5d13ef68 | 881 | if (stepPlot && prevX != null) { |
0599d13b NN |
882 | // Draw a horizontal line to the start of the missing data |
883 | ctx.beginPath(); | |
884 | ctx.strokeStyle = color; | |
885 | ctx.lineWidth = this.options.strokeWidth; | |
886 | ctx.moveTo(prevX, prevY); | |
887 | ctx.lineTo(point.canvasx, prevY); | |
888 | ctx.stroke(); | |
889 | } | |
80aaae18 DV |
890 | // this will make us move to the next point, not draw a line to it. |
891 | prevX = prevY = null; | |
892 | } else { | |
893 | // A point is "isolated" if it is non-null but both the previous | |
894 | // and next points are null. | |
895 | var isIsolated = (!prevX && (j == points.length - 1 || | |
896 | !isOK(points[j+1].canvasy))); | |
897 | ||
898 | if (!prevX) { | |
899 | prevX = point.canvasx; | |
900 | prevY = point.canvasy; | |
901 | } else { | |
46dde5f9 DV |
902 | // TODO(danvk): figure out why this conditional is necessary. |
903 | if (strokeWidth) { | |
904 | ctx.beginPath(); | |
905 | ctx.strokeStyle = color; | |
906 | ctx.lineWidth = strokeWidth; | |
907 | ctx.moveTo(prevX, prevY); | |
908 | if (stepPlot) { | |
909 | ctx.lineTo(point.canvasx, prevY); | |
910 | } | |
911 | prevX = point.canvasx; | |
912 | prevY = point.canvasy; | |
913 | ctx.lineTo(prevX, prevY); | |
914 | ctx.stroke(); | |
afdc483f | 915 | } |
80aaae18 DV |
916 | } |
917 | ||
918 | if (drawPoints || isIsolated) { | |
919 | ctx.beginPath(); | |
920 | ctx.fillStyle = color; | |
7bf6a9fe DV |
921 | ctx.arc(point.canvasx, point.canvasy, pointSize, |
922 | 0, 2 * Math.PI, false); | |
80aaae18 DV |
923 | ctx.fill(); |
924 | } | |
925 | } | |
926 | } | |
927 | } | |
928 | } | |
6a1aa64f | 929 | |
6a1aa64f DV |
930 | context.restore(); |
931 | }; |