temporarily disable some ticker tests
[dygraphs.git] / dygraph-canvas.js
... / ...
CommitLineData
1/**
2 * @license
3 * Copyright 2006 Dan Vanderkam (danvdk@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
5 */
6
7/**
8 * @fileoverview Based on PlotKit.CanvasRenderer, but modified to meet the
9 * needs of dygraphs.
10 *
11 * In particular, support for:
12 * - grid overlays
13 * - error bars
14 * - dygraphs attribute system
15 */
16
17/**
18 * The DygraphCanvasRenderer class does the actual rendering of the chart onto
19 * a canvas. It's based on PlotKit.CanvasRenderer.
20 * @param {Object} element The canvas to attach to
21 * @param {Object} elementContext The 2d context of the canvas (injected so it
22 * can be mocked for testing.)
23 * @param {Layout} layout The DygraphLayout object for this graph.
24 * @constructor
25 */
26DygraphCanvasRenderer = function(dygraph, element, elementContext, layout) {
27 this.dygraph_ = dygraph;
28
29 this.layout = layout;
30 this.element = element;
31 this.elementContext = elementContext;
32 this.container = this.element.parentNode;
33
34 this.height = this.element.height;
35 this.width = this.element.width;
36
37 // --- check whether everything is ok before we return
38 if (!this.isIE && !(DygraphCanvasRenderer.isSupported(this.element)))
39 throw "Canvas is not supported.";
40
41 // internal state
42 this.xlabels = new Array();
43 this.ylabels = new Array();
44 this.annotations = new Array();
45 this.chartLabels = {};
46
47 this.area = layout.plotArea;
48 this.container.style.position = "relative";
49 this.container.style.width = this.width + "px";
50
51 // Set up a clipping area for the canvas (and the interaction canvas).
52 // This ensures that we don't overdraw.
53 var ctx = this.dygraph_.canvas_ctx_;
54 ctx.beginPath();
55 ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h);
56 ctx.clip();
57
58 ctx = this.dygraph_.hidden_ctx_;
59 ctx.beginPath();
60 ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h);
61 ctx.clip();
62};
63
64DygraphCanvasRenderer.prototype.attr_ = function(x) {
65 return this.dygraph_.attr_(x);
66};
67
68DygraphCanvasRenderer.prototype.clear = function() {
69 if (this.isIE) {
70 // VML takes a while to start up, so we just poll every this.IEDelay
71 try {
72 if (this.clearDelay) {
73 this.clearDelay.cancel();
74 this.clearDelay = null;
75 }
76 var context = this.elementContext;
77 }
78 catch (e) {
79 // TODO(danvk): this is broken, since MochiKit.Async is gone.
80 this.clearDelay = MochiKit.Async.wait(this.IEDelay);
81 this.clearDelay.addCallback(bind(this.clear, this));
82 return;
83 }
84 }
85
86 var context = this.elementContext;
87 context.clearRect(0, 0, this.width, this.height);
88
89 for (var i = 0; i < this.xlabels.length; i++) {
90 var el = this.xlabels[i];
91 if (el.parentNode) el.parentNode.removeChild(el);
92 }
93 for (var i = 0; i < this.ylabels.length; i++) {
94 var el = this.ylabels[i];
95 if (el.parentNode) el.parentNode.removeChild(el);
96 }
97 for (var i = 0; i < this.annotations.length; i++) {
98 var el = this.annotations[i];
99 if (el.parentNode) el.parentNode.removeChild(el);
100 }
101 for (var k in this.chartLabels) {
102 if (!this.chartLabels.hasOwnProperty(k)) continue;
103 var el = this.chartLabels[k];
104 if (el.parentNode) el.parentNode.removeChild(el);
105 }
106 this.xlabels = new Array();
107 this.ylabels = new Array();
108 this.annotations = new Array();
109 this.chartLabels = {};
110};
111
112
113DygraphCanvasRenderer.isSupported = function(canvasName) {
114 var canvas = null;
115 try {
116 if (typeof(canvasName) == 'undefined' || canvasName == null)
117 canvas = document.createElement("canvas");
118 else
119 canvas = canvasName;
120 var context = canvas.getContext("2d");
121 }
122 catch (e) {
123 var ie = navigator.appVersion.match(/MSIE (\d\.\d)/);
124 var opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1);
125 if ((!ie) || (ie[1] < 6) || (opera))
126 return false;
127 return true;
128 }
129 return true;
130};
131
132/**
133 * @param { [String] } colors Array of color strings. Should have one entry for
134 * each series to be rendered.
135 */
136DygraphCanvasRenderer.prototype.setColors = function(colors) {
137 this.colorScheme_ = colors;
138};
139
140/**
141 * Draw an X/Y grid on top of the existing plot
142 */
143DygraphCanvasRenderer.prototype.render = function() {
144 // Draw the new X/Y grid. Lines appear crisper when pixels are rounded to
145 // half-integers. This prevents them from drawing in two rows/cols.
146 var ctx = this.elementContext;
147 function halfUp(x){return Math.round(x)+0.5};
148 function halfDown(y){return Math.round(y)-0.5};
149
150 if (this.attr_('underlayCallback')) {
151 // NOTE: we pass the dygraph object to this callback twice to avoid breaking
152 // users who expect a deprecated form of this callback.
153 this.attr_('underlayCallback')(ctx, this.area, this.dygraph_, this.dygraph_);
154 }
155
156 if (this.attr_('drawYGrid')) {
157 var ticks = this.layout.yticks;
158 ctx.save();
159 ctx.strokeStyle = this.attr_('gridLineColor');
160 ctx.lineWidth = this.attr_('gridLineWidth');
161 for (var i = 0; i < ticks.length; i++) {
162 // TODO(danvk): allow secondary axes to draw a grid, too.
163 if (ticks[i][0] != 0) continue;
164 var x = halfUp(this.area.x);
165 var y = halfDown(this.area.y + ticks[i][1] * this.area.h);
166 ctx.beginPath();
167 ctx.moveTo(x, y);
168 ctx.lineTo(x + this.area.w, y);
169 ctx.closePath();
170 ctx.stroke();
171 }
172 }
173
174 if (this.attr_('drawXGrid')) {
175 var ticks = this.layout.xticks;
176 ctx.save();
177 ctx.strokeStyle = this.attr_('gridLineColor');
178 ctx.lineWidth = this.attr_('gridLineWidth');
179 for (var i=0; i<ticks.length; i++) {
180 var x = halfUp(this.area.x + ticks[i][0] * this.area.w);
181 var y = halfDown(this.area.y + this.area.h);
182 ctx.beginPath();
183 ctx.moveTo(x, y);
184 ctx.lineTo(x, this.area.y);
185 ctx.closePath();
186 ctx.stroke();
187 }
188 }
189
190 // Do the ordinary rendering, as before
191 this._renderLineChart();
192 this._renderAxis();
193 this._renderChartLabels();
194 this._renderAnnotations();
195};
196
197
198DygraphCanvasRenderer.prototype._renderAxis = function() {
199 if (!this.attr_('drawXAxis') && !this.attr_('drawYAxis')) return;
200
201 // Round pixels to half-integer boundaries for crisper drawing.
202 function halfUp(x){return Math.round(x)+0.5};
203 function halfDown(y){return Math.round(y)-0.5};
204
205 var context = this.elementContext;
206
207 var labelStyle = {
208 position: "absolute",
209 fontSize: this.attr_('axisLabelFontSize') + "px",
210 zIndex: 10,
211 color: this.attr_('axisLabelColor'),
212 width: this.attr_('axisLabelWidth') + "px",
213 // height: this.attr_('axisLabelFontSize') + 2 + "px",
214 lineHeight: "normal", // Something other than "normal" line-height screws up label positioning.
215 overflow: "hidden"
216 };
217 var makeDiv = function(txt, axis, prec_axis) {
218 var div = document.createElement("div");
219 for (var name in labelStyle) {
220 if (labelStyle.hasOwnProperty(name)) {
221 div.style[name] = labelStyle[name];
222 }
223 }
224 var inner_div = document.createElement("div");
225 inner_div.className = 'dygraph-axis-label' +
226 ' dygraph-axis-label-' + axis +
227 (prec_axis ? ' dygraph-axis-label-' + prec_axis : '');
228 inner_div.appendChild(document.createTextNode(txt));
229 div.appendChild(inner_div);
230 return div;
231 };
232
233 // axis lines
234 context.save();
235 context.strokeStyle = this.attr_('axisLineColor');
236 context.lineWidth = this.attr_('axisLineWidth');
237
238 if (this.attr_('drawYAxis')) {
239 if (this.layout.yticks && this.layout.yticks.length > 0) {
240 var num_axes = this.dygraph_.numAxes();
241 for (var i = 0; i < this.layout.yticks.length; i++) {
242 var tick = this.layout.yticks[i];
243 if (typeof(tick) == "function") return;
244 var x = this.area.x;
245 var sgn = 1;
246 var prec_axis = 'y1';
247 if (tick[0] == 1) { // right-side y-axis
248 x = this.area.x + this.area.w;
249 sgn = -1;
250 prec_axis = 'y2';
251 }
252 var y = this.area.y + tick[1] * this.area.h;
253 context.beginPath();
254 context.moveTo(halfUp(x), halfDown(y));
255 context.lineTo(halfUp(x - sgn * this.attr_('axisTickSize')), halfDown(y));
256 context.closePath();
257 context.stroke();
258
259 var label = makeDiv(tick[2], 'y', num_axes == 2 ? prec_axis : null);
260 var top = (y - this.attr_('axisLabelFontSize') / 2);
261 if (top < 0) top = 0;
262
263 if (top + this.attr_('axisLabelFontSize') + 3 > this.height) {
264 label.style.bottom = "0px";
265 } else {
266 label.style.top = top + "px";
267 }
268 if (tick[0] == 0) {
269 label.style.left = (this.area.x - this.attr_('yAxisLabelWidth') - this.attr_('axisTickSize')) + "px";
270 label.style.textAlign = "right";
271 } else if (tick[0] == 1) {
272 label.style.left = (this.area.x + this.area.w +
273 this.attr_('axisTickSize')) + "px";
274 label.style.textAlign = "left";
275 }
276 label.style.width = this.attr_('yAxisLabelWidth') + "px";
277 this.container.appendChild(label);
278 this.ylabels.push(label);
279 }
280
281 // The lowest tick on the y-axis often overlaps with the leftmost
282 // tick on the x-axis. Shift the bottom tick up a little bit to
283 // compensate if necessary.
284 var bottomTick = this.ylabels[0];
285 var fontSize = this.attr_('axisLabelFontSize');
286 var bottom = parseInt(bottomTick.style.top) + fontSize;
287 if (bottom > this.height - fontSize) {
288 bottomTick.style.top = (parseInt(bottomTick.style.top) -
289 fontSize / 2) + "px";
290 }
291 }
292
293 // draw a vertical line on the left to separate the chart from the labels.
294 context.beginPath();
295 context.moveTo(halfUp(this.area.x), halfDown(this.area.y));
296 context.lineTo(halfUp(this.area.x), halfDown(this.area.y + this.area.h));
297 context.closePath();
298 context.stroke();
299
300 // if there's a secondary y-axis, draw a vertical line for that, too.
301 if (this.dygraph_.numAxes() == 2) {
302 context.beginPath();
303 context.moveTo(halfDown(this.area.x + this.area.w), halfDown(this.area.y));
304 context.lineTo(halfDown(this.area.x + this.area.w), halfDown(this.area.y + this.area.h));
305 context.closePath();
306 context.stroke();
307 }
308 }
309
310 if (this.attr_('drawXAxis')) {
311 if (this.layout.xticks) {
312 for (var i = 0; i < this.layout.xticks.length; i++) {
313 var tick = this.layout.xticks[i];
314 if (typeof(dataset) == "function") return;
315
316 var x = this.area.x + tick[0] * this.area.w;
317 var y = this.area.y + this.area.h;
318 context.beginPath();
319 context.moveTo(halfUp(x), halfDown(y));
320 context.lineTo(halfUp(x), halfDown(y + this.attr_('axisTickSize')));
321 context.closePath();
322 context.stroke();
323
324 var label = makeDiv(tick[1], 'x');
325 label.style.textAlign = "center";
326 label.style.top = (y + this.attr_('axisTickSize')) + 'px';
327
328 var left = (x - this.attr_('axisLabelWidth')/2);
329 if (left + this.attr_('axisLabelWidth') > this.width) {
330 left = this.width - this.attr_('xAxisLabelWidth');
331 label.style.textAlign = "right";
332 }
333 if (left < 0) {
334 left = 0;
335 label.style.textAlign = "left";
336 }
337
338 label.style.left = left + "px";
339 label.style.width = this.attr_('xAxisLabelWidth') + "px";
340 this.container.appendChild(label);
341 this.xlabels.push(label);
342 }
343 }
344
345 context.beginPath();
346 context.moveTo(halfUp(this.area.x), halfDown(this.area.y + this.area.h));
347 context.lineTo(halfUp(this.area.x + this.area.w), halfDown(this.area.y + this.area.h));
348 context.closePath();
349 context.stroke();
350 }
351
352 context.restore();
353};
354
355
356DygraphCanvasRenderer.prototype._renderChartLabels = function() {
357 // Generate divs for the chart title, xlabel and ylabel.
358 // Space for these divs has already been taken away from the charting area in
359 // the DygraphCanvasRenderer constructor.
360 if (this.attr_('title')) {
361 var div = document.createElement("div");
362 div.style.position = 'absolute';
363 div.style.top = '0px';
364 div.style.left = this.area.x + 'px';
365 div.style.width = this.area.w + 'px';
366 div.style.height = this.attr_('titleHeight') + 'px';
367 div.style.textAlign = 'center';
368 div.style.fontSize = (this.attr_('titleHeight') - 8) + 'px';
369 div.style.fontWeight = 'bold';
370 var class_div = document.createElement("div");
371 class_div.className = 'dygraph-label dygraph-title';
372 class_div.innerHTML = this.attr_('title');
373 div.appendChild(class_div);
374 this.container.appendChild(div);
375 this.chartLabels.title = div;
376 }
377
378 if (this.attr_('xlabel')) {
379 var div = document.createElement("div");
380 div.style.position = 'absolute';
381 div.style.bottom = 0; // TODO(danvk): this is lazy. Calculate style.top.
382 div.style.left = this.area.x + 'px';
383 div.style.width = this.area.w + 'px';
384 div.style.height = this.attr_('xLabelHeight') + 'px';
385 div.style.textAlign = 'center';
386 div.style.fontSize = (this.attr_('xLabelHeight') - 2) + 'px';
387
388 var class_div = document.createElement("div");
389 class_div.className = 'dygraph-label dygraph-xlabel';
390 class_div.innerHTML = this.attr_('xlabel');
391 div.appendChild(class_div);
392 this.container.appendChild(div);
393 this.chartLabels.xlabel = div;
394 }
395
396 if (this.attr_('ylabel')) {
397 var box = {
398 left: 0,
399 top: this.area.y,
400 width: this.attr_('yLabelWidth'),
401 height: this.area.h
402 };
403 // TODO(danvk): is this outer div actually necessary?
404 var div = document.createElement("div");
405 div.style.position = 'absolute';
406 div.style.left = box.left;
407 div.style.top = box.top + 'px';
408 div.style.width = box.width + 'px';
409 div.style.height = box.height + 'px';
410 div.style.fontSize = (this.attr_('yLabelWidth') - 2) + 'px';
411
412 var inner_div = document.createElement("div");
413 inner_div.style.position = 'absolute';
414 inner_div.style.width = box.height + 'px';
415 inner_div.style.height = box.width + 'px';
416 inner_div.style.top = (box.height / 2 - box.width / 2) + 'px';
417 inner_div.style.left = (box.width / 2 - box.height / 2) + 'px';
418 inner_div.style.textAlign = 'center';
419
420 // CSS rotation is an HTML5 feature which is not standardized. Hence every
421 // browser has its own name for the CSS style.
422 inner_div.style.transform = 'rotate(-90deg)'; // HTML5
423 inner_div.style.WebkitTransform = 'rotate(-90deg)'; // Safari/Chrome
424 inner_div.style.MozTransform = 'rotate(-90deg)'; // Firefox
425 inner_div.style.OTransform = 'rotate(-90deg)'; // Opera
426 inner_div.style.msTransform = 'rotate(-90deg)'; // IE9
427
428 if (typeof(document.documentMode) !== 'undefined' &&
429 document.documentMode < 9) {
430 // We're dealing w/ an old version of IE, so we have to rotate the text
431 // using a BasicImage transform. This uses a different origin of rotation
432 // than HTML5 rotation (top left of div vs. its center).
433 inner_div.style.filter =
434 'progid:DXImageTransform.Microsoft.BasicImage(rotation=3)';
435 inner_div.style.left = '0px';
436 inner_div.style.top = '0px';
437 }
438
439 var class_div = document.createElement("div");
440 class_div.className = 'dygraph-label dygraph-ylabel';
441 class_div.innerHTML = this.attr_('ylabel');
442
443 inner_div.appendChild(class_div);
444 div.appendChild(inner_div);
445 this.container.appendChild(div);
446 this.chartLabels.ylabel = div;
447 }
448};
449
450
451DygraphCanvasRenderer.prototype._renderAnnotations = function() {
452 var annotationStyle = {
453 "position": "absolute",
454 "fontSize": this.attr_('axisLabelFontSize') + "px",
455 "zIndex": 10,
456 "overflow": "hidden"
457 };
458
459 var bindEvt = function(eventName, classEventName, p, self) {
460 return function(e) {
461 var a = p.annotation;
462 if (a.hasOwnProperty(eventName)) {
463 a[eventName](a, p, self.dygraph_, e);
464 } else if (self.dygraph_.attr_(classEventName)) {
465 self.dygraph_.attr_(classEventName)(a, p, self.dygraph_,e );
466 }
467 };
468 }
469
470 // Get a list of point with annotations.
471 var points = this.layout.annotated_points;
472 for (var i = 0; i < points.length; i++) {
473 var p = points[i];
474 if (p.canvasx < this.area.x || p.canvasx > this.area.x + this.area.w) {
475 continue;
476 }
477
478 var a = p.annotation;
479 var tick_height = 6;
480 if (a.hasOwnProperty("tickHeight")) {
481 tick_height = a.tickHeight;
482 }
483
484 var div = document.createElement("div");
485 for (var name in annotationStyle) {
486 if (annotationStyle.hasOwnProperty(name)) {
487 div.style[name] = annotationStyle[name];
488 }
489 }
490 if (!a.hasOwnProperty('icon')) {
491 div.className = "dygraphDefaultAnnotation";
492 }
493 if (a.hasOwnProperty('cssClass')) {
494 div.className += " " + a.cssClass;
495 }
496
497 var width = a.hasOwnProperty('width') ? a.width : 16;
498 var height = a.hasOwnProperty('height') ? a.height : 16;
499 if (a.hasOwnProperty('icon')) {
500 var img = document.createElement("img");
501 img.src = a.icon;
502 img.width = width;
503 img.height = height;
504 div.appendChild(img);
505 } else if (p.annotation.hasOwnProperty('shortText')) {
506 div.appendChild(document.createTextNode(p.annotation.shortText));
507 }
508 div.style.left = (p.canvasx - width / 2) + "px";
509 if (a.attachAtBottom) {
510 div.style.top = (this.area.h - height - tick_height) + "px";
511 } else {
512 div.style.top = (p.canvasy - height - tick_height) + "px";
513 }
514 div.style.width = width + "px";
515 div.style.height = height + "px";
516 div.title = p.annotation.text;
517 div.style.color = this.colors[p.name];
518 div.style.borderColor = this.colors[p.name];
519 a.div = div;
520
521 Dygraph.addEvent(div, 'click',
522 bindEvt('clickHandler', 'annotationClickHandler', p, this));
523 Dygraph.addEvent(div, 'mouseover',
524 bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p, this));
525 Dygraph.addEvent(div, 'mouseout',
526 bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p, this));
527 Dygraph.addEvent(div, 'dblclick',
528 bindEvt('dblClickHandler', 'annotationDblClickHandler', p, this));
529
530 this.container.appendChild(div);
531 this.annotations.push(div);
532
533 var ctx = this.elementContext;
534 ctx.strokeStyle = this.colors[p.name];
535 ctx.beginPath();
536 if (!a.attachAtBottom) {
537 ctx.moveTo(p.canvasx, p.canvasy);
538 ctx.lineTo(p.canvasx, p.canvasy - 2 - tick_height);
539 } else {
540 ctx.moveTo(p.canvasx, this.area.h);
541 ctx.lineTo(p.canvasx, this.area.h - 2 - tick_height);
542 }
543 ctx.closePath();
544 ctx.stroke();
545 }
546};
547
548
549/**
550 * Overrides the CanvasRenderer method to draw error bars
551 */
552DygraphCanvasRenderer.prototype._renderLineChart = function() {
553 var isNullOrNaN = function(x) {
554 return (x === null || isNaN(x));
555 };
556
557 // TODO(danvk): use this.attr_ for many of these.
558 var context = this.elementContext;
559 var fillAlpha = this.attr_('fillAlpha');
560 var errorBars = this.attr_("errorBars") || this.attr_("customBars");
561 var fillGraph = this.attr_("fillGraph");
562 var stackedGraph = this.attr_("stackedGraph");
563 var stepPlot = this.attr_("stepPlot");
564 var points = this.layout.points;
565 var pointsLength = points.length;
566
567 var setNames = [];
568 for (var name in this.layout.datasets) {
569 if (this.layout.datasets.hasOwnProperty(name)) {
570 setNames.push(name);
571 }
572 }
573 var setCount = setNames.length;
574
575 // TODO(danvk): Move this mapping into Dygraph and get it out of here.
576 this.colors = {}
577 for (var i = 0; i < setCount; i++) {
578 this.colors[setNames[i]] = this.colorScheme_[i % this.colorScheme_.length];
579 }
580
581 // Update Points
582 // TODO(danvk): here
583 for (var i = pointsLength; i--;) {
584 var point = points[i];
585 point.canvasx = this.area.w * point.x + this.area.x;
586 point.canvasy = this.area.h * point.y + this.area.y;
587 }
588
589 // create paths
590 var ctx = context;
591 if (errorBars) {
592 if (fillGraph) {
593 this.dygraph_.warn("Can't use fillGraph option with error bars");
594 }
595
596 for (var i = 0; i < setCount; i++) {
597 var setName = setNames[i];
598 var axis = this.dygraph_.axisPropertiesForSeries(setName);
599 var color = this.colors[setName];
600
601 // setup graphics context
602 ctx.save();
603 var prevX = NaN;
604 var prevY = NaN;
605 var prevYs = [-1, -1];
606 var yscale = axis.yscale;
607 // should be same color as the lines but only 15% opaque.
608 var rgb = new RGBColor(color);
609 var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' +
610 fillAlpha + ')';
611 ctx.fillStyle = err_color;
612 ctx.beginPath();
613 for (var j = 0; j < pointsLength; j++) {
614 var point = points[j];
615 if (point.name == setName) {
616 if (!Dygraph.isOK(point.y)) {
617 prevX = NaN;
618 continue;
619 }
620
621 // TODO(danvk): here
622 if (stepPlot) {
623 var newYs = [ point.y_bottom, point.y_top ];
624 prevY = point.y;
625 } else {
626 var newYs = [ point.y_bottom, point.y_top ];
627 }
628 newYs[0] = this.area.h * newYs[0] + this.area.y;
629 newYs[1] = this.area.h * newYs[1] + this.area.y;
630 if (!isNaN(prevX)) {
631 if (stepPlot) {
632 ctx.moveTo(prevX, newYs[0]);
633 } else {
634 ctx.moveTo(prevX, prevYs[0]);
635 }
636 ctx.lineTo(point.canvasx, newYs[0]);
637 ctx.lineTo(point.canvasx, newYs[1]);
638 if (stepPlot) {
639 ctx.lineTo(prevX, newYs[1]);
640 } else {
641 ctx.lineTo(prevX, prevYs[1]);
642 }
643 ctx.closePath();
644 }
645 prevYs = newYs;
646 prevX = point.canvasx;
647 }
648 }
649 ctx.fill();
650 }
651 } else if (fillGraph) {
652 var baseline = [] // for stacked graphs: baseline for filling
653
654 // process sets in reverse order (needed for stacked graphs)
655 for (var i = setCount - 1; i >= 0; i--) {
656 var setName = setNames[i];
657 var color = this.colors[setName];
658 var axis = this.dygraph_.axisPropertiesForSeries(setName);
659 var axisY = 1.0 + axis.minyval * axis.yscale;
660 if (axisY < 0.0) axisY = 0.0;
661 else if (axisY > 1.0) axisY = 1.0;
662 axisY = this.area.h * axisY + this.area.y;
663
664 // setup graphics context
665 ctx.save();
666 var prevX = NaN;
667 var prevYs = [-1, -1];
668 var yscale = axis.yscale;
669 // should be same color as the lines but only 15% opaque.
670 var rgb = new RGBColor(color);
671 var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' +
672 fillAlpha + ')';
673 ctx.fillStyle = err_color;
674 ctx.beginPath();
675 for (var j = 0; j < pointsLength; j++) {
676 var point = points[j];
677 if (point.name == setName) {
678 if (!Dygraph.isOK(point.y)) {
679 prevX = NaN;
680 continue;
681 }
682 var newYs;
683 if (stackedGraph) {
684 lastY = baseline[point.canvasx];
685 if (lastY === undefined) lastY = axisY;
686 baseline[point.canvasx] = point.canvasy;
687 newYs = [ point.canvasy, lastY ];
688 } else {
689 newYs = [ point.canvasy, axisY ];
690 }
691 if (!isNaN(prevX)) {
692 ctx.moveTo(prevX, prevYs[0]);
693 if (stepPlot) {
694 ctx.lineTo(point.canvasx, prevYs[0]);
695 } else {
696 ctx.lineTo(point.canvasx, newYs[0]);
697 }
698 ctx.lineTo(point.canvasx, newYs[1]);
699 ctx.lineTo(prevX, prevYs[1]);
700 ctx.closePath();
701 }
702 prevYs = newYs;
703 prevX = point.canvasx;
704 }
705 }
706 ctx.fill();
707 }
708 }
709
710 // Drawing the lines.
711 var firstIndexInSet = 0;
712 var afterLastIndexInSet = 0;
713 var setLength = 0;
714 for (var i = 0; i < setCount; i += 1) {
715 setLength = this.layout.setPointsLengths[i];
716 afterLastIndexInSet += setLength;
717 var setName = setNames[i];
718 var color = this.colors[setName];
719 var strokeWidth = this.dygraph_.attr_("strokeWidth", setName);
720
721 // setup graphics context
722 context.save();
723 var pointSize = this.dygraph_.attr_("pointSize", setName);
724 var prevX = null, prevY = null;
725 var drawPoints = this.dygraph_.attr_("drawPoints", setName);
726 for (var j = firstIndexInSet; j < afterLastIndexInSet; j++) {
727 var point = points[j];
728 if (isNullOrNaN(point.canvasy)) {
729 if (stepPlot && prevX != null) {
730 // Draw a horizontal line to the start of the missing data
731 ctx.beginPath();
732 ctx.strokeStyle = color;
733 ctx.lineWidth = this.attr_('strokeWidth');
734 ctx.moveTo(prevX, prevY);
735 ctx.lineTo(point.canvasx, prevY);
736 ctx.stroke();
737 }
738 // this will make us move to the next point, not draw a line to it.
739 prevX = prevY = null;
740 } else {
741 // A point is "isolated" if it is non-null but both the previous
742 // and next points are null.
743 var isIsolated = (!prevX && (j == points.length - 1 ||
744 isNullOrNaN(points[j+1].canvasy)));
745 if (prevX === null) {
746 prevX = point.canvasx;
747 prevY = point.canvasy;
748 } else {
749 // Skip over points that will be drawn in the same pixel.
750 if (Math.round(prevX) == Math.round(point.canvasx) &&
751 Math.round(prevY) == Math.round(point.canvasy)) {
752 continue;
753 }
754 // TODO(antrob): skip over points that lie on a line that is already
755 // going to be drawn. There is no need to have more than 2
756 // consecutive points that are collinear.
757 if (strokeWidth) {
758 ctx.beginPath();
759 ctx.strokeStyle = color;
760 ctx.lineWidth = strokeWidth;
761 ctx.moveTo(prevX, prevY);
762 if (stepPlot) {
763 ctx.lineTo(point.canvasx, prevY);
764 }
765 prevX = point.canvasx;
766 prevY = point.canvasy;
767 ctx.lineTo(prevX, prevY);
768 ctx.stroke();
769 }
770 }
771
772 if (drawPoints || isIsolated) {
773 ctx.beginPath();
774 ctx.fillStyle = color;
775 ctx.arc(point.canvasx, point.canvasy, pointSize,
776 0, 2 * Math.PI, false);
777 ctx.fill();
778 }
779 }
780 }
781 firstIndexInSet = afterLastIndexInSet;
782 }
783
784 context.restore();
785};