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 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
;
158 // TODO(konigsberg): I don't think these calls to save() have a corresponding restore().
160 ctx
.strokeStyle
= this.attr_('gridLineColor');
161 ctx
.lineWidth
= this.attr_('gridLineWidth');
162 for (var i
= 0; i
< ticks
.length
; i
++) {
163 // TODO(danvk): allow secondary axes to draw a grid, too.
164 if (ticks
[i
][0] != 0) continue;
165 var x
= halfUp(this.area
.x
);
166 var y
= halfDown(this.area
.y
+ ticks
[i
][1] * this.area
.h
);
169 ctx
.lineTo(x
+ this.area
.w
, y
);
175 if (this.attr_('drawXGrid')) {
176 var ticks
= this.layout
.xticks
;
178 ctx
.strokeStyle
= this.attr_('gridLineColor');
179 ctx
.lineWidth
= this.attr_('gridLineWidth');
180 for (var i
=0; i
<ticks
.length
; i
++) {
181 var x
= halfUp(this.area
.x
+ ticks
[i
][0] * this.area
.w
);
182 var y
= halfDown(this.area
.y
+ this.area
.h
);
185 ctx
.lineTo(x
, this.area
.y
);
191 // Do the ordinary rendering, as before
192 this._renderLineChart();
194 this._renderChartLabels();
195 this._renderAnnotations();
199 DygraphCanvasRenderer
.prototype._renderAxis
= function() {
200 if (!this.attr_('drawXAxis') && !this.attr_('drawYAxis')) return;
202 // Round pixels to half-integer boundaries for crisper drawing.
203 function halfUp(x
){return Math
.round(x
)+0.5};
204 function halfDown(y
){return Math
.round(y
)-0.5};
206 var context
= this.elementContext
;
209 position
: "absolute",
210 fontSize
: this.attr_('axisLabelFontSize') + "px",
212 color
: this.attr_('axisLabelColor'),
213 width
: this.attr_('axisLabelWidth') + "px",
214 // height: this.attr_('axisLabelFontSize') + 2 + "px",
215 lineHeight
: "normal", // Something other than "normal" line-height screws up label positioning.
218 var makeDiv
= function(txt
, axis
, prec_axis
) {
219 var div
= document
.createElement("div");
220 for (var name
in labelStyle
) {
221 if (labelStyle
.hasOwnProperty(name
)) {
222 div
.style
[name
] = labelStyle
[name
];
225 var inner_div
= document
.createElement("div");
226 inner_div
.className
= 'dygraph-axis-label' +
227 ' dygraph-axis-label-' + axis
+
228 (prec_axis
? ' dygraph-axis-label-' + prec_axis
: '');
229 inner_div
.appendChild(document
.createTextNode(txt
));
230 div
.appendChild(inner_div
);
236 context
.strokeStyle
= this.attr_('axisLineColor');
237 context
.lineWidth
= this.attr_('axisLineWidth');
239 if (this.attr_('drawYAxis')) {
240 if (this.layout
.yticks
&& this.layout
.yticks
.length
> 0) {
241 var num_axes
= this.dygraph_
.numAxes();
242 for (var i
= 0; i
< this.layout
.yticks
.length
; i
++) {
243 var tick
= this.layout
.yticks
[i
];
244 if (typeof(tick
) == "function") return;
247 var prec_axis
= 'y1';
248 if (tick
[0] == 1) { // right-side y-axis
249 x
= this.area
.x
+ this.area
.w
;
253 var y
= this.area
.y
+ tick
[1] * this.area
.h
;
255 context
.moveTo(halfUp(x
), halfDown(y
));
256 context
.lineTo(halfUp(x
- sgn
* this.attr_('axisTickSize')), halfDown(y
));
260 var label
= makeDiv(tick
[2], 'y', num_axes
== 2 ? prec_axis
: null);
261 var top
= (y
- this.attr_('axisLabelFontSize') / 2);
262 if (top
< 0) top
= 0;
264 if (top
+ this.attr_('axisLabelFontSize') + 3 > this.height
) {
265 label
.style
.bottom
= "0px";
267 label
.style
.top
= top
+ "px";
270 label
.style
.left
= (this.area
.x
- this.attr_('yAxisLabelWidth') - this.attr_('axisTickSize')) + "px";
271 label
.style
.textAlign
= "right";
272 } else if (tick
[0] == 1) {
273 label
.style
.left
= (this.area
.x
+ this.area
.w
+
274 this.attr_('axisTickSize')) + "px";
275 label
.style
.textAlign
= "left";
277 label
.style
.width
= this.attr_('yAxisLabelWidth') + "px";
278 this.container
.appendChild(label
);
279 this.ylabels
.push(label
);
282 // The lowest tick on the y-axis often overlaps with the leftmost
283 // tick on the x-axis. Shift the bottom tick up a little bit to
284 // compensate if necessary.
285 var bottomTick
= this.ylabels
[0];
286 var fontSize
= this.attr_('axisLabelFontSize');
287 var bottom
= parseInt(bottomTick
.style
.top
) + fontSize
;
288 if (bottom
> this.height
- fontSize
) {
289 bottomTick
.style
.top
= (parseInt(bottomTick
.style
.top
) -
290 fontSize
/ 2) + "px";
294 // draw a vertical line on the left to separate the chart from the labels.
296 context
.moveTo(halfUp(this.area
.x
), halfDown(this.area
.y
));
297 context
.lineTo(halfUp(this.area
.x
), halfDown(this.area
.y
+ this.area
.h
));
301 // if there's a secondary y-axis, draw a vertical line for that, too.
302 if (this.dygraph_
.numAxes() == 2) {
304 context
.moveTo(halfDown(this.area
.x
+ this.area
.w
), halfDown(this.area
.y
));
305 context
.lineTo(halfDown(this.area
.x
+ this.area
.w
), halfDown(this.area
.y
+ this.area
.h
));
311 if (this.attr_('drawXAxis')) {
312 if (this.layout
.xticks
) {
313 for (var i
= 0; i
< this.layout
.xticks
.length
; i
++) {
314 var tick
= this.layout
.xticks
[i
];
315 if (typeof(dataset
) == "function") return;
317 var x
= this.area
.x
+ tick
[0] * this.area
.w
;
318 var y
= this.area
.y
+ this.area
.h
;
320 context
.moveTo(halfUp(x
), halfDown(y
));
321 context
.lineTo(halfUp(x
), halfDown(y
+ this.attr_('axisTickSize')));
325 var label
= makeDiv(tick
[1], 'x');
326 label
.style
.textAlign
= "center";
327 label
.style
.top
= (y
+ this.attr_('axisTickSize')) + 'px';
329 var left
= (x
- this.attr_('axisLabelWidth')/2);
330 if (left
+ this.attr_('axisLabelWidth') > this.width
) {
331 left
= this.width
- this.attr_('xAxisLabelWidth');
332 label
.style
.textAlign
= "right";
336 label
.style
.textAlign
= "left";
339 label
.style
.left
= left
+ "px";
340 label
.style
.width
= this.attr_('xAxisLabelWidth') + "px";
341 this.container
.appendChild(label
);
342 this.xlabels
.push(label
);
347 context
.moveTo(halfUp(this.area
.x
), halfDown(this.area
.y
+ this.area
.h
));
348 context
.lineTo(halfUp(this.area
.x
+ this.area
.w
), halfDown(this.area
.y
+ this.area
.h
));
357 DygraphCanvasRenderer
.prototype._renderChartLabels
= function() {
358 // Generate divs for the chart title, xlabel and ylabel.
359 // Space for these divs has already been taken away from the charting area in
360 // the DygraphCanvasRenderer constructor.
361 if (this.attr_('title')) {
362 var div
= document
.createElement("div");
363 div
.style
.position
= 'absolute';
364 div
.style
.top
= '0px';
365 div
.style
.left
= this.area
.x
+ 'px';
366 div
.style
.width
= this.area
.w
+ 'px';
367 div
.style
.height
= this.attr_('titleHeight') + 'px';
368 div
.style
.textAlign
= 'center';
369 div
.style
.fontSize
= (this.attr_('titleHeight') - 8) + 'px';
370 div
.style
.fontWeight
= 'bold';
371 var class_div
= document
.createElement("div");
372 class_div
.className
= 'dygraph-label dygraph-title';
373 class_div
.innerHTML
= this.attr_('title');
374 div
.appendChild(class_div
);
375 this.container
.appendChild(div
);
376 this.chartLabels
.title
= div
;
379 if (this.attr_('xlabel')) {
380 var div
= document
.createElement("div");
381 div
.style
.position
= 'absolute';
382 div
.style
.bottom
= 0; // TODO(danvk): this is lazy. Calculate style.top.
383 div
.style
.left
= this.area
.x
+ 'px';
384 div
.style
.width
= this.area
.w
+ 'px';
385 div
.style
.height
= this.attr_('xLabelHeight') + 'px';
386 div
.style
.textAlign
= 'center';
387 div
.style
.fontSize
= (this.attr_('xLabelHeight') - 2) + 'px';
389 var class_div
= document
.createElement("div");
390 class_div
.className
= 'dygraph-label dygraph-xlabel';
391 class_div
.innerHTML
= this.attr_('xlabel');
392 div
.appendChild(class_div
);
393 this.container
.appendChild(div
);
394 this.chartLabels
.xlabel
= div
;
397 if (this.attr_('ylabel')) {
401 width
: this.attr_('yLabelWidth'),
404 // TODO(danvk): is this outer div actually necessary?
405 var div
= document
.createElement("div");
406 div
.style
.position
= 'absolute';
407 div
.style
.left
= box
.left
;
408 div
.style
.top
= box
.top
+ 'px';
409 div
.style
.width
= box
.width
+ 'px';
410 div
.style
.height
= box
.height
+ 'px';
411 div
.style
.fontSize
= (this.attr_('yLabelWidth') - 2) + 'px';
413 var inner_div
= document
.createElement("div");
414 inner_div
.style
.position
= 'absolute';
415 inner_div
.style
.width
= box
.height
+ 'px';
416 inner_div
.style
.height
= box
.width
+ 'px';
417 inner_div
.style
.top
= (box
.height
/ 2 - box.width / 2) + 'px';
418 inner_div
.style
.left
= (box
.width
/ 2 - box.height / 2) + 'px';
419 inner_div
.style
.textAlign
= 'center';
421 // CSS rotation is an HTML5 feature which is not standardized. Hence every
422 // browser has its own name for the CSS style.
423 inner_div
.style
.transform
= 'rotate(-90deg)'; // HTML5
424 inner_div
.style
.WebkitTransform
= 'rotate(-90deg)'; // Safari/Chrome
425 inner_div
.style
.MozTransform
= 'rotate(-90deg)'; // Firefox
426 inner_div
.style
.OTransform
= 'rotate(-90deg)'; // Opera
427 inner_div
.style
.msTransform
= 'rotate(-90deg)'; // IE9
429 if (typeof(document
.documentMode
) !== 'undefined' &&
430 document
.documentMode
< 9) {
431 // We're dealing w/ an old version of IE
, so we have to rotate the text
432 // using a BasicImage transform. This uses a different origin of rotation
433 // than HTML5 rotation (top left of div vs. its center).
434 inner_div
.style
.filter
=
435 'progid:DXImageTransform.Microsoft.BasicImage(rotation=3)';
436 inner_div
.style
.left
= '0px';
437 inner_div
.style
.top
= '0px';
440 var class_div
= document
.createElement("div");
441 class_div
.className
= 'dygraph-label dygraph-ylabel';
442 class_div
.innerHTML
= this.attr_('ylabel');
444 inner_div
.appendChild(class_div
);
445 div
.appendChild(inner_div
);
446 this.container
.appendChild(div
);
447 this.chartLabels
.ylabel
= div
;
452 DygraphCanvasRenderer
.prototype._renderAnnotations
= function() {
453 var annotationStyle
= {
454 "position": "absolute",
455 "fontSize": this.attr_('axisLabelFontSize') + "px",
460 var bindEvt
= function(eventName
, classEventName
, p
, self
) {
462 var a
= p
.annotation
;
463 if (a
.hasOwnProperty(eventName
)) {
464 a
[eventName
](a
, p
, self
.dygraph_
, e
);
465 } else if (self
.dygraph_
.attr_(classEventName
)) {
466 self
.dygraph_
.attr_(classEventName
)(a
, p
, self
.dygraph_
,e
);
471 // Get a list of point with annotations.
472 var points
= this.layout
.annotated_points
;
473 for (var i
= 0; i
< points
.length
; i
++) {
475 if (p
.canvasx
< this.area
.x
|| p
.canvasx
> this.area
.x
+ this.area
.w
) {
479 var a
= p
.annotation
;
481 if (a
.hasOwnProperty("tickHeight")) {
482 tick_height
= a
.tickHeight
;
485 var div
= document
.createElement("div");
486 for (var name
in annotationStyle
) {
487 if (annotationStyle
.hasOwnProperty(name
)) {
488 div
.style
[name
] = annotationStyle
[name
];
491 if (!a
.hasOwnProperty('icon')) {
492 div
.className
= "dygraphDefaultAnnotation";
494 if (a
.hasOwnProperty('cssClass')) {
495 div
.className
+= " " + a
.cssClass
;
498 var width
= a
.hasOwnProperty('width') ? a
.width
: 16;
499 var height
= a
.hasOwnProperty('height') ? a
.height
: 16;
500 if (a
.hasOwnProperty('icon')) {
501 var img
= document
.createElement("img");
505 div
.appendChild(img
);
506 } else if (p
.annotation
.hasOwnProperty('shortText')) {
507 div
.appendChild(document
.createTextNode(p
.annotation
.shortText
));
509 div
.style
.left
= (p
.canvasx
- width
/ 2) + "px";
510 if (a
.attachAtBottom
) {
511 div
.style
.top
= (this.area
.h
- height
- tick_height
) + "px";
513 div
.style
.top
= (p
.canvasy
- height
- tick_height
) + "px";
515 div
.style
.width
= width
+ "px";
516 div
.style
.height
= height
+ "px";
517 div
.title
= p
.annotation
.text
;
518 div
.style
.color
= this.colors
[p
.name
];
519 div
.style
.borderColor
= this.colors
[p
.name
];
522 Dygraph
.addEvent(div
, 'click',
523 bindEvt('clickHandler', 'annotationClickHandler', p
, this));
524 Dygraph
.addEvent(div
, 'mouseover',
525 bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p
, this));
526 Dygraph
.addEvent(div
, 'mouseout',
527 bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p
, this));
528 Dygraph
.addEvent(div
, 'dblclick',
529 bindEvt('dblClickHandler', 'annotationDblClickHandler', p
, this));
531 this.container
.appendChild(div
);
532 this.annotations
.push(div
);
534 var ctx
= this.elementContext
;
535 ctx
.strokeStyle
= this.colors
[p
.name
];
537 if (!a
.attachAtBottom
) {
538 ctx
.moveTo(p
.canvasx
, p
.canvasy
);
539 ctx
.lineTo(p
.canvasx
, p
.canvasy
- 2 - tick_height
);
541 ctx
.moveTo(p
.canvasx
, this.area
.h
);
542 ctx
.lineTo(p
.canvasx
, this.area
.h
- 2 - tick_height
);
551 * Overrides the CanvasRenderer method to draw error bars
553 DygraphCanvasRenderer
.prototype._renderLineChart
= function() {
554 var isNullOrNaN
= function(x
) {
555 return (x
=== null || isNaN(x
));
558 // TODO(danvk): use this.attr_ for many of these.
559 var context
= this.elementContext
;
560 var fillAlpha
= this.attr_('fillAlpha');
561 var errorBars
= this.attr_("errorBars") || this.attr_("customBars");
562 var fillGraph
= this.attr_("fillGraph");
563 var stackedGraph
= this.attr_("stackedGraph");
564 var stepPlot
= this.attr_("stepPlot");
565 var points
= this.layout
.points
;
566 var pointsLength
= points
.length
;
569 for (var name
in this.layout
.datasets
) {
570 if (this.layout
.datasets
.hasOwnProperty(name
)) {
574 var setCount
= setNames
.length
;
576 // TODO(danvk): Move this mapping into Dygraph and get it out of here.
578 for (var i
= 0; i
< setCount
; i
++) {
579 this.colors
[setNames
[i
]] = this.colorScheme_
[i
% this.colorScheme_
.length
];
584 for (var i
= pointsLength
; i
--;) {
585 var point
= points
[i
];
586 point
.canvasx
= this.area
.w
* point
.x
+ this.area
.x
;
587 point
.canvasy
= this.area
.h
* point
.y
+ this.area
.y
;
594 this.dygraph_
.warn("Can't use fillGraph option with error bars");
597 for (var i
= 0; i
< setCount
; i
++) {
598 var setName
= setNames
[i
];
599 var axis
= this.dygraph_
.axisPropertiesForSeries(setName
);
600 var color
= this.colors
[setName
];
602 // setup graphics context
606 var prevYs
= [-1, -1];
607 var yscale
= axis
.yscale
;
608 // should be same color as the lines but only 15% opaque.
609 var rgb
= new RGBColor(color
);
610 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
612 ctx
.fillStyle
= err_color
;
614 for (var j
= 0; j
< pointsLength
; j
++) {
615 var point
= points
[j
];
616 if (point
.name
== setName
) {
617 if (!Dygraph
.isOK(point
.y
)) {
624 var newYs
= [ point
.y_bottom
, point
.y_top
];
627 var newYs
= [ point
.y_bottom
, point
.y_top
];
629 newYs
[0] = this.area
.h
* newYs
[0] + this.area
.y
;
630 newYs
[1] = this.area
.h
* newYs
[1] + this.area
.y
;
633 ctx
.moveTo(prevX
, newYs
[0]);
635 ctx
.moveTo(prevX
, prevYs
[0]);
637 ctx
.lineTo(point
.canvasx
, newYs
[0]);
638 ctx
.lineTo(point
.canvasx
, newYs
[1]);
640 ctx
.lineTo(prevX
, newYs
[1]);
642 ctx
.lineTo(prevX
, prevYs
[1]);
647 prevX
= point
.canvasx
;
652 } else if (fillGraph
) {
653 var baseline
= [] // for stacked graphs: baseline for filling
655 // process sets in reverse order (needed for stacked graphs)
656 for (var i
= setCount
- 1; i
>= 0; i
--) {
657 var setName
= setNames
[i
];
658 var color
= this.colors
[setName
];
659 var axis
= this.dygraph_
.axisPropertiesForSeries(setName
);
660 var axisY
= 1.0 + axis
.minyval
* axis
.yscale
;
661 if (axisY
< 0.0) axisY
= 0.0;
662 else if (axisY
> 1.0) axisY
= 1.0;
663 axisY
= this.area
.h
* axisY
+ this.area
.y
;
665 // setup graphics context
668 var prevYs
= [-1, -1];
669 var yscale
= axis
.yscale
;
670 // should be same color as the lines but only 15% opaque.
671 var rgb
= new RGBColor(color
);
672 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
674 ctx
.fillStyle
= err_color
;
676 for (var j
= 0; j
< pointsLength
; j
++) {
677 var point
= points
[j
];
678 if (point
.name
== setName
) {
679 if (!Dygraph
.isOK(point
.y
)) {
685 lastY
= baseline
[point
.canvasx
];
686 if (lastY
=== undefined
) lastY
= axisY
;
687 baseline
[point
.canvasx
] = point
.canvasy
;
688 newYs
= [ point
.canvasy
, lastY
];
690 newYs
= [ point
.canvasy
, axisY
];
693 ctx
.moveTo(prevX
, prevYs
[0]);
695 ctx
.lineTo(point
.canvasx
, prevYs
[0]);
697 ctx
.lineTo(point
.canvasx
, newYs
[0]);
699 ctx
.lineTo(point
.canvasx
, newYs
[1]);
700 ctx
.lineTo(prevX
, prevYs
[1]);
704 prevX
= point
.canvasx
;
711 // Drawing the lines.
712 var firstIndexInSet
= 0;
713 var afterLastIndexInSet
= 0;
715 for (var i
= 0; i
< setCount
; i
+= 1) {
716 setLength
= this.layout
.setPointsLengths
[i
];
717 afterLastIndexInSet
+= setLength
;
718 var setName
= setNames
[i
];
719 var color
= this.colors
[setName
];
720 var strokeWidth
= this.dygraph_
.attr_("strokeWidth", setName
);
722 // setup graphics context
724 var pointSize
= this.dygraph_
.attr_("pointSize", setName
);
725 var prevX
= null, prevY
= null;
726 var drawPoints
= this.dygraph_
.attr_("drawPoints", setName
);
727 for (var j
= firstIndexInSet
; j
< afterLastIndexInSet
; j
++) {
728 var point
= points
[j
];
729 if (isNullOrNaN(point
.canvasy
)) {
730 if (stepPlot
&& prevX
!= null) {
731 // Draw a horizontal line to the start of the missing data
733 ctx
.strokeStyle
= color
;
734 ctx
.lineWidth
= this.attr_('strokeWidth');
735 ctx
.moveTo(prevX
, prevY
);
736 ctx
.lineTo(point
.canvasx
, prevY
);
739 // this will make us move to the next point, not draw a line to it.
740 prevX
= prevY
= null;
742 // A point is "isolated" if it is non-null but both the previous
743 // and next points are null.
744 var isIsolated
= (!prevX
&& (j
== points
.length
- 1 ||
745 isNullOrNaN(points
[j
+1].canvasy
)));
746 if (prevX
=== null) {
747 prevX
= point
.canvasx
;
748 prevY
= point
.canvasy
;
750 // Skip over points that will be drawn in the same pixel.
751 if (Math
.round(prevX
) == Math
.round(point
.canvasx
) &&
752 Math
.round(prevY
) == Math
.round(point
.canvasy
)) {
755 // TODO(antrob): skip over points that lie on a line that is already
756 // going to be drawn. There is no need to have more than 2
757 // consecutive points that are collinear.
760 ctx
.strokeStyle
= color
;
761 ctx
.lineWidth
= strokeWidth
;
762 ctx
.moveTo(prevX
, prevY
);
764 ctx
.lineTo(point
.canvasx
, prevY
);
766 prevX
= point
.canvasx
;
767 prevY
= point
.canvasy
;
768 ctx
.lineTo(prevX
, prevY
);
773 if (drawPoints
|| isIsolated
) {
775 ctx
.fillStyle
= color
;
776 ctx
.arc(point
.canvasx
, point
.canvasy
, pointSize
,
777 0, 2 * Math
.PI
, false);
782 firstIndexInSet
= afterLastIndexInSet
;