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