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