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