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