3 * Copyright 2006 Dan Vanderkam (danvdk@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
8 * @fileoverview Based on PlotKit.CanvasRenderer, but modified to meet the
11 * In particular, support for:
14 * - dygraphs attribute system
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.
26 DygraphCanvasRenderer
= function(dygraph
, element
, elementContext
, layout
) {
27 this.dygraph_
= dygraph
;
30 this.element
= element
;
31 this.elementContext
= elementContext
;
32 this.container
= this.element
.parentNode
;
34 this.height
= this.element
.height
;
35 this.width
= this.element
.width
;
37 // --- check whether everything is ok before we return
38 if (!this.isIE
&& !(DygraphCanvasRenderer
.isSupported(this.element
)))
39 throw "Canvas is not supported.";
42 this.xlabels
= new Array();
43 this.ylabels
= new Array();
44 this.annotations
= new Array();
45 this.chartLabels
= {};
47 this.area
= layout
.getPlotArea();
48 this.container
.style
.position
= "relative";
49 this.container
.style
.width
= this.width
+ "px";
51 // Set up a clipping area for the canvas (and the interaction canvas).
52 // This ensures that we don't overdraw.
53 if (this.dygraph_
.isUsingExcanvas_
) {
54 this._createIEClipArea();
56 var ctx
= this.dygraph_
.canvas_ctx_
;
58 ctx
.rect(this.area
.x
, this.area
.y
, this.area
.w
, this.area
.h
);
61 ctx
= this.dygraph_
.hidden_ctx_
;
63 ctx
.rect(this.area
.x
, this.area
.y
, this.area
.w
, this.area
.h
);
68 DygraphCanvasRenderer
.prototype.attr_
= function(x
) {
69 return this.dygraph_
.attr_(x
);
72 DygraphCanvasRenderer
.prototype.clear
= function() {
74 // VML takes a while to start up, so we just poll every this.IEDelay
76 if (this.clearDelay
) {
77 this.clearDelay
.cancel();
78 this.clearDelay
= null;
80 var context
= this.elementContext
;
83 // TODO(danvk): this is broken, since MochiKit.Async is gone.
84 this.clearDelay
= MochiKit
.Async
.wait(this.IEDelay
);
85 this.clearDelay
.addCallback(bind(this.clear
, this));
90 var context
= this.elementContext
;
91 context
.clearRect(0, 0, this.width
, this.height
);
93 for (var i
= 0; i
< this.xlabels
.length
; i
++) {
94 var el
= this.xlabels
[i
];
95 if (el
.parentNode
) el
.parentNode
.removeChild(el
);
97 for (var i
= 0; i
< this.ylabels
.length
; i
++) {
98 var el
= this.ylabels
[i
];
99 if (el
.parentNode
) el
.parentNode
.removeChild(el
);
101 for (var i
= 0; i
< this.annotations
.length
; i
++) {
102 var el
= this.annotations
[i
];
103 if (el
.parentNode
) el
.parentNode
.removeChild(el
);
105 for (var k
in this.chartLabels
) {
106 if (!this.chartLabels
.hasOwnProperty(k
)) continue;
107 var el
= this.chartLabels
[k
];
108 if (el
.parentNode
) el
.parentNode
.removeChild(el
);
110 this.xlabels
= new Array();
111 this.ylabels
= new Array();
112 this.annotations
= new Array();
113 this.chartLabels
= {};
117 DygraphCanvasRenderer
.isSupported
= function(canvasName
) {
120 if (typeof(canvasName
) == 'undefined' || canvasName
== null)
121 canvas
= document
.createElement("canvas");
124 var context
= canvas
.getContext("2d");
127 var ie
= navigator
.appVersion
.match(/MSIE (\d\.\d)/);
128 var opera
= (navigator
.userAgent
.toLowerCase().indexOf("opera") != -1);
129 if ((!ie
) || (ie
[1] < 6) || (opera
))
137 * @param { [String] } colors Array of color strings. Should have one entry for
138 * each series to be rendered.
140 DygraphCanvasRenderer
.prototype.setColors
= function(colors
) {
141 this.colorScheme_
= colors
;
145 * Draw an X/Y grid on top of the existing plot
147 DygraphCanvasRenderer
.prototype.render
= function() {
148 // Draw the new X/Y grid
. Lines appear crisper when pixels are rounded to
149 // half-integers. This prevents them from drawing in two rows/cols.
150 var ctx
= this.elementContext
;
151 function halfUp(x
){return Math
.round(x
)+0.5};
152 function halfDown(y
){return Math
.round(y
)-0.5};
154 if (this.attr_('underlayCallback')) {
155 // NOTE: we pass the dygraph object to this callback twice to avoid breaking
156 // users who expect a deprecated form of this callback.
157 this.attr_('underlayCallback')(ctx
, this.area
, this.dygraph_
, this.dygraph_
);
160 if (this.attr_('drawYGrid')) {
161 var ticks
= this.layout
.yticks
;
162 // TODO(konigsberg): I don't think these calls to save() have a corresponding restore().
164 ctx
.strokeStyle
= this.attr_('gridLineColor');
165 ctx
.lineWidth
= this.attr_('gridLineWidth');
166 for (var i
= 0; i
< ticks
.length
; i
++) {
167 // TODO(danvk): allow secondary axes to draw a grid, too.
168 if (ticks
[i
][0] != 0) continue;
169 var x
= halfUp(this.area
.x
);
170 var y
= halfDown(this.area
.y
+ ticks
[i
][1] * this.area
.h
);
173 ctx
.lineTo(x
+ this.area
.w
, y
);
179 if (this.attr_('drawXGrid')) {
180 var ticks
= this.layout
.xticks
;
182 ctx
.strokeStyle
= this.attr_('gridLineColor');
183 ctx
.lineWidth
= this.attr_('gridLineWidth');
184 for (var i
=0; i
<ticks
.length
; i
++) {
185 var x
= halfUp(this.area
.x
+ ticks
[i
][0] * this.area
.w
);
186 var y
= halfDown(this.area
.y
+ this.area
.h
);
189 ctx
.lineTo(x
, this.area
.y
);
195 // Do the ordinary rendering, as before
196 this._renderLineChart();
198 this._renderChartLabels();
199 this._renderAnnotations();
202 DygraphCanvasRenderer
.prototype._createIEClipArea
= function() {
203 var className
= 'dygraph-clip-div';
204 var graphDiv
= this.dygraph_
.graphDiv
;
206 // Remove old clip divs.
207 for (var i
= graphDiv
.childNodes
.length
-1; i
>= 0; i
--) {
208 if (graphDiv
.childNodes
[i
].className
== className
) {
209 graphDiv
.removeChild(graphDiv
.childNodes
[i
]);
213 // Determine background color to give clip divs.
214 var backgroundColor
= document
.bgColor
;
215 var element
= this.dygraph_
.graphDiv
;
216 while (element
!= document
) {
217 var bgcolor
= element
.currentStyle
.backgroundColor
;
218 if (bgcolor
&& bgcolor
!= 'transparent') {
219 backgroundColor
= bgcolor
;
222 element
= element
.parentNode
;
225 function createClipDiv(area
) {
226 if (area
.w
== 0 || area
.h
== 0) {
229 var elem
= document
.createElement('div');
230 elem
.className
= className
;
231 elem
.style
.backgroundColor
= backgroundColor
;
232 elem
.style
.position
= 'absolute';
233 elem
.style
.left
= area
.x
+ 'px';
234 elem
.style
.top
= area
.y
+ 'px';
235 elem
.style
.width
= area
.w
+ 'px';
236 elem
.style
.height
= area
.h
+ 'px';
237 graphDiv
.appendChild(elem
);
240 var plotArea
= this.area
;
242 createClipDiv({x
:0, y
:0, w
:plotArea
.x
, h
:this.height
});
244 createClipDiv({x
:plotArea
.x
, y
:0, w
:this.width
-plotArea
.x
, h
:plotArea
.y
});
246 createClipDiv({x
:plotArea
.x
+plotArea
.w
, y
:0, w
:this.width
-plotArea
.x
-plotArea
.w
, h
:this.height
});
248 createClipDiv({x
:plotArea
.x
, y
:plotArea
.y
+plotArea
.h
, w
:this.width
-plotArea
.x
, h
:this.height
-plotArea
.h
-plotArea
.y
});
251 DygraphCanvasRenderer
.prototype._renderAxis
= function() {
252 if (!this.attr_('drawXAxis') && !this.attr_('drawYAxis')) return;
254 // Round pixels to half-integer boundaries for crisper drawing.
255 function halfUp(x
){return Math
.round(x
)+0.5};
256 function halfDown(y
){return Math
.round(y
)-0.5};
258 var context
= this.elementContext
;
261 position
: "absolute",
262 fontSize
: this.attr_('axisLabelFontSize') + "px",
264 color
: this.attr_('axisLabelColor'),
265 width
: this.attr_('axisLabelWidth') + "px",
266 // height: this.attr_('axisLabelFontSize') + 2 + "px",
267 lineHeight
: "normal", // Something other than "normal" line-height screws up label positioning.
270 var makeDiv
= function(txt
, axis
, prec_axis
) {
271 var div
= document
.createElement("div");
272 for (var name
in labelStyle
) {
273 if (labelStyle
.hasOwnProperty(name
)) {
274 div
.style
[name
] = labelStyle
[name
];
277 var inner_div
= document
.createElement("div");
278 inner_div
.className
= 'dygraph-axis-label' +
279 ' dygraph-axis-label-' + axis
+
280 (prec_axis
? ' dygraph-axis-label-' + prec_axis
: '');
281 inner_div
.appendChild(document
.createTextNode(txt
));
282 div
.appendChild(inner_div
);
288 context
.strokeStyle
= this.attr_('axisLineColor');
289 context
.lineWidth
= this.attr_('axisLineWidth');
291 if (this.attr_('drawYAxis')) {
292 if (this.layout
.yticks
&& this.layout
.yticks
.length
> 0) {
293 var num_axes
= this.dygraph_
.numAxes();
294 for (var i
= 0; i
< this.layout
.yticks
.length
; i
++) {
295 var tick
= this.layout
.yticks
[i
];
296 if (typeof(tick
) == "function") return;
299 var prec_axis
= 'y1';
300 if (tick
[0] == 1) { // right-side y-axis
301 x
= this.area
.x
+ this.area
.w
;
305 var y
= this.area
.y
+ tick
[1] * this.area
.h
;
307 /* Tick marks are currently clipped, so don't bother drawing them.
309 context.moveTo(halfUp(x), halfDown(y));
310 context.lineTo(halfUp(x - sgn * this.attr_('axisTickSize')), halfDown(y));
315 var label
= makeDiv(tick
[2], 'y', num_axes
== 2 ? prec_axis
: null);
316 var top
= (y
- this.attr_('axisLabelFontSize') / 2);
317 if (top
< 0) top
= 0;
319 if (top
+ this.attr_('axisLabelFontSize') + 3 > this.height
) {
320 label
.style
.bottom
= "0px";
322 label
.style
.top
= top
+ "px";
325 label
.style
.left
= (this.area
.x
- this.attr_('yAxisLabelWidth') - this.attr_('axisTickSize')) + "px";
326 label
.style
.textAlign
= "right";
327 } else if (tick
[0] == 1) {
328 label
.style
.left
= (this.area
.x
+ this.area
.w
+
329 this.attr_('axisTickSize')) + "px";
330 label
.style
.textAlign
= "left";
332 label
.style
.width
= this.attr_('yAxisLabelWidth') + "px";
333 this.container
.appendChild(label
);
334 this.ylabels
.push(label
);
337 // The lowest tick on the y-axis often overlaps with the leftmost
338 // tick on the x-axis. Shift the bottom tick up a little bit to
339 // compensate if necessary.
340 var bottomTick
= this.ylabels
[0];
341 var fontSize
= this.attr_('axisLabelFontSize');
342 var bottom
= parseInt(bottomTick
.style
.top
) + fontSize
;
343 if (bottom
> this.height
- fontSize
) {
344 bottomTick
.style
.top
= (parseInt(bottomTick
.style
.top
) -
345 fontSize
/ 2) + "px";
349 // draw a vertical line on the left to separate the chart from the labels.
351 context
.moveTo(halfUp(this.area
.x
), halfDown(this.area
.y
));
352 context
.lineTo(halfUp(this.area
.x
), halfDown(this.area
.y
+ this.area
.h
));
356 // if there's a secondary y-axis, draw a vertical line for that, too.
357 if (this.dygraph_
.numAxes() == 2) {
359 context
.moveTo(halfDown(this.area
.x
+ this.area
.w
), halfDown(this.area
.y
));
360 context
.lineTo(halfDown(this.area
.x
+ this.area
.w
), halfDown(this.area
.y
+ this.area
.h
));
366 if (this.attr_('drawXAxis')) {
367 if (this.layout
.xticks
) {
368 for (var i
= 0; i
< this.layout
.xticks
.length
; i
++) {
369 var tick
= this.layout
.xticks
[i
];
370 if (typeof(dataset
) == "function") return;
372 var x
= this.area
.x
+ tick
[0] * this.area
.w
;
373 var y
= this.area
.y
+ this.area
.h
;
375 /* Tick marks are currently clipped, so don't bother drawing them.
377 context.moveTo(halfUp(x), halfDown(y));
378 context.lineTo(halfUp(x), halfDown(y + this.attr_('axisTickSize')));
383 var label
= makeDiv(tick
[1], 'x');
384 label
.style
.textAlign
= "center";
385 label
.style
.top
= (y
+ this.attr_('axisTickSize')) + 'px';
387 var left
= (x
- this.attr_('axisLabelWidth')/2);
388 if (left
+ this.attr_('axisLabelWidth') > this.width
) {
389 left
= this.width
- this.attr_('xAxisLabelWidth');
390 label
.style
.textAlign
= "right";
394 label
.style
.textAlign
= "left";
397 label
.style
.left
= left
+ "px";
398 label
.style
.width
= this.attr_('xAxisLabelWidth') + "px";
399 this.container
.appendChild(label
);
400 this.xlabels
.push(label
);
405 context
.moveTo(halfUp(this.area
.x
), halfDown(this.area
.y
+ this.area
.h
));
406 context
.lineTo(halfUp(this.area
.x
+ this.area
.w
), halfDown(this.area
.y
+ this.area
.h
));
415 DygraphCanvasRenderer
.prototype._renderChartLabels
= function() {
416 // Generate divs for the chart title, xlabel and ylabel.
417 // Space for these divs has already been taken away from the charting area in
418 // the DygraphCanvasRenderer constructor.
419 if (this.attr_('title')) {
420 var div
= document
.createElement("div");
421 div
.style
.position
= 'absolute';
422 div
.style
.top
= '0px';
423 div
.style
.left
= this.area
.x
+ 'px';
424 div
.style
.width
= this.area
.w
+ 'px';
425 div
.style
.height
= this.attr_('titleHeight') + 'px';
426 div
.style
.textAlign
= 'center';
427 div
.style
.fontSize
= (this.attr_('titleHeight') - 8) + 'px';
428 div
.style
.fontWeight
= 'bold';
429 var class_div
= document
.createElement("div");
430 class_div
.className
= 'dygraph-label dygraph-title';
431 class_div
.innerHTML
= this.attr_('title');
432 div
.appendChild(class_div
);
433 this.container
.appendChild(div
);
434 this.chartLabels
.title
= div
;
437 if (this.attr_('xlabel')) {
438 var div
= document
.createElement("div");
439 div
.style
.position
= 'absolute';
440 div
.style
.bottom
= 0; // TODO(danvk): this is lazy. Calculate style.top.
441 div
.style
.left
= this.area
.x
+ 'px';
442 div
.style
.width
= this.area
.w
+ 'px';
443 div
.style
.height
= this.attr_('xLabelHeight') + 'px';
444 div
.style
.textAlign
= 'center';
445 div
.style
.fontSize
= (this.attr_('xLabelHeight') - 2) + 'px';
447 var class_div
= document
.createElement("div");
448 class_div
.className
= 'dygraph-label dygraph-xlabel';
449 class_div
.innerHTML
= this.attr_('xlabel');
450 div
.appendChild(class_div
);
451 this.container
.appendChild(div
);
452 this.chartLabels
.xlabel
= div
;
455 if (this.attr_('ylabel')) {
459 width
: this.attr_('yLabelWidth'),
462 // TODO(danvk): is this outer div actually necessary?
463 var div
= document
.createElement("div");
464 div
.style
.position
= 'absolute';
465 div
.style
.left
= box
.left
;
466 div
.style
.top
= box
.top
+ 'px';
467 div
.style
.width
= box
.width
+ 'px';
468 div
.style
.height
= box
.height
+ 'px';
469 div
.style
.fontSize
= (this.attr_('yLabelWidth') - 2) + 'px';
471 var inner_div
= document
.createElement("div");
472 inner_div
.style
.position
= 'absolute';
473 inner_div
.style
.width
= box
.height
+ 'px';
474 inner_div
.style
.height
= box
.width
+ 'px';
475 inner_div
.style
.top
= (box
.height
/ 2 - box.width / 2) + 'px';
476 inner_div
.style
.left
= (box
.width
/ 2 - box.height / 2) + 'px';
477 inner_div
.style
.textAlign
= 'center';
479 // CSS rotation is an HTML5 feature which is not standardized. Hence every
480 // browser has its own name for the CSS style.
481 inner_div
.style
.transform
= 'rotate(-90deg)'; // HTML5
482 inner_div
.style
.WebkitTransform
= 'rotate(-90deg)'; // Safari/Chrome
483 inner_div
.style
.MozTransform
= 'rotate(-90deg)'; // Firefox
484 inner_div
.style
.OTransform
= 'rotate(-90deg)'; // Opera
485 inner_div
.style
.msTransform
= 'rotate(-90deg)'; // IE9
487 if (typeof(document
.documentMode
) !== 'undefined' &&
488 document
.documentMode
< 9) {
489 // We're dealing w/ an old version of IE
, so we have to rotate the text
490 // using a BasicImage transform. This uses a different origin of rotation
491 // than HTML5 rotation (top left of div vs. its center).
492 inner_div
.style
.filter
=
493 'progid:DXImageTransform.Microsoft.BasicImage(rotation=3)';
494 inner_div
.style
.left
= '0px';
495 inner_div
.style
.top
= '0px';
498 var class_div
= document
.createElement("div");
499 class_div
.className
= 'dygraph-label dygraph-ylabel';
500 class_div
.innerHTML
= this.attr_('ylabel');
502 inner_div
.appendChild(class_div
);
503 div
.appendChild(inner_div
);
504 this.container
.appendChild(div
);
505 this.chartLabels
.ylabel
= div
;
510 DygraphCanvasRenderer
.prototype._renderAnnotations
= function() {
511 var annotationStyle
= {
512 "position": "absolute",
513 "fontSize": this.attr_('axisLabelFontSize') + "px",
518 var bindEvt
= function(eventName
, classEventName
, p
, self
) {
520 var a
= p
.annotation
;
521 if (a
.hasOwnProperty(eventName
)) {
522 a
[eventName
](a
, p
, self
.dygraph_
, e
);
523 } else if (self
.dygraph_
.attr_(classEventName
)) {
524 self
.dygraph_
.attr_(classEventName
)(a
, p
, self
.dygraph_
,e
);
529 // Get a list of point with annotations.
530 var points
= this.layout
.annotated_points
;
531 for (var i
= 0; i
< points
.length
; i
++) {
533 if (p
.canvasx
< this.area
.x
|| p
.canvasx
> this.area
.x
+ this.area
.w
) {
537 var a
= p
.annotation
;
539 if (a
.hasOwnProperty("tickHeight")) {
540 tick_height
= a
.tickHeight
;
543 var div
= document
.createElement("div");
544 for (var name
in annotationStyle
) {
545 if (annotationStyle
.hasOwnProperty(name
)) {
546 div
.style
[name
] = annotationStyle
[name
];
549 if (!a
.hasOwnProperty('icon')) {
550 div
.className
= "dygraphDefaultAnnotation";
552 if (a
.hasOwnProperty('cssClass')) {
553 div
.className
+= " " + a
.cssClass
;
556 var width
= a
.hasOwnProperty('width') ? a
.width
: 16;
557 var height
= a
.hasOwnProperty('height') ? a
.height
: 16;
558 if (a
.hasOwnProperty('icon')) {
559 var img
= document
.createElement("img");
563 div
.appendChild(img
);
564 } else if (p
.annotation
.hasOwnProperty('shortText')) {
565 div
.appendChild(document
.createTextNode(p
.annotation
.shortText
));
567 div
.style
.left
= (p
.canvasx
- width
/ 2) + "px";
568 if (a
.attachAtBottom
) {
569 div
.style
.top
= (this.area
.h
- height
- tick_height
) + "px";
571 div
.style
.top
= (p
.canvasy
- height
- tick_height
) + "px";
573 div
.style
.width
= width
+ "px";
574 div
.style
.height
= height
+ "px";
575 div
.title
= p
.annotation
.text
;
576 div
.style
.color
= this.colors
[p
.name
];
577 div
.style
.borderColor
= this.colors
[p
.name
];
580 Dygraph
.addEvent(div
, 'click',
581 bindEvt('clickHandler', 'annotationClickHandler', p
, this));
582 Dygraph
.addEvent(div
, 'mouseover',
583 bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p
, this));
584 Dygraph
.addEvent(div
, 'mouseout',
585 bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p
, this));
586 Dygraph
.addEvent(div
, 'dblclick',
587 bindEvt('dblClickHandler', 'annotationDblClickHandler', p
, this));
589 this.container
.appendChild(div
);
590 this.annotations
.push(div
);
592 var ctx
= this.elementContext
;
593 ctx
.strokeStyle
= this.colors
[p
.name
];
595 if (!a
.attachAtBottom
) {
596 ctx
.moveTo(p
.canvasx
, p
.canvasy
);
597 ctx
.lineTo(p
.canvasx
, p
.canvasy
- 2 - tick_height
);
599 ctx
.moveTo(p
.canvasx
, this.area
.h
);
600 ctx
.lineTo(p
.canvasx
, this.area
.h
- 2 - tick_height
);
609 * Overrides the CanvasRenderer method to draw error bars
611 DygraphCanvasRenderer
.prototype._renderLineChart
= function() {
612 var isNullOrNaN
= function(x
) {
613 return (x
=== null || isNaN(x
));
616 // TODO(danvk): use this.attr_ for many of these.
617 var context
= this.elementContext
;
618 var fillAlpha
= this.attr_('fillAlpha');
619 var errorBars
= this.attr_("errorBars") || this.attr_("customBars");
620 var fillGraph
= this.attr_("fillGraph");
621 var stackedGraph
= this.attr_("stackedGraph");
622 var stepPlot
= this.attr_("stepPlot");
623 var points
= this.layout
.points
;
624 var pointsLength
= points
.length
;
627 for (var name
in this.layout
.datasets
) {
628 if (this.layout
.datasets
.hasOwnProperty(name
)) {
632 var setCount
= setNames
.length
;
634 // TODO(danvk): Move this mapping into Dygraph and get it out of here.
636 for (var i
= 0; i
< setCount
; i
++) {
637 this.colors
[setNames
[i
]] = this.colorScheme_
[i
% this.colorScheme_
.length
];
642 for (var i
= pointsLength
; i
--;) {
643 var point
= points
[i
];
644 point
.canvasx
= this.area
.w
* point
.x
+ this.area
.x
;
645 point
.canvasy
= this.area
.h
* point
.y
+ this.area
.y
;
652 this.dygraph_
.warn("Can't use fillGraph option with error bars");
655 for (var i
= 0; i
< setCount
; i
++) {
656 var setName
= setNames
[i
];
657 var axis
= this.dygraph_
.axisPropertiesForSeries(setName
);
658 var color
= this.colors
[setName
];
660 // setup graphics context
664 var prevYs
= [-1, -1];
665 var yscale
= axis
.yscale
;
666 // should be same color as the lines but only 15% opaque.
667 var rgb
= new RGBColor(color
);
668 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
670 ctx
.fillStyle
= err_color
;
672 for (var j
= 0; j
< pointsLength
; j
++) {
673 var point
= points
[j
];
674 if (point
.name
== setName
) {
675 if (!Dygraph
.isOK(point
.y
)) {
682 var newYs
= [ point
.y_bottom
, point
.y_top
];
685 var newYs
= [ point
.y_bottom
, point
.y_top
];
687 newYs
[0] = this.area
.h
* newYs
[0] + this.area
.y
;
688 newYs
[1] = this.area
.h
* newYs
[1] + this.area
.y
;
691 ctx
.moveTo(prevX
, newYs
[0]);
693 ctx
.moveTo(prevX
, prevYs
[0]);
695 ctx
.lineTo(point
.canvasx
, newYs
[0]);
696 ctx
.lineTo(point
.canvasx
, newYs
[1]);
698 ctx
.lineTo(prevX
, newYs
[1]);
700 ctx
.lineTo(prevX
, prevYs
[1]);
705 prevX
= point
.canvasx
;
710 } else if (fillGraph
) {
711 var baseline
= [] // for stacked graphs: baseline for filling
713 // process sets in reverse order (needed for stacked graphs)
714 for (var i
= setCount
- 1; i
>= 0; i
--) {
715 var setName
= setNames
[i
];
716 var color
= this.colors
[setName
];
717 var axis
= this.dygraph_
.axisPropertiesForSeries(setName
);
718 var axisY
= 1.0 + axis
.minyval
* axis
.yscale
;
719 if (axisY
< 0.0) axisY
= 0.0;
720 else if (axisY
> 1.0) axisY
= 1.0;
721 axisY
= this.area
.h
* axisY
+ this.area
.y
;
723 // setup graphics context
726 var prevYs
= [-1, -1];
727 var yscale
= axis
.yscale
;
728 // should be same color as the lines but only 15% opaque.
729 var rgb
= new RGBColor(color
);
730 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
732 ctx
.fillStyle
= err_color
;
734 for (var j
= 0; j
< pointsLength
; j
++) {
735 var point
= points
[j
];
736 if (point
.name
== setName
) {
737 if (!Dygraph
.isOK(point
.y
)) {
743 lastY
= baseline
[point
.canvasx
];
744 if (lastY
=== undefined
) lastY
= axisY
;
745 baseline
[point
.canvasx
] = point
.canvasy
;
746 newYs
= [ point
.canvasy
, lastY
];
748 newYs
= [ point
.canvasy
, axisY
];
751 ctx
.moveTo(prevX
, prevYs
[0]);
753 ctx
.lineTo(point
.canvasx
, prevYs
[0]);
755 ctx
.lineTo(point
.canvasx
, newYs
[0]);
757 ctx
.lineTo(point
.canvasx
, newYs
[1]);
758 ctx
.lineTo(prevX
, prevYs
[1]);
762 prevX
= point
.canvasx
;
769 // Drawing the lines.
770 var firstIndexInSet
= 0;
771 var afterLastIndexInSet
= 0;
773 for (var i
= 0; i
< setCount
; i
+= 1) {
774 setLength
= this.layout
.setPointsLengths
[i
];
775 afterLastIndexInSet
+= setLength
;
776 var setName
= setNames
[i
];
777 var color
= this.colors
[setName
];
778 var strokeWidth
= this.dygraph_
.attr_("strokeWidth", setName
);
780 // setup graphics context
782 var pointSize
= this.dygraph_
.attr_("pointSize", setName
);
783 var prevX
= null, prevY
= null;
784 var drawPoints
= this.dygraph_
.attr_("drawPoints", setName
);
785 for (var j
= firstIndexInSet
; j
< afterLastIndexInSet
; j
++) {
786 var point
= points
[j
];
787 if (isNullOrNaN(point
.canvasy
)) {
788 if (stepPlot
&& prevX
!= null) {
789 // Draw a horizontal line to the start of the missing data
791 ctx
.strokeStyle
= color
;
792 ctx
.lineWidth
= this.attr_('strokeWidth');
793 ctx
.moveTo(prevX
, prevY
);
794 ctx
.lineTo(point
.canvasx
, prevY
);
797 // this will make us move to the next point, not draw a line to it.
798 prevX
= prevY
= null;
800 // A point is "isolated" if it is non-null but both the previous
801 // and next points are null.
802 var isIsolated
= (!prevX
&& (j
== points
.length
- 1 ||
803 isNullOrNaN(points
[j
+1].canvasy
)));
804 if (prevX
=== null) {
805 prevX
= point
.canvasx
;
806 prevY
= point
.canvasy
;
808 // Skip over points that will be drawn in the same pixel.
809 if (Math
.round(prevX
) == Math
.round(point
.canvasx
) &&
810 Math
.round(prevY
) == Math
.round(point
.canvasy
)) {
813 // TODO(antrob): skip over points that lie on a line that is already
814 // going to be drawn. There is no need to have more than 2
815 // consecutive points that are collinear.
818 ctx
.strokeStyle
= color
;
819 ctx
.lineWidth
= strokeWidth
;
820 ctx
.moveTo(prevX
, prevY
);
822 ctx
.lineTo(point
.canvasx
, prevY
);
824 prevX
= point
.canvasx
;
825 prevY
= point
.canvasy
;
826 ctx
.lineTo(prevX
, prevY
);
831 if (drawPoints
|| isIsolated
) {
833 ctx
.fillStyle
= color
;
834 ctx
.arc(point
.canvasx
, point
.canvasy
, pointSize
,
835 0, 2 * Math
.PI
, false);
840 firstIndexInSet
= afterLastIndexInSet
;