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
.plotArea
;
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 var ctx
= this.dygraph_
.canvas_ctx_
;
55 ctx
.rect(this.area
.x
, this.area
.y
, this.area
.w
, this.area
.h
);
58 ctx
= this.dygraph_
.hidden_ctx_
;
60 ctx
.rect(this.area
.x
, this.area
.y
, this.area
.w
, this.area
.h
);
64 DygraphCanvasRenderer
.prototype.attr_
= function(x
) {
65 return this.dygraph_
.attr_(x
);
68 DygraphCanvasRenderer
.prototype.clear
= function() {
70 // VML takes a while to start up, so we just poll every this.IEDelay
72 if (this.clearDelay
) {
73 this.clearDelay
.cancel();
74 this.clearDelay
= null;
76 var context
= this.elementContext
;
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));
86 var context
= this.elementContext
;
87 context
.clearRect(0, 0, this.width
, this.height
);
89 for (var i
= 0; i
< this.xlabels
.length
; i
++) {
90 var el
= this.xlabels
[i
];
91 if (el
.parentNode
) el
.parentNode
.removeChild(el
);
93 for (var i
= 0; i
< this.ylabels
.length
; i
++) {
94 var el
= this.ylabels
[i
];
95 if (el
.parentNode
) el
.parentNode
.removeChild(el
);
97 for (var i
= 0; i
< this.annotations
.length
; i
++) {
98 var el
= this.annotations
[i
];
99 if (el
.parentNode
) el
.parentNode
.removeChild(el
);
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
);
106 this.xlabels
= new Array();
107 this.ylabels
= new Array();
108 this.annotations
= new Array();
109 this.chartLabels
= {};
113 DygraphCanvasRenderer
.isSupported
= function(canvasName
) {
116 if (typeof(canvasName
) == 'undefined' || canvasName
== null)
117 canvas
= document
.createElement("canvas");
120 var context
= canvas
.getContext("2d");
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
))
133 * @param { [String] } colors Array of color strings. Should have one entry for
134 * each series to be rendered.
136 DygraphCanvasRenderer
.prototype.setColors
= function(colors
) {
137 this.colorScheme_
= colors
;
141 * Draw an X/Y grid on top of the existing plot
143 DygraphCanvasRenderer
.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};
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_
);
156 if (this.attr_('drawYGrid')) {
157 var ticks
= this.layout
.yticks
;
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
);
168 ctx
.lineTo(x
+ this.area
.w
, y
);
174 if (this.attr_('drawXGrid')) {
175 var ticks
= this.layout
.xticks
;
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
);
184 ctx
.lineTo(x
, this.area
.y
);
190 // Do the ordinary rendering, as before
191 this._renderLineChart();
193 this._renderChartLabels();
194 this._renderAnnotations();
198 DygraphCanvasRenderer
.prototype._renderAxis
= function() {
199 if (!this.attr_('drawXAxis') && !this.attr_('drawYAxis')) return;
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};
205 var context
= this.elementContext
;
208 position
: "absolute",
209 fontSize
: this.attr_('axisLabelFontSize') + "px",
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.
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
];
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
);
235 context
.strokeStyle
= this.attr_('axisLineColor');
236 context
.lineWidth
= this.attr_('axisLineWidth');
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;
246 var prec_axis
= 'y1';
247 if (tick
[0] == 1) { // right-side y-axis
248 x
= this.area
.x
+ this.area
.w
;
252 var y
= this.area
.y
+ tick
[1] * this.area
.h
;
254 context
.moveTo(halfUp(x
), halfDown(y
));
255 context
.lineTo(halfUp(x
- sgn
* this.attr_('axisTickSize')), halfDown(y
));
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;
263 if (top
+ this.attr_('axisLabelFontSize') + 3 > this.height
) {
264 label
.style
.bottom
= "0px";
266 label
.style
.top
= top
+ "px";
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";
276 label
.style
.width
= this.attr_('yAxisLabelWidth') + "px";
277 this.container
.appendChild(label
);
278 this.ylabels
.push(label
);
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";
293 // draw a vertical line on the left to separate the chart from the labels.
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
));
300 // if there's a secondary y-axis, draw a vertical line for that, too.
301 if (this.dygraph_
.numAxes() == 2) {
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
));
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;
316 var x
= this.area
.x
+ tick
[0] * this.area
.w
;
317 var y
= this.area
.y
+ this.area
.h
;
319 context
.moveTo(halfUp(x
), halfDown(y
));
320 context
.lineTo(halfUp(x
), halfDown(y
+ this.attr_('axisTickSize')));
324 var label
= makeDiv(tick
[1], 'x');
325 label
.style
.textAlign
= "center";
326 label
.style
.top
= (y
+ this.attr_('axisTickSize')) + 'px';
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";
335 label
.style
.textAlign
= "left";
338 label
.style
.left
= left
+ "px";
339 label
.style
.width
= this.attr_('xAxisLabelWidth') + "px";
340 this.container
.appendChild(label
);
341 this.xlabels
.push(label
);
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
));
356 DygraphCanvasRenderer
.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
;
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';
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
;
396 if (this.attr_('ylabel')) {
400 width
: this.attr_('yLabelWidth'),
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';
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';
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
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';
439 var class_div
= document
.createElement("div");
440 class_div
.className
= 'dygraph-label dygraph-ylabel';
441 class_div
.innerHTML
= this.attr_('ylabel');
443 inner_div
.appendChild(class_div
);
444 div
.appendChild(inner_div
);
445 this.container
.appendChild(div
);
446 this.chartLabels
.ylabel
= div
;
451 DygraphCanvasRenderer
.prototype._renderAnnotations
= function() {
452 var annotationStyle
= {
453 "position": "absolute",
454 "fontSize": this.attr_('axisLabelFontSize') + "px",
459 var bindEvt
= function(eventName
, classEventName
, p
, self
) {
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
);
470 // Get a list of point with annotations.
471 var points
= this.layout
.annotated_points
;
472 for (var i
= 0; i
< points
.length
; i
++) {
474 if (p
.canvasx
< this.area
.x
|| p
.canvasx
> this.area
.x
+ this.area
.w
) {
478 var a
= p
.annotation
;
480 if (a
.hasOwnProperty("tickHeight")) {
481 tick_height
= a
.tickHeight
;
484 var div
= document
.createElement("div");
485 for (var name
in annotationStyle
) {
486 if (annotationStyle
.hasOwnProperty(name
)) {
487 div
.style
[name
] = annotationStyle
[name
];
490 if (!a
.hasOwnProperty('icon')) {
491 div
.className
= "dygraphDefaultAnnotation";
493 if (a
.hasOwnProperty('cssClass')) {
494 div
.className
+= " " + a
.cssClass
;
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");
504 div
.appendChild(img
);
505 } else if (p
.annotation
.hasOwnProperty('shortText')) {
506 div
.appendChild(document
.createTextNode(p
.annotation
.shortText
));
508 div
.style
.left
= (p
.canvasx
- width
/ 2) + "px";
509 if (a
.attachAtBottom
) {
510 div
.style
.top
= (this.area
.h
- height
- tick_height
) + "px";
512 div
.style
.top
= (p
.canvasy
- height
- tick_height
) + "px";
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
];
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));
530 this.container
.appendChild(div
);
531 this.annotations
.push(div
);
533 var ctx
= this.elementContext
;
534 ctx
.strokeStyle
= this.colors
[p
.name
];
536 if (!a
.attachAtBottom
) {
537 ctx
.moveTo(p
.canvasx
, p
.canvasy
);
538 ctx
.lineTo(p
.canvasx
, p
.canvasy
- 2 - tick_height
);
540 ctx
.moveTo(p
.canvasx
, this.area
.h
);
541 ctx
.lineTo(p
.canvasx
, this.area
.h
- 2 - tick_height
);
550 * Overrides the CanvasRenderer method to draw error bars
552 DygraphCanvasRenderer
.prototype._renderLineChart
= function() {
553 var isNullOrNaN
= function(x
) {
554 return (x
=== null || isNaN(x
));
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
;
568 for (var name
in this.layout
.datasets
) {
569 if (this.layout
.datasets
.hasOwnProperty(name
)) {
573 var setCount
= setNames
.length
;
575 // TODO(danvk): Move this mapping into Dygraph and get it out of here.
577 for (var i
= 0; i
< setCount
; i
++) {
578 this.colors
[setNames
[i
]] = this.colorScheme_
[i
% this.colorScheme_
.length
];
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
;
593 this.dygraph_
.warn("Can't use fillGraph option with error bars");
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
];
601 // setup graphics context
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
+ ',' +
611 ctx
.fillStyle
= err_color
;
613 for (var j
= 0; j
< pointsLength
; j
++) {
614 var point
= points
[j
];
615 if (point
.name
== setName
) {
616 if (!Dygraph
.isOK(point
.y
)) {
623 var newYs
= [ point
.y_bottom
, point
.y_top
];
626 var newYs
= [ point
.y_bottom
, point
.y_top
];
628 newYs
[0] = this.area
.h
* newYs
[0] + this.area
.y
;
629 newYs
[1] = this.area
.h
* newYs
[1] + this.area
.y
;
632 ctx
.moveTo(prevX
, newYs
[0]);
634 ctx
.moveTo(prevX
, prevYs
[0]);
636 ctx
.lineTo(point
.canvasx
, newYs
[0]);
637 ctx
.lineTo(point
.canvasx
, newYs
[1]);
639 ctx
.lineTo(prevX
, newYs
[1]);
641 ctx
.lineTo(prevX
, prevYs
[1]);
646 prevX
= point
.canvasx
;
651 } else if (fillGraph
) {
652 var baseline
= [] // for stacked graphs: baseline for filling
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
;
664 // setup graphics context
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
+ ',' +
673 ctx
.fillStyle
= err_color
;
675 for (var j
= 0; j
< pointsLength
; j
++) {
676 var point
= points
[j
];
677 if (point
.name
== setName
) {
678 if (!Dygraph
.isOK(point
.y
)) {
684 lastY
= baseline
[point
.canvasx
];
685 if (lastY
=== undefined
) lastY
= axisY
;
686 baseline
[point
.canvasx
] = point
.canvasy
;
687 newYs
= [ point
.canvasy
, lastY
];
689 newYs
= [ point
.canvasy
, axisY
];
692 ctx
.moveTo(prevX
, prevYs
[0]);
694 ctx
.lineTo(point
.canvasx
, prevYs
[0]);
696 ctx
.lineTo(point
.canvasx
, newYs
[0]);
698 ctx
.lineTo(point
.canvasx
, newYs
[1]);
699 ctx
.lineTo(prevX
, prevYs
[1]);
703 prevX
= point
.canvasx
;
710 // Drawing the lines.
711 var firstIndexInSet
= 0;
712 var afterLastIndexInSet
= 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
);
721 // setup graphics context
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
732 ctx
.strokeStyle
= color
;
733 ctx
.lineWidth
= this.attr_('strokeWidth');
734 ctx
.moveTo(prevX
, prevY
);
735 ctx
.lineTo(point
.canvasx
, prevY
);
738 // this will make us move to the next point, not draw a line to it.
739 prevX
= prevY
= null;
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
;
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
)) {
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.
759 ctx
.strokeStyle
= color
;
760 ctx
.lineWidth
= strokeWidth
;
761 ctx
.moveTo(prevX
, prevY
);
763 ctx
.lineTo(point
.canvasx
, prevY
);
765 prevX
= point
.canvasx
;
766 prevY
= point
.canvasy
;
767 ctx
.lineTo(prevX
, prevY
);
772 if (drawPoints
|| isIsolated
) {
774 ctx
.fillStyle
= color
;
775 ctx
.arc(point
.canvasx
, point
.canvasy
, pointSize
,
776 0, 2 * Math
.PI
, false);
781 firstIndexInSet
= afterLastIndexInSet
;