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