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