7ce34bf39b4483547f44697922ca339acbe71171
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 var x1
= series
[0][0];
73 if (!this.minxval
|| x1
< this.minxval
) this.minxval
= x1
;
75 var x2
= series
[series
.length
- 1][0];
76 if (!this.maxxval
|| x2
> this.maxxval
) this.maxxval
= x2
;
79 this.xrange
= this.maxxval
- this.minxval
;
80 this.xscale
= (this.xrange
!= 0 ? 1/this.xrange
: 1.0);
82 for (var i
= 0; i
< this.options
.yAxes
.length
; i
++) {
83 var axis
= this.options
.yAxes
[i
];
84 axis
.minyval
= axis
.valueRange
[0];
85 axis
.maxyval
= axis
.valueRange
[1];
86 axis
.yrange
= axis
.maxyval
- axis
.minyval
;
87 axis
.yscale
= (axis
.yrange
!= 0 ? 1.0 / axis
.yrange
: 1.0);
91 DygraphLayout
.prototype._evaluateLineCharts
= function() {
93 this.points
= new Array();
94 for (var setName
in this.datasets
) {
95 if (!this.datasets
.hasOwnProperty(setName
)) continue;
97 var dataset
= this.datasets
[setName
];
98 var axis
= this.options
.yAxes
[this.options
.seriesToAxisMap
[setName
]];
100 for (var j
= 0; j
< dataset
.length
; j
++) {
101 var item
= dataset
[j
];
104 x
: ((parseFloat(item
[0]) - this.minxval
) * this.xscale
),
105 y
: 1.0 - ((parseFloat(item
[1]) - axis
.minyval
) * axis
.yscale
),
106 xval
: parseFloat(item
[0]),
107 yval
: parseFloat(item
[1]),
111 // limit the x, y values so they do not overdraw
112 if (point
.y
<= 0.0) {
115 if (point
.y
>= 1.0) {
118 this.points
.push(point
);
123 DygraphLayout
.prototype._evaluateLineTicks
= function() {
124 this.xticks
= new Array();
125 for (var i
= 0; i
< this.options
.xTicks
.length
; i
++) {
126 var tick
= this.options
.xTicks
[i
];
127 var label
= tick
.label
;
128 var pos
= this.xscale
* (tick
.v
- this.minxval
);
129 if ((pos
>= 0.0) && (pos
<= 1.0)) {
130 this.xticks
.push([pos
, label
]);
134 this.yticks
= new Array();
135 for (var i
= 0; i
< this.options
.yTicks
.length
; i
++) {
136 var tick
= this.options
.yTicks
[i
];
137 var label
= tick
.label
;
138 var pos
= 1.0 - (this.yscale
* (tick
.v
- this.minyval
));
139 if ((pos
>= 0.0) && (pos
<= 1.0)) {
140 this.yticks
.push([pos
, label
]);
147 * Behaves the same way as PlotKit.Layout, but also copies the errors
150 DygraphLayout
.prototype.evaluateWithError
= function() {
152 if (!this.options
.errorBars
) return;
154 // Copy over the error terms
155 var i
= 0; // index in this.points
156 for (var setName
in this.datasets
) {
157 if (!this.datasets
.hasOwnProperty(setName
)) continue;
159 var dataset
= this.datasets
[setName
];
160 for (var j
= 0; j
< dataset
.length
; j
++, i
++) {
161 var item
= dataset
[j
];
162 var xv
= parseFloat(item
[0]);
163 var yv
= parseFloat(item
[1]);
165 if (xv
== this.points
[i
].xval
&&
166 yv
== this.points
[i
].yval
) {
167 this.points
[i
].errorMinus
= parseFloat(item
[2]);
168 this.points
[i
].errorPlus
= parseFloat(item
[3]);
174 DygraphLayout
.prototype._evaluateAnnotations
= function() {
175 // Add the annotations to the point to which they belong.
176 // Make a map from (setName, xval) to annotation for quick lookups.
177 var annotations
= {};
178 for (var i
= 0; i
< this.annotations
.length
; i
++) {
179 var a
= this.annotations
[i
];
180 annotations
[a
.xval
+ "," + a
.series
] = a
;
183 this.annotated_points
= [];
184 for (var i
= 0; i
< this.points
.length
; i
++) {
185 var p
= this.points
[i
];
186 var k
= p
.xval
+ "," + p
.name
;
187 if (k
in annotations
) {
188 p
.annotation
= annotations
[k
];
189 this.annotated_points
.push(p
);
195 * Convenience function to remove all the data sets from a graph
197 DygraphLayout
.prototype.removeAllDatasets
= function() {
198 delete this.datasets
;
199 this.datasets
= new Array();
203 * Change the values of various layout options
204 * @param {Object} new_options an associative array of new properties
206 DygraphLayout
.prototype.updateOptions
= function(new_options
) {
207 Dygraph
.update(this.options
, new_options
? new_options
: {});
210 // Subclass PlotKit.CanvasRenderer to add:
211 // 1. X/Y grid overlay
212 // 2. Ability to draw error bars (if required)
215 * Sets some PlotKit.CanvasRenderer options
216 * @param {Object} element The canvas to attach to
217 * @param {Layout} layout The DygraphLayout object for this graph.
218 * @param {Object} options Options to pass on to CanvasRenderer
220 DygraphCanvasRenderer
= function(dygraph
, element
, layout
, options
) {
221 // TODO(danvk): remove options, just use dygraph.attr_.
222 this.dygraph_
= dygraph
;
229 "axisLineColor": "black",
230 "axisLineWidth": 0.5,
232 "axisLabelColor": "black",
233 "axisLabelFont": "Arial",
234 "axisLabelFontSize": 9,
235 "axisLabelWidth": 50,
238 "gridLineColor": "rgb(128,128,128)",
240 "underlayCallback": null
242 Dygraph
.update(this.options
, options
);
244 this.layout
= layout
;
245 this.element
= element
;
246 this.container
= this.element
.parentNode
;
248 this.height
= this.element
.height
;
249 this.width
= this.element
.width
;
251 // --- check whether everything is ok before we return
252 if (!this.isIE
&& !(DygraphCanvasRenderer
.isSupported(this.element
)))
253 throw "Canvas is not supported.";
256 this.xlabels
= new Array();
257 this.ylabels
= new Array();
258 this.annotations
= new Array();
260 // TODO(danvk): consider all axes in this computation.
262 x
: this.options
.yAxisLabelWidth
+ 2 * this.options
.axisTickSize
,
265 this.area
.w
= this.width
- this.area
.x
- this.options
.rightGap
;
266 this.area
.h
= this.height
- this.options
.axisLabelFontSize
-
267 2 * this.options
.axisTickSize
;
269 this.container
.style
.position
= "relative";
270 this.container
.style
.width
= this.width
+ "px";
273 DygraphCanvasRenderer
.prototype.clear
= function() {
275 // VML takes a while to start up, so we just poll every this.IEDelay
277 if (this.clearDelay
) {
278 this.clearDelay
.cancel();
279 this.clearDelay
= null;
281 var context
= this.element
.getContext("2d");
284 // TODO(danvk): this is broken, since MochiKit.Async is gone.
285 this.clearDelay
= MochiKit
.Async
.wait(this.IEDelay
);
286 this.clearDelay
.addCallback(bind(this.clear
, this));
291 var context
= this.element
.getContext("2d");
292 context
.clearRect(0, 0, this.width
, this.height
);
294 for (var i
= 0; i
< this.xlabels
.length
; i
++) {
295 var el
= this.xlabels
[i
];
296 el
.parentNode
.removeChild(el
);
298 for (var i
= 0; i
< this.ylabels
.length
; i
++) {
299 var el
= this.ylabels
[i
];
300 el
.parentNode
.removeChild(el
);
302 for (var i
= 0; i
< this.annotations
.length
; i
++) {
303 var el
= this.annotations
[i
];
304 el
.parentNode
.removeChild(el
);
306 this.xlabels
= new Array();
307 this.ylabels
= new Array();
308 this.annotations
= new Array();
312 DygraphCanvasRenderer
.isSupported
= function(canvasName
) {
315 if (typeof(canvasName
) == 'undefined' || canvasName
== null)
316 canvas
= document
.createElement("canvas");
319 var context
= canvas
.getContext("2d");
322 var ie
= navigator
.appVersion
.match(/MSIE (\d\.\d)/);
323 var opera
= (navigator
.userAgent
.toLowerCase().indexOf("opera") != -1);
324 if ((!ie
) || (ie
[1] < 6) || (opera
))
332 * Draw an X/Y grid on top of the existing plot
334 DygraphCanvasRenderer
.prototype.render
= function() {
335 // Draw the new X/Y grid
336 var ctx
= this.element
.getContext("2d");
338 if (this.options
.underlayCallback
) {
339 this.options
.underlayCallback(ctx
, this.area
, this.layout
, this.dygraph_
);
342 if (this.options
.drawYGrid
) {
343 var ticks
= this.layout
.yticks
;
345 ctx
.strokeStyle
= this.options
.gridLineColor
;
346 ctx
.lineWidth
= this.options
.axisLineWidth
;
347 for (var i
= 0; i
< ticks
.length
; i
++) {
349 var y
= this.area
.y
+ ticks
[i
][0] * this.area
.h
;
352 ctx
.lineTo(x
+ this.area
.w
, y
);
358 if (this.options
.drawXGrid
) {
359 var ticks
= this.layout
.xticks
;
361 ctx
.strokeStyle
= this.options
.gridLineColor
;
362 ctx
.lineWidth
= this.options
.axisLineWidth
;
363 for (var i
=0; i
<ticks
.length
; i
++) {
364 var x
= this.area
.x
+ ticks
[i
][0] * this.area
.w
;
365 var y
= this.area
.y
+ this.area
.h
;
368 ctx
.lineTo(x
, this.area
.y
);
374 // Do the ordinary rendering, as before
375 this._renderLineChart();
377 this._renderAnnotations();
381 DygraphCanvasRenderer
.prototype._renderAxis
= function() {
382 if (!this.options
.drawXAxis
&& !this.options
.drawYAxis
)
385 var context
= this.element
.getContext("2d");
388 "position": "absolute",
389 "fontSize": this.options
.axisLabelFontSize
+ "px",
391 "color": this.options
.axisLabelColor
,
392 "width": this.options
.axisLabelWidth
+ "px",
395 var makeDiv
= function(txt
) {
396 var div
= document
.createElement("div");
397 for (var name
in labelStyle
) {
398 if (labelStyle
.hasOwnProperty(name
)) {
399 div
.style
[name
] = labelStyle
[name
];
402 div
.appendChild(document
.createTextNode(txt
));
408 context
.strokeStyle
= this.options
.axisLineColor
;
409 context
.lineWidth
= this.options
.axisLineWidth
;
411 if (this.options
.drawYAxis
) {
412 if (this.layout
.yticks
&& this.layout
.yticks
.length
> 0) {
413 for (var i
= 0; i
< this.layout
.yticks
.length
; i
++) {
414 var tick
= this.layout
.yticks
[i
];
415 if (typeof(tick
) == "function") return;
417 var y
= this.area
.y
+ tick
[0] * this.area
.h
;
419 context
.moveTo(x
, y
);
420 context
.lineTo(x
- this.options
.axisTickSize
, y
);
424 var label
= makeDiv(tick
[1]);
425 var top
= (y
- this.options
.axisLabelFontSize
/ 2);
426 if (top
< 0) top
= 0;
428 if (top
+ this.options
.axisLabelFontSize
+ 3 > this.height
) {
429 label
.style
.bottom
= "0px";
431 label
.style
.top
= top
+ "px";
433 label
.style
.left
= "0px";
434 label
.style
.textAlign
= "right";
435 label
.style
.width
= this.options
.yAxisLabelWidth
+ "px";
436 this.container
.appendChild(label
);
437 this.ylabels
.push(label
);
440 // The lowest tick on the y-axis often overlaps with the leftmost
441 // tick on the x-axis. Shift the bottom tick up a little bit to
442 // compensate if necessary.
443 var bottomTick
= this.ylabels
[0];
444 var fontSize
= this.options
.axisLabelFontSize
;
445 var bottom
= parseInt(bottomTick
.style
.top
) + fontSize
;
446 if (bottom
> this.height
- fontSize
) {
447 bottomTick
.style
.top
= (parseInt(bottomTick
.style
.top
) -
448 fontSize
/ 2) + "px";
453 context
.moveTo(this.area
.x
, this.area
.y
);
454 context
.lineTo(this.area
.x
, this.area
.y
+ this.area
.h
);
459 if (this.options
.drawXAxis
) {
460 if (this.layout
.xticks
) {
461 for (var i
= 0; i
< this.layout
.xticks
.length
; i
++) {
462 var tick
= this.layout
.xticks
[i
];
463 if (typeof(dataset
) == "function") return;
465 var x
= this.area
.x
+ tick
[0] * this.area
.w
;
466 var y
= this.area
.y
+ this.area
.h
;
468 context
.moveTo(x
, y
);
469 context
.lineTo(x
, y
+ this.options
.axisTickSize
);
473 var label
= makeDiv(tick
[1]);
474 label
.style
.textAlign
= "center";
475 label
.style
.bottom
= "0px";
477 var left
= (x
- this.options
.axisLabelWidth
/2);
478 if (left
+ this.options
.axisLabelWidth
> this.width
) {
479 left
= this.width
- this.options
.xAxisLabelWidth
;
480 label
.style
.textAlign
= "right";
484 label
.style
.textAlign
= "left";
487 label
.style
.left
= left
+ "px";
488 label
.style
.width
= this.options
.xAxisLabelWidth
+ "px";
489 this.container
.appendChild(label
);
490 this.xlabels
.push(label
);
495 context
.moveTo(this.area
.x
, this.area
.y
+ this.area
.h
);
496 context
.lineTo(this.area
.x
+ this.area
.w
, this.area
.y
+ this.area
.h
);
505 DygraphCanvasRenderer
.prototype._renderAnnotations
= function() {
506 var annotationStyle
= {
507 "position": "absolute",
508 "fontSize": this.options
.axisLabelFontSize
+ "px",
513 var bindEvt
= function(eventName
, classEventName
, p
, self
) {
515 var a
= p
.annotation
;
516 if (a
.hasOwnProperty(eventName
)) {
517 a
[eventName
](a
, p
, self
.dygraph_
, e
);
518 } else if (self
.dygraph_
.attr_(classEventName
)) {
519 self
.dygraph_
.attr_(classEventName
)(a
, p
, self
.dygraph_
,e
);
524 // Get a list of point with annotations.
525 var points
= this.layout
.annotated_points
;
526 for (var i
= 0; i
< points
.length
; i
++) {
528 if (p
.canvasx
< this.area
.x
|| p
.canvasx
> this.area
.x
+ this.area
.w
) {
532 var a
= p
.annotation
;
534 if (a
.hasOwnProperty("tickHeight")) {
535 tick_height
= a
.tickHeight
;
538 var div
= document
.createElement("div");
539 for (var name
in annotationStyle
) {
540 if (annotationStyle
.hasOwnProperty(name
)) {
541 div
.style
[name
] = annotationStyle
[name
];
544 if (!a
.hasOwnProperty('icon')) {
545 div
.className
= "dygraphDefaultAnnotation";
547 if (a
.hasOwnProperty('cssClass')) {
548 div
.className
+= " " + a
.cssClass
;
551 var width
= a
.hasOwnProperty('width') ? a
.width
: 16;
552 var height
= a
.hasOwnProperty('height') ? a
.height
: 16;
553 if (a
.hasOwnProperty('icon')) {
554 var img
= document
.createElement("img");
558 div
.appendChild(img
);
559 } else if (p
.annotation
.hasOwnProperty('shortText')) {
560 div
.appendChild(document
.createTextNode(p
.annotation
.shortText
));
562 div
.style
.left
= (p
.canvasx
- width
/ 2) + "px";
563 if (a
.attachAtBottom
) {
564 div
.style
.top
= (this.area
.h
- height
- tick_height
) + "px";
566 div
.style
.top
= (p
.canvasy
- height
- tick_height
) + "px";
568 div
.style
.width
= width
+ "px";
569 div
.style
.height
= height
+ "px";
570 div
.title
= p
.annotation
.text
;
571 div
.style
.color
= this.colors
[p
.name
];
572 div
.style
.borderColor
= this.colors
[p
.name
];
575 Dygraph
.addEvent(div
, 'click',
576 bindEvt('clickHandler', 'annotationClickHandler', p
, this));
577 Dygraph
.addEvent(div
, 'mouseover',
578 bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p
, this));
579 Dygraph
.addEvent(div
, 'mouseout',
580 bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p
, this));
581 Dygraph
.addEvent(div
, 'dblclick',
582 bindEvt('dblClickHandler', 'annotationDblClickHandler', p
, this));
584 this.container
.appendChild(div
);
585 this.annotations
.push(div
);
587 var ctx
= this.element
.getContext("2d");
588 ctx
.strokeStyle
= this.colors
[p
.name
];
590 if (!a
.attachAtBottom
) {
591 ctx
.moveTo(p
.canvasx
, p
.canvasy
);
592 ctx
.lineTo(p
.canvasx
, p
.canvasy
- 2 - tick_height
);
594 ctx
.moveTo(p
.canvasx
, this.area
.h
);
595 ctx
.lineTo(p
.canvasx
, this.area
.h
- 2 - tick_height
);
604 * Overrides the CanvasRenderer method to draw error bars
606 DygraphCanvasRenderer
.prototype._renderLineChart
= function() {
607 var context
= this.element
.getContext("2d");
608 var colorCount
= this.options
.colorScheme
.length
;
609 var colorScheme
= this.options
.colorScheme
;
610 var fillAlpha
= this.options
.fillAlpha
;
611 var errorBars
= this.layout
.options
.errorBars
;
612 var fillGraph
= this.layout
.options
.fillGraph
;
613 var stackedGraph
= this.layout
.options
.stackedGraph
;
614 var stepPlot
= this.layout
.options
.stepPlot
;
617 for (var name
in this.layout
.datasets
) {
618 if (this.layout
.datasets
.hasOwnProperty(name
)) {
622 var setCount
= setNames
.length
;
625 for (var i
= 0; i
< setCount
; i
++) {
626 this.colors
[setNames
[i
]] = colorScheme
[i
% colorCount
];
631 for (var i
= 0; i
< this.layout
.points
.length
; i
++) {
632 var point
= this.layout
.points
[i
];
633 point
.canvasx
= this.area
.w
* point
.x
+ this.area
.x
;
634 point
.canvasy
= this.area
.h
* point
.y
+ this.area
.y
;
638 var isOK
= function(x
) { return x
&& !isNaN(x
); };
643 this.dygraph_
.warn("Can't use fillGraph option with error bars");
646 for (var i
= 0; i
< setCount
; i
++) {
647 var setName
= setNames
[i
];
648 var axis
= this.layout
.options
.yAxes
[
649 this.layout
.options
.seriesToAxisMap
[setName
]];
650 var color
= this.colors
[setName
];
652 // setup graphics context
656 var prevYs
= [-1, -1];
657 var yscale
= axis
.yscale
;
658 // should be same color as the lines but only 15% opaque.
659 var rgb
= new RGBColor(color
);
660 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
662 ctx
.fillStyle
= err_color
;
664 for (var j
= 0; j
< this.layout
.points
.length
; j
++) {
665 var point
= this.layout
.points
[j
];
666 if (point
.name
== setName
) {
667 if (!isOK(point
.y
)) {
674 var newYs
= [ prevY
- point
.errorPlus
* yscale
,
675 prevY
+ point
.errorMinus
* yscale
];
678 var newYs
= [ point
.y
- point
.errorPlus
* yscale
,
679 point
.y
+ point
.errorMinus
* yscale
];
681 newYs
[0] = this.area
.h
* newYs
[0] + this.area
.y
;
682 newYs
[1] = this.area
.h
* newYs
[1] + this.area
.y
;
685 ctx
.moveTo(prevX
, newYs
[0]);
687 ctx
.moveTo(prevX
, prevYs
[0]);
689 ctx
.lineTo(point
.canvasx
, newYs
[0]);
690 ctx
.lineTo(point
.canvasx
, newYs
[1]);
692 ctx
.lineTo(prevX
, newYs
[1]);
694 ctx
.lineTo(prevX
, prevYs
[1]);
699 prevX
= point
.canvasx
;
704 } else if (fillGraph
) {
705 var baseline
= [] // for stacked graphs: baseline for filling
707 // process sets in reverse order (needed for stacked graphs)
708 for (var i
= setCount
- 1; i
>= 0; i
--) {
709 var setName
= setNames
[i
];
710 var color
= this.colors
[setName
];
711 var axis
= this.layout
.options
.yAxes
[
712 this.layout
.options
.seriesToAxisMap
[setName
]];
713 var axisY
= 1.0 + axis
.minyval
* axis
.yscale
;
714 if (axisY
< 0.0) axisY
= 0.0;
715 else if (axisY
> 1.0) axisY
= 1.0;
716 axisY
= this.area
.h
* axisY
+ this.area
.y
;
718 // setup graphics context
721 var prevYs
= [-1, -1];
722 var yscale
= axis
.yscale
;
723 // should be same color as the lines but only 15% opaque.
724 var rgb
= new RGBColor(color
);
725 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
727 ctx
.fillStyle
= err_color
;
729 for (var j
= 0; j
< this.layout
.points
.length
; j
++) {
730 var point
= this.layout
.points
[j
];
731 if (point
.name
== setName
) {
732 if (!isOK(point
.y
)) {
738 lastY
= baseline
[point
.canvasx
];
739 if (lastY
=== undefined
) lastY
= axisY
;
740 baseline
[point
.canvasx
] = point
.canvasy
;
741 newYs
= [ point
.canvasy
, lastY
];
743 newYs
= [ point
.canvasy
, axisY
];
746 ctx
.moveTo(prevX
, prevYs
[0]);
748 ctx
.lineTo(point
.canvasx
, prevYs
[0]);
750 ctx
.lineTo(point
.canvasx
, newYs
[0]);
752 ctx
.lineTo(point
.canvasx
, newYs
[1]);
753 ctx
.lineTo(prevX
, prevYs
[1]);
757 prevX
= point
.canvasx
;
764 for (var i
= 0; i
< setCount
; i
++) {
765 var setName
= setNames
[i
];
766 var color
= this.colors
[setName
];
767 var strokeWidth
= this.dygraph_
.attr_("strokeWidth", setName
);
769 // setup graphics context
771 var point
= this.layout
.points
[0];
772 var pointSize
= this.dygraph_
.attr_("pointSize", setName
);
773 var prevX
= null, prevY
= null;
774 var drawPoints
= this.dygraph_
.attr_("drawPoints", setName
);
775 var points
= this.layout
.points
;
776 for (var j
= 0; j
< points
.length
; j
++) {
777 var point
= points
[j
];
778 if (point
.name
== setName
) {
779 if (!isOK(point
.canvasy
)) {
780 // this will make us move to the next point, not draw a line to it.
781 prevX
= prevY
= null;
783 // A point is "isolated" if it is non-null but both the previous
784 // and next points are null.
785 var isIsolated
= (!prevX
&& (j
== points
.length
- 1 ||
786 !isOK(points
[j
+1].canvasy
)));
789 prevX
= point
.canvasx
;
790 prevY
= point
.canvasy
;
792 // TODO(danvk): figure out why this conditional is necessary.
795 ctx
.strokeStyle
= color
;
796 ctx
.lineWidth
= strokeWidth
;
797 ctx
.moveTo(prevX
, prevY
);
799 ctx
.lineTo(point
.canvasx
, prevY
);
801 prevX
= point
.canvasx
;
802 prevY
= point
.canvasy
;
803 ctx
.lineTo(prevX
, prevY
);
808 if (drawPoints
|| isIsolated
) {
810 ctx
.fillStyle
= color
;
811 ctx
.arc(point
.canvasx
, point
.canvasy
, pointSize
,
812 0, 2 * Math
.PI
, false);