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