drawing works, just need to add ticks and fix filled/stacked
[dygraphs.git] / dygraph-canvas.js
1 // Copyright 2006 Dan Vanderkam (danvdk@gmail.com)
2 // All Rights Reserved.
3
4 /**
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
10 */
11
12 /**
13 * Creates a new DygraphLayout object.
14 * @param {Object} options Options for PlotKit.Layout
15 * @return {Object} The DygraphLayout object
16 */
17 DygraphLayout = function(dygraph, options) {
18 this.dygraph_ = dygraph;
19 this.options = {}; // TODO(danvk): remove, use attr_ instead.
20 Dygraph.update(this.options, options ? options : {});
21 this.datasets = new Array();
22 this.annotations = new Array()
23 };
24
25 DygraphLayout.prototype.attr_ = function(name) {
26 return this.dygraph_.attr_(name);
27 };
28
29 DygraphLayout.prototype.addDataset = function(setname, set_xy) {
30 this.datasets[setname] = set_xy;
31 };
32
33 DygraphLayout.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 = {};
39 if (!ann[i].xval && !ann[i].x) {
40 this.dygraph_.error("Annotations must have an 'x' property");
41 return;
42 }
43 if (ann[i].icon &&
44 !(ann[i].hasOwnProperty('width') &&
45 ann[i].hasOwnProperty('height'))) {
46 this.dygraph_.error("Must set width and height when setting " +
47 "annotation.icon property");
48 return;
49 }
50 Dygraph.update(a, ann[i]);
51 if (!a.xval) a.xval = parse(a.x);
52 this.annotations.push(a);
53 }
54 };
55
56 DygraphLayout.prototype.evaluate = function() {
57 this._evaluateLimits();
58 this._evaluateLineCharts();
59 this._evaluateLineTicks();
60 this._evaluateAnnotations();
61 };
62
63 DygraphLayout.prototype._evaluateLimits = function() {
64 this.minxval = this.maxxval = null;
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;
74
75 var x2 = series[series.length - 1][0];
76 if (!this.maxxval || x2 > this.maxxval) this.maxxval = x2;
77 }
78 }
79 this.xrange = this.maxxval - this.minxval;
80 this.xscale = (this.xrange != 0 ? 1/this.xrange : 1.0);
81
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 }
89 };
90
91 DygraphLayout.prototype._evaluateLineCharts = function() {
92 // add all the rects
93 this.points = new Array();
94 for (var setName in this.datasets) {
95 if (!this.datasets.hasOwnProperty(setName)) continue;
96
97 var dataset = this.datasets[setName];
98 var axis = this.options.yAxes[this.options.seriesToAxisMap[setName]];
99
100 for (var j = 0; j < dataset.length; j++) {
101 var item = dataset[j];
102 var point = {
103 // TODO(danvk): here
104 x: ((parseFloat(item[0]) - this.minxval) * this.xscale),
105 y: 1.0 - ((parseFloat(item[1]) - axis.minyval) * axis.yscale),
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 }
118 this.points.push(point);
119 }
120 }
121 };
122
123 DygraphLayout.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
145
146 /**
147 * Behaves the same way as PlotKit.Layout, but also copies the errors
148 * @private
149 */
150 DygraphLayout.prototype.evaluateWithError = function() {
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) {
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 }
171 }
172 };
173
174 DygraphLayout.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
194 /**
195 * Convenience function to remove all the data sets from a graph
196 */
197 DygraphLayout.prototype.removeAllDatasets = function() {
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 */
206 DygraphLayout.prototype.updateOptions = function(new_options) {
207 Dygraph.update(this.options, new_options ? new_options : {});
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
217 * @param {Layout} layout The DygraphLayout object for this graph.
218 * @param {Object} options Options to pass on to CanvasRenderer
219 */
220 DygraphCanvasRenderer = function(dygraph, element, layout, options) {
221 // TODO(danvk): remove options, just use dygraph.attr_.
222 this.dygraph_ = dygraph;
223
224 // default options
225 this.options = {
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,
238 "gridLineColor": "rgb(128,128,128)",
239 "fillAlpha": 0.15,
240 "underlayCallback": null
241 };
242 Dygraph.update(this.options, options);
243
244 this.layout = layout;
245 this.element = element;
246 this.container = this.element.parentNode;
247
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();
258 this.annotations = new Array();
259
260 // TODO(danvk): consider all axes in this computation.
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
269 this.container.style.position = "relative";
270 this.container.style.width = this.width + "px";
271 };
272
273 DygraphCanvasRenderer.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) {
284 // TODO(danvk): this is broken, since MochiKit.Async is gone.
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
294 for (var i = 0; i < this.xlabels.length; i++) {
295 var el = this.xlabels[i];
296 el.parentNode.removeChild(el);
297 }
298 for (var i = 0; i < this.ylabels.length; i++) {
299 var el = this.ylabels[i];
300 el.parentNode.removeChild(el);
301 }
302 for (var i = 0; i < this.annotations.length; i++) {
303 var el = this.annotations[i];
304 el.parentNode.removeChild(el);
305 }
306 this.xlabels = new Array();
307 this.ylabels = new Array();
308 this.annotations = new Array();
309 };
310
311
312 DygraphCanvasRenderer.isSupported = function(canvasName) {
313 var canvas = null;
314 try {
315 if (typeof(canvasName) == 'undefined' || canvasName == null)
316 canvas = document.createElement("canvas");
317 else
318 canvas = canvasName;
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;
329 };
330
331 /**
332 * Draw an X/Y grid on top of the existing plot
333 */
334 DygraphCanvasRenderer.prototype.render = function() {
335 // Draw the new X/Y grid
336 var ctx = this.element.getContext("2d");
337
338 if (this.options.underlayCallback) {
339 this.options.underlayCallback(ctx, this.area, this.layout, this.dygraph_);
340 }
341
342 if (this.options.drawYGrid) {
343 var ticks = this.layout.yticks;
344 ctx.save();
345 ctx.strokeStyle = this.options.gridLineColor;
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();
361 ctx.strokeStyle = this.options.gridLineColor;
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 }
373
374 // Do the ordinary rendering, as before
375 this._renderLineChart();
376 this._renderAxis();
377 this._renderAnnotations();
378 };
379
380
381 DygraphCanvasRenderer.prototype._renderAxis = function() {
382 if (!this.options.drawXAxis && !this.options.drawYAxis)
383 return;
384
385 var context = this.element.getContext("2d");
386
387 var labelStyle = {
388 "position": "absolute",
389 "fontSize": this.options.axisLabelFontSize + "px",
390 "zIndex": 10,
391 "color": this.options.axisLabelColor,
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) {
398 if (labelStyle.hasOwnProperty(name)) {
399 div.style[name] = labelStyle[name];
400 }
401 }
402 div.appendChild(document.createTextNode(txt));
403 return div;
404 };
405
406 // axis lines
407 context.save();
408 context.strokeStyle = this.options.axisLineColor;
409 context.lineWidth = this.options.axisLineWidth;
410
411 if (this.options.drawYAxis) {
412 if (this.layout.yticks && this.layout.yticks.length > 0) {
413 for (var i = 0; i < this.layout.yticks.length; i++) {
414 var tick = this.layout.yticks[i];
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
424 var label = makeDiv(tick[1]);
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";
436 this.container.appendChild(label);
437 this.ylabels.push(label);
438 }
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) {
461 for (var i = 0; i < this.layout.xticks.length; i++) {
462 var tick = this.layout.xticks[i];
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
473 var label = makeDiv(tick[1]);
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";
489 this.container.appendChild(label);
490 this.xlabels.push(label);
491 }
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();
502 };
503
504
505 DygraphCanvasRenderer.prototype._renderAnnotations = function() {
506 var annotationStyle = {
507 "position": "absolute",
508 "fontSize": this.options.axisLabelFontSize + "px",
509 "zIndex": 10,
510 "overflow": "hidden"
511 };
512
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
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];
528 if (p.canvasx < this.area.x || p.canvasx > this.area.x + this.area.w) {
529 continue;
530 }
531
532 var a = p.annotation;
533 var tick_height = 6;
534 if (a.hasOwnProperty("tickHeight")) {
535 tick_height = a.tickHeight;
536 }
537
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 }
544 if (!a.hasOwnProperty('icon')) {
545 div.className = "dygraphDefaultAnnotation";
546 }
547 if (a.hasOwnProperty('cssClass')) {
548 div.className += " " + a.cssClass;
549 }
550
551 var width = a.hasOwnProperty('width') ? a.width : 16;
552 var height = a.hasOwnProperty('height') ? a.height : 16;
553 if (a.hasOwnProperty('icon')) {
554 var img = document.createElement("img");
555 img.src = a.icon;
556 img.width = width;
557 img.height = height;
558 div.appendChild(img);
559 } else if (p.annotation.hasOwnProperty('shortText')) {
560 div.appendChild(document.createTextNode(p.annotation.shortText));
561 }
562 div.style.left = (p.canvasx - width / 2) + "px";
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 }
568 div.style.width = width + "px";
569 div.style.height = height + "px";
570 div.title = p.annotation.text;
571 div.style.color = this.colors[p.name];
572 div.style.borderColor = this.colors[p.name];
573 a.div = div;
574
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));
583
584 this.container.appendChild(div);
585 this.annotations.push(div);
586
587 var ctx = this.element.getContext("2d");
588 ctx.strokeStyle = this.colors[p.name];
589 ctx.beginPath();
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 }
597 ctx.closePath();
598 ctx.stroke();
599 }
600 };
601
602
603 /**
604 * Overrides the CanvasRenderer method to draw error bars
605 */
606 DygraphCanvasRenderer.prototype._renderLineChart = function() {
607 var context = this.element.getContext("2d");
608 var colorCount = this.options.colorScheme.length;
609 var colorScheme = this.options.colorScheme;
610 var fillAlpha = this.options.fillAlpha;
611 var errorBars = this.layout.options.errorBars;
612 var fillGraph = this.layout.options.fillGraph;
613 var stackedGraph = this.layout.options.stackedGraph;
614 var stepPlot = this.layout.options.stepPlot;
615
616 var setNames = [];
617 for (var name in this.layout.datasets) {
618 if (this.layout.datasets.hasOwnProperty(name)) {
619 setNames.push(name);
620 }
621 }
622 var setCount = setNames.length;
623
624 this.colors = {}
625 for (var i = 0; i < setCount; i++) {
626 this.colors[setNames[i]] = colorScheme[i % colorCount];
627 }
628
629 // Update Points
630 // TODO(danvk): here
631 for (var i = 0; i < this.layout.points.length; i++) {
632 var point = this.layout.points[i];
633 point.canvasx = this.area.w * point.x + this.area.x;
634 point.canvasy = this.area.h * point.y + this.area.y;
635 }
636
637 // create paths
638 var isOK = function(x) { return x && !isNaN(x); };
639
640 var ctx = context;
641 if (errorBars) {
642 if (fillGraph) {
643 this.dygraph_.warn("Can't use fillGraph option with error bars");
644 }
645
646 for (var i = 0; i < setCount; i++) {
647 var setName = setNames[i];
648 var axis = this.layout.options.yAxes[
649 this.layout.options.seriesToAxisMap[setName]];
650 var color = this.colors[setName];
651
652 // setup graphics context
653 ctx.save();
654 var prevX = NaN;
655 var prevY = NaN;
656 var prevYs = [-1, -1];
657 var yscale = axis.yscale;
658 // should be same color as the lines but only 15% opaque.
659 var rgb = new RGBColor(color);
660 var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' +
661 fillAlpha + ')';
662 ctx.fillStyle = err_color;
663 ctx.beginPath();
664 for (var j = 0; j < this.layout.points.length; j++) {
665 var point = this.layout.points[j];
666 if (point.name == setName) {
667 if (!isOK(point.y)) {
668 prevX = NaN;
669 continue;
670 }
671
672 // TODO(danvk): here
673 if (stepPlot) {
674 var newYs = [ prevY - point.errorPlus * yscale,
675 prevY + point.errorMinus * yscale ];
676 prevY = point.y;
677 } else {
678 var newYs = [ point.y - point.errorPlus * yscale,
679 point.y + point.errorMinus * yscale ];
680 }
681 newYs[0] = this.area.h * newYs[0] + this.area.y;
682 newYs[1] = this.area.h * newYs[1] + this.area.y;
683 if (!isNaN(prevX)) {
684 if (stepPlot) {
685 ctx.moveTo(prevX, newYs[0]);
686 } else {
687 ctx.moveTo(prevX, prevYs[0]);
688 }
689 ctx.lineTo(point.canvasx, newYs[0]);
690 ctx.lineTo(point.canvasx, newYs[1]);
691 if (stepPlot) {
692 ctx.lineTo(prevX, newYs[1]);
693 } else {
694 ctx.lineTo(prevX, prevYs[1]);
695 }
696 ctx.closePath();
697 }
698 prevYs = newYs;
699 prevX = point.canvasx;
700 }
701 }
702 ctx.fill();
703 }
704 } else if (fillGraph) {
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--) {
709 var setName = setNames[i];
710 var color = this.colors[setName];
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;
717
718 // setup graphics context
719 ctx.save();
720 var prevX = NaN;
721 var prevYs = [-1, -1];
722 var yscale = axis.yscale;
723 // should be same color as the lines but only 15% opaque.
724 var rgb = new RGBColor(color);
725 var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' +
726 fillAlpha + ')';
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];
731 if (point.name == setName) {
732 if (!isOK(point.y)) {
733 prevX = NaN;
734 continue;
735 }
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 }
745 if (!isNaN(prevX)) {
746 ctx.moveTo(prevX, prevYs[0]);
747 if (stepPlot) {
748 ctx.lineTo(point.canvasx, prevYs[0]);
749 } else {
750 ctx.lineTo(point.canvasx, newYs[0]);
751 }
752 ctx.lineTo(point.canvasx, newYs[1]);
753 ctx.lineTo(prevX, prevYs[1]);
754 ctx.closePath();
755 }
756 prevYs = newYs;
757 prevX = point.canvasx;
758 }
759 }
760 ctx.fill();
761 }
762 }
763
764 for (var i = 0; i < setCount; i++) {
765 var setName = setNames[i];
766 var color = this.colors[setName];
767 var strokeWidth = this.dygraph_.attr_("strokeWidth", setName);
768
769 // setup graphics context
770 context.save();
771 var point = this.layout.points[0];
772 var pointSize = this.dygraph_.attr_("pointSize", setName);
773 var prevX = null, prevY = null;
774 var drawPoints = this.dygraph_.attr_("drawPoints", setName);
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 {
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();
805 }
806 }
807
808 if (drawPoints || isIsolated) {
809 ctx.beginPath();
810 ctx.fillStyle = color;
811 ctx.arc(point.canvasx, point.canvasy, pointSize,
812 0, 2 * Math.PI, false);
813 ctx.fill();
814 }
815 }
816 }
817 }
818 }
819
820 context.restore();
821 };