1 // Copyright 2006 Dan Vanderkam (danvdk@gmail.com)
2 // All Rights Reserved.
5 * @fileoverview Based on PlotKit, but modified to meet the needs of dygraphs.
6 * In particular, support for:
9 * - dygraphs attribute system
13 * Creates a new DygraphLayout object.
14 * @param {Object} options Options for PlotKit.Layout
15 * @return {Object} The DygraphLayout object
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()
25 DygraphLayout
.prototype.attr_
= function(name
) {
26 return this.dygraph_
.attr_(name
);
29 DygraphLayout
.prototype.addDataset
= function(setname
, set_xy
) {
30 this.datasets
[setname
] = set_xy
;
33 DygraphLayout
.prototype.setAnnotations
= function(ann
) {
34 // The Dygraph object's annotations aren't parsed. We parse them here and
36 var parse
= this.attr_('xValueParser');
37 for (var i
= 0; i
< ann
.length
; i
++) {
39 if (!ann
[i
].xval
&& !ann
[i
].x
) {
40 this.dygraph_
.error("Annotations must have an 'x' property");
44 !(ann
[i
].hasOwnProperty('width') &&
45 ann
[i
].hasOwnProperty('height'))) {
46 this.dygraph_
.error("Must set width and height when setting " +
47 "annotation.icon property");
50 Dygraph
.update(a
, ann
[i
]);
51 if (!a
.xval
) a
.xval
= parse(a
.x
);
52 this.annotations
.push(a
);
56 DygraphLayout
.prototype.evaluate
= function() {
57 this._evaluateLimits();
58 this._evaluateLineCharts();
59 this._evaluateLineTicks();
60 this._evaluateAnnotations();
63 DygraphLayout
.prototype._evaluateLimits
= function() {
64 this.minxval
= this.maxxval
= null;
65 if (this.options
.dateWindow
) {
66 this.minxval
= this.options
.dateWindow
[0];
67 this.maxxval
= this.options
.dateWindow
[1];
69 for (var name
in this.datasets
) {
70 if (!this.datasets
.hasOwnProperty(name
)) continue;
71 var series
= this.datasets
[name
];
72 if (series
.length
> 1) {
73 var x1
= series
[0][0];
74 if (!this.minxval
|| x1
< this.minxval
) this.minxval
= x1
;
76 var x2
= series
[series
.length
- 1][0];
77 if (!this.maxxval
|| x2
> this.maxxval
) this.maxxval
= x2
;
81 this.xrange
= this.maxxval
- this.minxval
;
82 this.xscale
= (this.xrange
!= 0 ? 1/this.xrange
: 1.0);
84 this.minyval
= this.options
.yAxis
[0];
85 this.maxyval
= this.options
.yAxis
[1];
86 this.yrange
= this.maxyval
- this.minyval
;
87 this.yscale
= (this.yrange
!= 0 ? 1/this.yrange
: 1.0);
90 DygraphLayout
.prototype._evaluateLineCharts
= function() {
92 this.points
= new Array();
93 for (var setName
in this.datasets
) {
94 if (!this.datasets
.hasOwnProperty(setName
)) continue;
96 var dataset
= this.datasets
[setName
];
97 for (var j
= 0; j
< dataset
.length
; j
++) {
98 var item
= dataset
[j
];
101 x
: ((parseFloat(item
[0]) - this.minxval
) * this.xscale
),
102 y
: 1.0 - ((parseFloat(item
[1]) - this.minyval
) * this.yscale
),
103 xval
: parseFloat(item
[0]),
104 yval
: parseFloat(item
[1]),
108 // limit the x, y values so they do not overdraw
109 if (point
.y
<= 0.0) {
112 if (point
.y
>= 1.0) {
115 this.points
.push(point
);
120 DygraphLayout
.prototype._evaluateLineTicks
= function() {
121 this.xticks
= new Array();
122 for (var i
= 0; i
< this.options
.xTicks
.length
; i
++) {
123 var tick
= this.options
.xTicks
[i
];
124 var label
= tick
.label
;
125 var pos
= this.xscale
* (tick
.v
- this.minxval
);
126 if ((pos
>= 0.0) && (pos
<= 1.0)) {
127 this.xticks
.push([pos
, label
]);
131 this.yticks
= new Array();
132 for (var i
= 0; i
< this.options
.yTicks
.length
; i
++) {
133 var tick
= this.options
.yTicks
[i
];
134 var label
= tick
.label
;
135 var pos
= 1.0 - (this.yscale
* (tick
.v
- this.minyval
));
136 if ((pos
>= 0.0) && (pos
<= 1.0)) {
137 this.yticks
.push([pos
, label
]);
144 * Behaves the same way as PlotKit.Layout, but also copies the errors
147 DygraphLayout
.prototype.evaluateWithError
= function() {
149 if (!this.options
.errorBars
) return;
151 // Copy over the error terms
152 var i
= 0; // index in this.points
153 for (var setName
in this.datasets
) {
154 if (!this.datasets
.hasOwnProperty(setName
)) continue;
156 var dataset
= this.datasets
[setName
];
157 for (var j
= 0; j
< dataset
.length
; j
++, i
++) {
158 var item
= dataset
[j
];
159 var xv
= parseFloat(item
[0]);
160 var yv
= parseFloat(item
[1]);
162 if (xv
== this.points
[i
].xval
&&
163 yv
== this.points
[i
].yval
) {
164 this.points
[i
].errorMinus
= parseFloat(item
[2]);
165 this.points
[i
].errorPlus
= parseFloat(item
[3]);
171 DygraphLayout
.prototype._evaluateAnnotations
= function() {
172 // Add the annotations to the point to which they belong.
173 // Make a map from (setName, xval) to annotation for quick lookups.
174 var annotations
= {};
175 for (var i
= 0; i
< this.annotations
.length
; i
++) {
176 var a
= this.annotations
[i
];
177 annotations
[a
.xval
+ "," + a
.series
] = a
;
180 this.annotated_points
= [];
181 for (var i
= 0; i
< this.points
.length
; i
++) {
182 var p
= this.points
[i
];
183 var k
= p
.xval
+ "," + p
.name
;
184 if (k
in annotations
) {
185 p
.annotation
= annotations
[k
];
186 this.annotated_points
.push(p
);
192 * Convenience function to remove all the data sets from a graph
194 DygraphLayout
.prototype.removeAllDatasets
= function() {
195 delete this.datasets
;
196 this.datasets
= new Array();
200 * Change the values of various layout options
201 * @param {Object} new_options an associative array of new properties
203 DygraphLayout
.prototype.updateOptions
= function(new_options
) {
204 Dygraph
.update(this.options
, new_options
? new_options
: {});
208 * Return a copy of the point at the indicated index, with its yval unstacked.
209 * @param int index of point in layout_.points
211 DygraphLayout
.prototype.unstackPointAtIndex
= function(idx
) {
212 var point
= this.points
[idx
];
214 // Clone the point since we modify it
215 var unstackedPoint
= {};
216 for (var i
in point
) {
217 unstackedPoint
[i
] = point
[i
];
220 if (!this.attr_("stackedGraph")) {
221 return unstackedPoint
;
224 // The unstacked yval is equal to the current yval minus the yval of the
225 // next point at the same xval.
226 for (var i
= idx
+1; i
< this.points
.length
; i
++) {
227 if (this.points
[i
].xval
== point
.xval
) {
228 unstackedPoint
.yval
-= this.points
[i
].yval
;
233 return unstackedPoint
;
236 // Subclass PlotKit.CanvasRenderer to add:
237 // 1. X/Y grid overlay
238 // 2. Ability to draw error bars (if required)
241 * Sets some PlotKit.CanvasRenderer options
242 * @param {Object} element The canvas to attach to
243 * @param {Layout} layout The DygraphLayout object for this graph.
244 * @param {Object} options Options to pass on to CanvasRenderer
246 DygraphCanvasRenderer
= function(dygraph
, element
, layout
, options
) {
247 // TODO(danvk): remove options, just use dygraph.attr_.
248 this.dygraph_
= dygraph
;
255 "axisLineColor": "black",
256 "axisLineWidth": 0.5,
258 "axisLabelColor": "black",
259 "axisLabelFont": "Arial",
260 "axisLabelFontSize": 9,
261 "axisLabelWidth": 50,
264 "gridLineColor": "rgb(128,128,128)",
266 "underlayCallback": null
268 Dygraph
.update(this.options
, options
);
270 this.layout
= layout
;
271 this.element
= element
;
272 this.container
= this.element
.parentNode
;
274 this.height
= this.element
.height
;
275 this.width
= this.element
.width
;
277 // --- check whether everything is ok before we return
278 if (!this.isIE
&& !(DygraphCanvasRenderer
.isSupported(this.element
)))
279 throw "Canvas is not supported.";
282 this.xlabels
= new Array();
283 this.ylabels
= new Array();
284 this.annotations
= new Array();
287 x
: this.options
.yAxisLabelWidth
+ 2 * this.options
.axisTickSize
,
290 this.area
.w
= this.width
- this.area
.x
- this.options
.rightGap
;
291 this.area
.h
= this.height
- this.options
.axisLabelFontSize
-
292 2 * this.options
.axisTickSize
;
294 this.container
.style
.position
= "relative";
295 this.container
.style
.width
= this.width
+ "px";
298 DygraphCanvasRenderer
.prototype.clear
= function() {
300 // VML takes a while to start up, so we just poll every this.IEDelay
302 if (this.clearDelay
) {
303 this.clearDelay
.cancel();
304 this.clearDelay
= null;
306 var context
= this.element
.getContext("2d");
309 // TODO(danvk): this is broken, since MochiKit.Async is gone.
310 this.clearDelay
= MochiKit
.Async
.wait(this.IEDelay
);
311 this.clearDelay
.addCallback(bind(this.clear
, this));
316 var context
= this.element
.getContext("2d");
317 context
.clearRect(0, 0, this.width
, this.height
);
319 for (var i
= 0; i
< this.xlabels
.length
; i
++) {
320 var el
= this.xlabels
[i
];
321 el
.parentNode
.removeChild(el
);
323 for (var i
= 0; i
< this.ylabels
.length
; i
++) {
324 var el
= this.ylabels
[i
];
325 el
.parentNode
.removeChild(el
);
327 for (var i
= 0; i
< this.annotations
.length
; i
++) {
328 var el
= this.annotations
[i
];
329 el
.parentNode
.removeChild(el
);
331 this.xlabels
= new Array();
332 this.ylabels
= new Array();
333 this.annotations
= new Array();
337 DygraphCanvasRenderer
.isSupported
= function(canvasName
) {
340 if (typeof(canvasName
) == 'undefined' || canvasName
== null)
341 canvas
= document
.createElement("canvas");
344 var context
= canvas
.getContext("2d");
347 var ie
= navigator
.appVersion
.match(/MSIE (\d\.\d)/);
348 var opera
= (navigator
.userAgent
.toLowerCase().indexOf("opera") != -1);
349 if ((!ie
) || (ie
[1] < 6) || (opera
))
357 * Draw an X/Y grid on top of the existing plot
359 DygraphCanvasRenderer
.prototype.render
= function() {
360 // Draw the new X/Y grid
361 var ctx
= this.element
.getContext("2d");
363 if (this.options
.underlayCallback
) {
364 this.options
.underlayCallback(ctx
, this.area
, this.layout
, this.dygraph_
);
367 if (this.options
.drawYGrid
) {
368 var ticks
= this.layout
.yticks
;
370 ctx
.strokeStyle
= this.options
.gridLineColor
;
371 ctx
.lineWidth
= this.options
.axisLineWidth
;
372 for (var i
= 0; i
< ticks
.length
; i
++) {
374 var y
= this.area
.y
+ ticks
[i
][0] * this.area
.h
;
377 ctx
.lineTo(x
+ this.area
.w
, y
);
383 if (this.options
.drawXGrid
) {
384 var ticks
= this.layout
.xticks
;
386 ctx
.strokeStyle
= this.options
.gridLineColor
;
387 ctx
.lineWidth
= this.options
.axisLineWidth
;
388 for (var i
=0; i
<ticks
.length
; i
++) {
389 var x
= this.area
.x
+ ticks
[i
][0] * this.area
.w
;
390 var y
= this.area
.y
+ this.area
.h
;
393 ctx
.lineTo(x
, this.area
.y
);
399 // Do the ordinary rendering, as before
400 this._renderLineChart();
402 this._renderAnnotations();
406 DygraphCanvasRenderer
.prototype._renderAxis
= function() {
407 if (!this.options
.drawXAxis
&& !this.options
.drawYAxis
)
410 var context
= this.element
.getContext("2d");
413 "position": "absolute",
414 "fontSize": this.options
.axisLabelFontSize
+ "px",
416 "color": this.options
.axisLabelColor
,
417 "width": this.options
.axisLabelWidth
+ "px",
420 var makeDiv
= function(txt
) {
421 var div
= document
.createElement("div");
422 for (var name
in labelStyle
) {
423 if (labelStyle
.hasOwnProperty(name
)) {
424 div
.style
[name
] = labelStyle
[name
];
427 div
.appendChild(document
.createTextNode(txt
));
433 context
.strokeStyle
= this.options
.axisLineColor
;
434 context
.lineWidth
= this.options
.axisLineWidth
;
436 if (this.options
.drawYAxis
) {
437 if (this.layout
.yticks
&& this.layout
.yticks
.length
> 0) {
438 for (var i
= 0; i
< this.layout
.yticks
.length
; i
++) {
439 var tick
= this.layout
.yticks
[i
];
440 if (typeof(tick
) == "function") return;
442 var y
= this.area
.y
+ tick
[0] * this.area
.h
;
444 context
.moveTo(x
, y
);
445 context
.lineTo(x
- this.options
.axisTickSize
, y
);
449 var label
= makeDiv(tick
[1]);
450 var top
= (y
- this.options
.axisLabelFontSize
/ 2);
451 if (top
< 0) top
= 0;
453 if (top
+ this.options
.axisLabelFontSize
+ 3 > this.height
) {
454 label
.style
.bottom
= "0px";
456 label
.style
.top
= top
+ "px";
458 label
.style
.left
= "0px";
459 label
.style
.textAlign
= "right";
460 label
.style
.width
= this.options
.yAxisLabelWidth
+ "px";
461 this.container
.appendChild(label
);
462 this.ylabels
.push(label
);
465 // The lowest tick on the y-axis often overlaps with the leftmost
466 // tick on the x-axis. Shift the bottom tick up a little bit to
467 // compensate if necessary.
468 var bottomTick
= this.ylabels
[0];
469 var fontSize
= this.options
.axisLabelFontSize
;
470 var bottom
= parseInt(bottomTick
.style
.top
) + fontSize
;
471 if (bottom
> this.height
- fontSize
) {
472 bottomTick
.style
.top
= (parseInt(bottomTick
.style
.top
) -
473 fontSize
/ 2) + "px";
478 context
.moveTo(this.area
.x
, this.area
.y
);
479 context
.lineTo(this.area
.x
, this.area
.y
+ this.area
.h
);
484 if (this.options
.drawXAxis
) {
485 if (this.layout
.xticks
) {
486 for (var i
= 0; i
< this.layout
.xticks
.length
; i
++) {
487 var tick
= this.layout
.xticks
[i
];
488 if (typeof(dataset
) == "function") return;
490 var x
= this.area
.x
+ tick
[0] * this.area
.w
;
491 var y
= this.area
.y
+ this.area
.h
;
493 context
.moveTo(x
, y
);
494 context
.lineTo(x
, y
+ this.options
.axisTickSize
);
498 var label
= makeDiv(tick
[1]);
499 label
.style
.textAlign
= "center";
500 label
.style
.bottom
= "0px";
502 var left
= (x
- this.options
.axisLabelWidth
/2);
503 if (left
+ this.options
.axisLabelWidth
> this.width
) {
504 left
= this.width
- this.options
.xAxisLabelWidth
;
505 label
.style
.textAlign
= "right";
509 label
.style
.textAlign
= "left";
512 label
.style
.left
= left
+ "px";
513 label
.style
.width
= this.options
.xAxisLabelWidth
+ "px";
514 this.container
.appendChild(label
);
515 this.xlabels
.push(label
);
520 context
.moveTo(this.area
.x
, this.area
.y
+ this.area
.h
);
521 context
.lineTo(this.area
.x
+ this.area
.w
, this.area
.y
+ this.area
.h
);
530 DygraphCanvasRenderer
.prototype._renderAnnotations
= function() {
531 var annotationStyle
= {
532 "position": "absolute",
533 "fontSize": this.options
.axisLabelFontSize
+ "px",
538 var bindEvt
= function(eventName
, classEventName
, p
, self
) {
540 var a
= p
.annotation
;
541 if (a
.hasOwnProperty(eventName
)) {
542 a
[eventName
](a
, p
, self
.dygraph_
, e
);
543 } else if (self
.dygraph_
.attr_(classEventName
)) {
544 self
.dygraph_
.attr_(classEventName
)(a
, p
, self
.dygraph_
,e
);
549 // Get a list of point with annotations.
550 var points
= this.layout
.annotated_points
;
551 for (var i
= 0; i
< points
.length
; i
++) {
553 if (p
.canvasx
< this.area
.x
|| p
.canvasx
> this.area
.x
+ this.area
.w
) {
557 var a
= p
.annotation
;
559 if (a
.hasOwnProperty("tickHeight")) {
560 tick_height
= a
.tickHeight
;
563 var div
= document
.createElement("div");
564 for (var name
in annotationStyle
) {
565 if (annotationStyle
.hasOwnProperty(name
)) {
566 div
.style
[name
] = annotationStyle
[name
];
569 if (!a
.hasOwnProperty('icon')) {
570 div
.className
= "dygraphDefaultAnnotation";
572 if (a
.hasOwnProperty('cssClass')) {
573 div
.className
+= " " + a
.cssClass
;
576 var width
= a
.hasOwnProperty('width') ? a
.width
: 16;
577 var height
= a
.hasOwnProperty('height') ? a
.height
: 16;
578 if (a
.hasOwnProperty('icon')) {
579 var img
= document
.createElement("img");
583 div
.appendChild(img
);
584 } else if (p
.annotation
.hasOwnProperty('shortText')) {
585 div
.appendChild(document
.createTextNode(p
.annotation
.shortText
));
587 div
.style
.left
= (p
.canvasx
- width
/ 2) + "px";
588 if (a
.attachAtBottom
) {
589 div
.style
.top
= (this.area
.h
- height
- tick_height
) + "px";
591 div
.style
.top
= (p
.canvasy
- height
- tick_height
) + "px";
593 div
.style
.width
= width
+ "px";
594 div
.style
.height
= height
+ "px";
595 div
.title
= p
.annotation
.text
;
596 div
.style
.color
= this.colors
[p
.name
];
597 div
.style
.borderColor
= this.colors
[p
.name
];
600 Dygraph
.addEvent(div
, 'click',
601 bindEvt('clickHandler', 'annotationClickHandler', p
, this));
602 Dygraph
.addEvent(div
, 'mouseover',
603 bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p
, this));
604 Dygraph
.addEvent(div
, 'mouseout',
605 bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p
, this));
606 Dygraph
.addEvent(div
, 'dblclick',
607 bindEvt('dblClickHandler', 'annotationDblClickHandler', p
, this));
609 this.container
.appendChild(div
);
610 this.annotations
.push(div
);
612 var ctx
= this.element
.getContext("2d");
613 ctx
.strokeStyle
= this.colors
[p
.name
];
615 if (!a
.attachAtBottom
) {
616 ctx
.moveTo(p
.canvasx
, p
.canvasy
);
617 ctx
.lineTo(p
.canvasx
, p
.canvasy
- 2 - tick_height
);
619 ctx
.moveTo(p
.canvasx
, this.area
.h
);
620 ctx
.lineTo(p
.canvasx
, this.area
.h
- 2 - tick_height
);
629 * Overrides the CanvasRenderer method to draw error bars
631 DygraphCanvasRenderer
.prototype._renderLineChart
= function() {
632 var context
= this.element
.getContext("2d");
633 var colorCount
= this.options
.colorScheme
.length
;
634 var colorScheme
= this.options
.colorScheme
;
635 var fillAlpha
= this.options
.fillAlpha
;
636 var errorBars
= this.layout
.options
.errorBars
;
637 var fillGraph
= this.layout
.options
.fillGraph
;
638 var stackedGraph
= this.layout
.options
.stackedGraph
;
639 var stepPlot
= this.layout
.options
.stepPlot
;
642 for (var name
in this.layout
.datasets
) {
643 if (this.layout
.datasets
.hasOwnProperty(name
)) {
647 var setCount
= setNames
.length
;
650 for (var i
= 0; i
< setCount
; i
++) {
651 this.colors
[setNames
[i
]] = colorScheme
[i
% colorCount
];
656 for (var i
= 0; i
< this.layout
.points
.length
; i
++) {
657 var point
= this.layout
.points
[i
];
658 point
.canvasx
= this.area
.w
* point
.x
+ this.area
.x
;
659 point
.canvasy
= this.area
.h
* point
.y
+ this.area
.y
;
663 var isOK
= function(x
) { return x
&& !isNaN(x
); };
668 this.dygraph_
.warn("Can't use fillGraph option with error bars");
671 for (var i
= 0; i
< setCount
; i
++) {
672 var setName
= setNames
[i
];
673 var color
= this.colors
[setName
];
675 // setup graphics context
679 var prevYs
= [-1, -1];
680 var yscale
= this.layout
.yscale
;
681 // should be same color as the lines but only 15% opaque.
682 var rgb
= new RGBColor(color
);
683 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
685 ctx
.fillStyle
= err_color
;
687 for (var j
= 0; j
< this.layout
.points
.length
; j
++) {
688 var point
= this.layout
.points
[j
];
689 if (point
.name
== setName
) {
690 if (!isOK(point
.y
)) {
697 var newYs
= [ prevY
- point
.errorPlus
* yscale
,
698 prevY
+ point
.errorMinus
* yscale
];
701 var newYs
= [ point
.y
- point
.errorPlus
* yscale
,
702 point
.y
+ point
.errorMinus
* yscale
];
704 newYs
[0] = this.area
.h
* newYs
[0] + this.area
.y
;
705 newYs
[1] = this.area
.h
* newYs
[1] + this.area
.y
;
708 ctx
.moveTo(prevX
, newYs
[0]);
710 ctx
.moveTo(prevX
, prevYs
[0]);
712 ctx
.lineTo(point
.canvasx
, newYs
[0]);
713 ctx
.lineTo(point
.canvasx
, newYs
[1]);
715 ctx
.lineTo(prevX
, newYs
[1]);
717 ctx
.lineTo(prevX
, prevYs
[1]);
722 prevX
= point
.canvasx
;
727 } else if (fillGraph
) {
728 var axisY
= 1.0 + this.layout
.minyval
* this.layout
.yscale
;
729 if (axisY
< 0.0) axisY
= 0.0;
730 else if (axisY
> 1.0) axisY
= 1.0;
731 axisY
= this.area
.h
* axisY
+ this.area
.y
;
733 var baseline
= [] // for stacked graphs: baseline for filling
735 // process sets in reverse order (needed for stacked graphs)
736 for (var i
= setCount
- 1; i
>= 0; i
--) {
737 var setName
= setNames
[i
];
738 var color
= this.colors
[setName
];
740 // setup graphics context
743 var prevYs
= [-1, -1];
744 var yscale
= this.layout
.yscale
;
745 // should be same color as the lines but only 15% opaque.
746 var rgb
= new RGBColor(color
);
747 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
749 ctx
.fillStyle
= err_color
;
751 for (var j
= 0; j
< this.layout
.points
.length
; j
++) {
752 var point
= this.layout
.points
[j
];
753 if (point
.name
== setName
) {
754 if (!isOK(point
.y
)) {
760 lastY
= baseline
[point
.canvasx
];
761 if (lastY
=== undefined
) lastY
= axisY
;
762 baseline
[point
.canvasx
] = point
.canvasy
;
763 newYs
= [ point
.canvasy
, lastY
];
765 newYs
= [ point
.canvasy
, axisY
];
768 ctx
.moveTo(prevX
, prevYs
[0]);
770 ctx
.lineTo(point
.canvasx
, prevYs
[0]);
772 ctx
.lineTo(point
.canvasx
, newYs
[0]);
774 ctx
.lineTo(point
.canvasx
, newYs
[1]);
775 ctx
.lineTo(prevX
, prevYs
[1]);
779 prevX
= point
.canvasx
;
786 for (var i
= 0; i
< setCount
; i
++) {
787 var setName
= setNames
[i
];
788 var color
= this.colors
[setName
];
789 var strokeWidth
= this.dygraph_
.attr_("strokeWidth", setName
);
791 // setup graphics context
793 var point
= this.layout
.points
[0];
794 var pointSize
= this.dygraph_
.attr_("pointSize", setName
);
795 var prevX
= null, prevY
= null;
796 var drawPoints
= this.dygraph_
.attr_("drawPoints", setName
);
797 var points
= this.layout
.points
;
798 for (var j
= 0; j
< points
.length
; j
++) {
799 var point
= points
[j
];
800 if (point
.name
== setName
) {
801 if (!isOK(point
.canvasy
)) {
802 if (stepPlot
&& prevX
!= null) {
803 // Draw a horizontal line to the start of the missing data
805 ctx
.strokeStyle
= color
;
806 ctx
.lineWidth
= this.options
.strokeWidth
;
807 ctx
.moveTo(prevX
, prevY
);
808 ctx
.lineTo(point
.canvasx
, prevY
);
811 // this will make us move to the next point, not draw a line to it.
812 prevX
= prevY
= null;
814 // A point is "isolated" if it is non-null but both the previous
815 // and next points are null.
816 var isIsolated
= (!prevX
&& (j
== points
.length
- 1 ||
817 !isOK(points
[j
+1].canvasy
)));
820 prevX
= point
.canvasx
;
821 prevY
= point
.canvasy
;
823 // TODO(danvk): figure out why this conditional is necessary.
826 ctx
.strokeStyle
= color
;
827 ctx
.lineWidth
= strokeWidth
;
828 ctx
.moveTo(prevX
, prevY
);
830 ctx
.lineTo(point
.canvasx
, prevY
);
832 prevX
= point
.canvasx
;
833 prevY
= point
.canvasy
;
834 ctx
.lineTo(prevX
, prevY
);
839 if (drawPoints
|| isIsolated
) {
841 ctx
.fillStyle
= color
;
842 ctx
.arc(point
.canvasx
, point
.canvasy
, pointSize
,
843 0, 2 * Math
.PI
, false);