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