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 this.minyval
= this.options
.yAxis
[0];
83 this.maxyval
= this.options
.yAxis
[1];
84 this.yrange
= this.maxyval
- this.minyval
;
85 this.yscale
= (this.yrange
!= 0 ? 1/this.yrange
: 1.0);
88 DygraphLayout
.prototype._evaluateLineCharts
= function() {
90 this.points
= new Array();
91 for (var setName
in this.datasets
) {
92 if (!this.datasets
.hasOwnProperty(setName
)) continue;
94 var dataset
= this.datasets
[setName
];
95 for (var j
= 0; j
< dataset
.length
; j
++) {
96 var item
= dataset
[j
];
99 x
: ((parseFloat(item
[0]) - this.minxval
) * this.xscale
),
100 y
: 1.0 - ((parseFloat(item
[1]) - this.minyval
) * this.yscale
),
101 xval
: parseFloat(item
[0]),
102 yval
: parseFloat(item
[1]),
106 // limit the x, y values so they do not overdraw
107 if (point
.y
<= 0.0) {
110 if (point
.y
>= 1.0) {
113 this.points
.push(point
);
118 DygraphLayout
.prototype._evaluateLineTicks
= function() {
119 this.xticks
= new Array();
120 for (var i
= 0; i
< this.options
.xTicks
.length
; i
++) {
121 var tick
= this.options
.xTicks
[i
];
122 var label
= tick
.label
;
123 var pos
= this.xscale
* (tick
.v
- this.minxval
);
124 if ((pos
>= 0.0) && (pos
<= 1.0)) {
125 this.xticks
.push([pos
, label
]);
129 this.yticks
= new Array();
130 for (var i
= 0; i
< this.options
.yTicks
.length
; i
++) {
131 var tick
= this.options
.yTicks
[i
];
132 var label
= tick
.label
;
133 var pos
= 1.0 - (this.yscale
* (tick
.v
- this.minyval
));
134 if ((pos
>= 0.0) && (pos
<= 1.0)) {
135 this.yticks
.push([pos
, label
]);
142 * Behaves the same way as PlotKit.Layout, but also copies the errors
145 DygraphLayout
.prototype.evaluateWithError
= function() {
147 if (!this.options
.errorBars
) return;
149 // Copy over the error terms
150 var i
= 0; // index in this.points
151 for (var setName
in this.datasets
) {
152 if (!this.datasets
.hasOwnProperty(setName
)) continue;
154 var dataset
= this.datasets
[setName
];
155 for (var j
= 0; j
< dataset
.length
; j
++, i
++) {
156 var item
= dataset
[j
];
157 var xv
= parseFloat(item
[0]);
158 var yv
= parseFloat(item
[1]);
160 if (xv
== this.points
[i
].xval
&&
161 yv
== this.points
[i
].yval
) {
162 this.points
[i
].errorMinus
= parseFloat(item
[2]);
163 this.points
[i
].errorPlus
= parseFloat(item
[3]);
169 DygraphLayout
.prototype._evaluateAnnotations
= function() {
170 // Add the annotations to the point to which they belong.
171 // Make a map from (setName, xval) to annotation for quick lookups.
172 var annotations
= {};
173 for (var i
= 0; i
< this.annotations
.length
; i
++) {
174 var a
= this.annotations
[i
];
175 annotations
[a
.xval
+ "," + a
.series
] = a
;
178 this.annotated_points
= [];
179 for (var i
= 0; i
< this.points
.length
; i
++) {
180 var p
= this.points
[i
];
181 var k
= p
.xval
+ "," + p
.name
;
182 if (k
in annotations
) {
183 p
.annotation
= annotations
[k
];
184 this.annotated_points
.push(p
);
190 * Convenience function to remove all the data sets from a graph
192 DygraphLayout
.prototype.removeAllDatasets
= function() {
193 delete this.datasets
;
194 this.datasets
= new Array();
198 * Change the values of various layout options
199 * @param {Object} new_options an associative array of new properties
201 DygraphLayout
.prototype.updateOptions
= function(new_options
) {
202 Dygraph
.update(this.options
, new_options
? new_options
: {});
206 * Return a copy of the point at the indicated index, with its yval unstacked.
207 * @param int index of point in layout_.points
209 DygraphLayout
.prototype.unstackPointAtIndex
= function(idx
) {
210 var point
= this.points
[idx
];
212 // Clone the point since we modify it
213 var unstackedPoint
= {};
214 for (var i
in point
) {
215 unstackedPoint
[i
] = point
[i
];
218 if (!this.attr_("stackedGraph")) {
219 return unstackedPoint
;
222 // The unstacked yval is equal to the current yval minus the yval of the
223 // next point at the same xval.
224 for (var i
= idx
+1; i
< this.points
.length
; i
++) {
225 if (this.points
[i
].xval
== point
.xval
) {
226 unstackedPoint
.yval
-= this.points
[i
].yval
;
231 return unstackedPoint
;
234 // Subclass PlotKit.CanvasRenderer to add:
235 // 1. X/Y grid overlay
236 // 2. Ability to draw error bars (if required)
239 * Sets some PlotKit.CanvasRenderer options
240 * @param {Object} element The canvas to attach to
241 * @param {Layout} layout The DygraphLayout object for this graph.
242 * @param {Object} options Options to pass on to CanvasRenderer
244 DygraphCanvasRenderer
= function(dygraph
, element
, layout
, options
) {
245 // TODO(danvk): remove options, just use dygraph.attr_.
246 this.dygraph_
= dygraph
;
253 "axisLineColor": "black",
254 "axisLineWidth": 0.5,
256 "axisLabelColor": "black",
257 "axisLabelFont": "Arial",
258 "axisLabelFontSize": 9,
259 "axisLabelWidth": 50,
262 "gridLineColor": "rgb(128,128,128)",
264 "underlayCallback": null
266 Dygraph
.update(this.options
, options
);
268 this.layout
= layout
;
269 this.element
= element
;
270 this.container
= this.element
.parentNode
;
272 this.height
= this.element
.height
;
273 this.width
= this.element
.width
;
275 // --- check whether everything is ok before we return
276 if (!this.isIE
&& !(DygraphCanvasRenderer
.isSupported(this.element
)))
277 throw "Canvas is not supported.";
280 this.xlabels
= new Array();
281 this.ylabels
= new Array();
282 this.annotations
= new Array();
285 x
: this.options
.yAxisLabelWidth
+ 2 * this.options
.axisTickSize
,
288 this.area
.w
= this.width
- this.area
.x
- this.options
.rightGap
;
289 this.area
.h
= this.height
- this.options
.axisLabelFontSize
-
290 2 * this.options
.axisTickSize
;
292 this.container
.style
.position
= "relative";
293 this.container
.style
.width
= this.width
+ "px";
296 DygraphCanvasRenderer
.prototype.clear
= function() {
298 // VML takes a while to start up, so we just poll every this.IEDelay
300 if (this.clearDelay
) {
301 this.clearDelay
.cancel();
302 this.clearDelay
= null;
304 var context
= this.element
.getContext("2d");
307 // TODO(danvk): this is broken, since MochiKit.Async is gone.
308 this.clearDelay
= MochiKit
.Async
.wait(this.IEDelay
);
309 this.clearDelay
.addCallback(bind(this.clear
, this));
314 var context
= this.element
.getContext("2d");
315 context
.clearRect(0, 0, this.width
, this.height
);
317 for (var i
= 0; i
< this.xlabels
.length
; i
++) {
318 var el
= this.xlabels
[i
];
319 el
.parentNode
.removeChild(el
);
321 for (var i
= 0; i
< this.ylabels
.length
; i
++) {
322 var el
= this.ylabels
[i
];
323 el
.parentNode
.removeChild(el
);
325 for (var i
= 0; i
< this.annotations
.length
; i
++) {
326 var el
= this.annotations
[i
];
327 el
.parentNode
.removeChild(el
);
329 this.xlabels
= new Array();
330 this.ylabels
= new Array();
331 this.annotations
= new Array();
335 DygraphCanvasRenderer
.isSupported
= function(canvasName
) {
338 if (typeof(canvasName
) == 'undefined' || canvasName
== null)
339 canvas
= document
.createElement("canvas");
342 var context
= canvas
.getContext("2d");
345 var ie
= navigator
.appVersion
.match(/MSIE (\d\.\d)/);
346 var opera
= (navigator
.userAgent
.toLowerCase().indexOf("opera") != -1);
347 if ((!ie
) || (ie
[1] < 6) || (opera
))
355 * Draw an X/Y grid on top of the existing plot
357 DygraphCanvasRenderer
.prototype.render
= function() {
358 // Draw the new X/Y grid
359 var ctx
= this.element
.getContext("2d");
361 if (this.options
.underlayCallback
) {
362 this.options
.underlayCallback(ctx
, this.area
, this.layout
, this.dygraph_
);
365 if (this.options
.drawYGrid
) {
366 var ticks
= this.layout
.yticks
;
368 ctx
.strokeStyle
= this.options
.gridLineColor
;
369 ctx
.lineWidth
= this.options
.axisLineWidth
;
370 for (var i
= 0; i
< ticks
.length
; i
++) {
372 var y
= this.area
.y
+ ticks
[i
][0] * this.area
.h
;
375 ctx
.lineTo(x
+ this.area
.w
, y
);
381 if (this.options
.drawXGrid
) {
382 var ticks
= this.layout
.xticks
;
384 ctx
.strokeStyle
= this.options
.gridLineColor
;
385 ctx
.lineWidth
= this.options
.axisLineWidth
;
386 for (var i
=0; i
<ticks
.length
; i
++) {
387 var x
= this.area
.x
+ ticks
[i
][0] * this.area
.w
;
388 var y
= this.area
.y
+ this.area
.h
;
391 ctx
.lineTo(x
, this.area
.y
);
397 // Do the ordinary rendering, as before
398 this._renderLineChart();
400 this._renderAnnotations();
404 DygraphCanvasRenderer
.prototype._renderAxis
= function() {
405 if (!this.options
.drawXAxis
&& !this.options
.drawYAxis
)
408 var context
= this.element
.getContext("2d");
411 "position": "absolute",
412 "fontSize": this.options
.axisLabelFontSize
+ "px",
414 "color": this.options
.axisLabelColor
,
415 "width": this.options
.axisLabelWidth
+ "px",
418 var makeDiv
= function(txt
) {
419 var div
= document
.createElement("div");
420 for (var name
in labelStyle
) {
421 if (labelStyle
.hasOwnProperty(name
)) {
422 div
.style
[name
] = labelStyle
[name
];
425 div
.appendChild(document
.createTextNode(txt
));
431 context
.strokeStyle
= this.options
.axisLineColor
;
432 context
.lineWidth
= this.options
.axisLineWidth
;
434 if (this.options
.drawYAxis
) {
435 if (this.layout
.yticks
&& this.layout
.yticks
.length
> 0) {
436 for (var i
= 0; i
< this.layout
.yticks
.length
; i
++) {
437 var tick
= this.layout
.yticks
[i
];
438 if (typeof(tick
) == "function") return;
440 var y
= this.area
.y
+ tick
[0] * this.area
.h
;
442 context
.moveTo(x
, y
);
443 context
.lineTo(x
- this.options
.axisTickSize
, y
);
447 var label
= makeDiv(tick
[1]);
448 var top
= (y
- this.options
.axisLabelFontSize
/ 2);
449 if (top
< 0) top
= 0;
451 if (top
+ this.options
.axisLabelFontSize
+ 3 > this.height
) {
452 label
.style
.bottom
= "0px";
454 label
.style
.top
= top
+ "px";
456 label
.style
.left
= "0px";
457 label
.style
.textAlign
= "right";
458 label
.style
.width
= this.options
.yAxisLabelWidth
+ "px";
459 this.container
.appendChild(label
);
460 this.ylabels
.push(label
);
463 // The lowest tick on the y-axis often overlaps with the leftmost
464 // tick on the x-axis. Shift the bottom tick up a little bit to
465 // compensate if necessary.
466 var bottomTick
= this.ylabels
[0];
467 var fontSize
= this.options
.axisLabelFontSize
;
468 var bottom
= parseInt(bottomTick
.style
.top
) + fontSize
;
469 if (bottom
> this.height
- fontSize
) {
470 bottomTick
.style
.top
= (parseInt(bottomTick
.style
.top
) -
471 fontSize
/ 2) + "px";
476 context
.moveTo(this.area
.x
, this.area
.y
);
477 context
.lineTo(this.area
.x
, this.area
.y
+ this.area
.h
);
482 if (this.options
.drawXAxis
) {
483 if (this.layout
.xticks
) {
484 for (var i
= 0; i
< this.layout
.xticks
.length
; i
++) {
485 var tick
= this.layout
.xticks
[i
];
486 if (typeof(dataset
) == "function") return;
488 var x
= this.area
.x
+ tick
[0] * this.area
.w
;
489 var y
= this.area
.y
+ this.area
.h
;
491 context
.moveTo(x
, y
);
492 context
.lineTo(x
, y
+ this.options
.axisTickSize
);
496 var label
= makeDiv(tick
[1]);
497 label
.style
.textAlign
= "center";
498 label
.style
.bottom
= "0px";
500 var left
= (x
- this.options
.axisLabelWidth
/2);
501 if (left
+ this.options
.axisLabelWidth
> this.width
) {
502 left
= this.width
- this.options
.xAxisLabelWidth
;
503 label
.style
.textAlign
= "right";
507 label
.style
.textAlign
= "left";
510 label
.style
.left
= left
+ "px";
511 label
.style
.width
= this.options
.xAxisLabelWidth
+ "px";
512 this.container
.appendChild(label
);
513 this.xlabels
.push(label
);
518 context
.moveTo(this.area
.x
, this.area
.y
+ this.area
.h
);
519 context
.lineTo(this.area
.x
+ this.area
.w
, this.area
.y
+ this.area
.h
);
528 DygraphCanvasRenderer
.prototype._renderAnnotations
= function() {
529 var annotationStyle
= {
530 "position": "absolute",
531 "fontSize": this.options
.axisLabelFontSize
+ "px",
536 var bindEvt
= function(eventName
, classEventName
, p
, self
) {
538 var a
= p
.annotation
;
539 if (a
.hasOwnProperty(eventName
)) {
540 a
[eventName
](a
, p
, self
.dygraph_
, e
);
541 } else if (self
.dygraph_
.attr_(classEventName
)) {
542 self
.dygraph_
.attr_(classEventName
)(a
, p
, self
.dygraph_
,e
);
547 // Get a list of point with annotations.
548 var points
= this.layout
.annotated_points
;
549 for (var i
= 0; i
< points
.length
; i
++) {
551 if (p
.canvasx
< this.area
.x
|| p
.canvasx
> this.area
.x
+ this.area
.w
) {
555 var a
= p
.annotation
;
557 if (a
.hasOwnProperty("tickHeight")) {
558 tick_height
= a
.tickHeight
;
561 var div
= document
.createElement("div");
562 for (var name
in annotationStyle
) {
563 if (annotationStyle
.hasOwnProperty(name
)) {
564 div
.style
[name
] = annotationStyle
[name
];
567 if (!a
.hasOwnProperty('icon')) {
568 div
.className
= "dygraphDefaultAnnotation";
570 if (a
.hasOwnProperty('cssClass')) {
571 div
.className
+= " " + a
.cssClass
;
574 var width
= a
.hasOwnProperty('width') ? a
.width
: 16;
575 var height
= a
.hasOwnProperty('height') ? a
.height
: 16;
576 if (a
.hasOwnProperty('icon')) {
577 var img
= document
.createElement("img");
581 div
.appendChild(img
);
582 } else if (p
.annotation
.hasOwnProperty('shortText')) {
583 div
.appendChild(document
.createTextNode(p
.annotation
.shortText
));
585 div
.style
.left
= (p
.canvasx
- width
/ 2) + "px";
586 if (a
.attachAtBottom
) {
587 div
.style
.top
= (this.area
.h
- height
- tick_height
) + "px";
589 div
.style
.top
= (p
.canvasy
- height
- tick_height
) + "px";
591 div
.style
.width
= width
+ "px";
592 div
.style
.height
= height
+ "px";
593 div
.title
= p
.annotation
.text
;
594 div
.style
.color
= this.colors
[p
.name
];
595 div
.style
.borderColor
= this.colors
[p
.name
];
598 Dygraph
.addEvent(div
, 'click',
599 bindEvt('clickHandler', 'annotationClickHandler', p
, this));
600 Dygraph
.addEvent(div
, 'mouseover',
601 bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p
, this));
602 Dygraph
.addEvent(div
, 'mouseout',
603 bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p
, this));
604 Dygraph
.addEvent(div
, 'dblclick',
605 bindEvt('dblClickHandler', 'annotationDblClickHandler', p
, this));
607 this.container
.appendChild(div
);
608 this.annotations
.push(div
);
610 var ctx
= this.element
.getContext("2d");
611 ctx
.strokeStyle
= this.colors
[p
.name
];
613 if (!a
.attachAtBottom
) {
614 ctx
.moveTo(p
.canvasx
, p
.canvasy
);
615 ctx
.lineTo(p
.canvasx
, p
.canvasy
- 2 - tick_height
);
617 ctx
.moveTo(p
.canvasx
, this.area
.h
);
618 ctx
.lineTo(p
.canvasx
, this.area
.h
- 2 - tick_height
);
627 * Overrides the CanvasRenderer method to draw error bars
629 DygraphCanvasRenderer
.prototype._renderLineChart
= function() {
630 var context
= this.element
.getContext("2d");
631 var colorCount
= this.options
.colorScheme
.length
;
632 var colorScheme
= this.options
.colorScheme
;
633 var fillAlpha
= this.options
.fillAlpha
;
634 var errorBars
= this.layout
.options
.errorBars
;
635 var fillGraph
= this.layout
.options
.fillGraph
;
636 var stackedGraph
= this.layout
.options
.stackedGraph
;
637 var stepPlot
= this.layout
.options
.stepPlot
;
640 for (var name
in this.layout
.datasets
) {
641 if (this.layout
.datasets
.hasOwnProperty(name
)) {
645 var setCount
= setNames
.length
;
648 for (var i
= 0; i
< setCount
; i
++) {
649 this.colors
[setNames
[i
]] = colorScheme
[i
% colorCount
];
654 for (var i
= 0; i
< this.layout
.points
.length
; i
++) {
655 var point
= this.layout
.points
[i
];
656 point
.canvasx
= this.area
.w
* point
.x
+ this.area
.x
;
657 point
.canvasy
= this.area
.h
* point
.y
+ this.area
.y
;
661 var isOK
= function(x
) { return x
&& !isNaN(x
); };
666 this.dygraph_
.warn("Can't use fillGraph option with error bars");
669 for (var i
= 0; i
< setCount
; i
++) {
670 var setName
= setNames
[i
];
671 var color
= this.colors
[setName
];
673 // setup graphics context
677 var prevYs
= [-1, -1];
678 var yscale
= this.layout
.yscale
;
679 // should be same color as the lines but only 15% opaque.
680 var rgb
= new RGBColor(color
);
681 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
683 ctx
.fillStyle
= err_color
;
685 for (var j
= 0; j
< this.layout
.points
.length
; j
++) {
686 var point
= this.layout
.points
[j
];
687 if (point
.name
== setName
) {
688 if (!isOK(point
.y
)) {
695 var newYs
= [ prevY
- point
.errorPlus
* yscale
,
696 prevY
+ point
.errorMinus
* yscale
];
699 var newYs
= [ point
.y
- point
.errorPlus
* yscale
,
700 point
.y
+ point
.errorMinus
* yscale
];
702 newYs
[0] = this.area
.h
* newYs
[0] + this.area
.y
;
703 newYs
[1] = this.area
.h
* newYs
[1] + this.area
.y
;
706 ctx
.moveTo(prevX
, newYs
[0]);
708 ctx
.moveTo(prevX
, prevYs
[0]);
710 ctx
.lineTo(point
.canvasx
, newYs
[0]);
711 ctx
.lineTo(point
.canvasx
, newYs
[1]);
713 ctx
.lineTo(prevX
, newYs
[1]);
715 ctx
.lineTo(prevX
, prevYs
[1]);
720 prevX
= point
.canvasx
;
725 } else if (fillGraph
) {
726 var axisY
= 1.0 + this.layout
.minyval
* this.layout
.yscale
;
727 if (axisY
< 0.0) axisY
= 0.0;
728 else if (axisY
> 1.0) axisY
= 1.0;
729 axisY
= this.area
.h
* axisY
+ this.area
.y
;
731 var baseline
= [] // for stacked graphs: baseline for filling
733 // process sets in reverse order (needed for stacked graphs)
734 for (var i
= setCount
- 1; i
>= 0; i
--) {
735 var setName
= setNames
[i
];
736 var color
= this.colors
[setName
];
738 // setup graphics context
741 var prevYs
= [-1, -1];
742 var yscale
= this.layout
.yscale
;
743 // should be same color as the lines but only 15% opaque.
744 var rgb
= new RGBColor(color
);
745 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
747 ctx
.fillStyle
= err_color
;
749 for (var j
= 0; j
< this.layout
.points
.length
; j
++) {
750 var point
= this.layout
.points
[j
];
751 if (point
.name
== setName
) {
752 if (!isOK(point
.y
)) {
758 lastY
= baseline
[point
.canvasx
];
759 if (lastY
=== undefined
) lastY
= axisY
;
760 baseline
[point
.canvasx
] = point
.canvasy
;
761 newYs
= [ point
.canvasy
, lastY
];
763 newYs
= [ point
.canvasy
, axisY
];
766 ctx
.moveTo(prevX
, prevYs
[0]);
768 ctx
.lineTo(point
.canvasx
, prevYs
[0]);
770 ctx
.lineTo(point
.canvasx
, newYs
[0]);
772 ctx
.lineTo(point
.canvasx
, newYs
[1]);
773 ctx
.lineTo(prevX
, prevYs
[1]);
777 prevX
= point
.canvasx
;
784 for (var i
= 0; i
< setCount
; i
++) {
785 var setName
= setNames
[i
];
786 var color
= this.colors
[setName
];
787 var strokeWidth
= this.dygraph_
.attr_("strokeWidth", setName
);
789 // setup graphics context
791 var point
= this.layout
.points
[0];
792 var pointSize
= this.dygraph_
.attr_("pointSize", setName
);
793 var prevX
= null, prevY
= null;
794 var drawPoints
= this.dygraph_
.attr_("drawPoints", setName
);
795 var points
= this.layout
.points
;
796 for (var j
= 0; j
< points
.length
; j
++) {
797 var point
= points
[j
];
798 if (point
.name
== setName
) {
799 if (!isOK(point
.canvasy
)) {
800 // this will make us move to the next point, not draw a line to it.
801 prevX
= prevY
= null;
803 // A point is "isolated" if it is non-null but both the previous
804 // and next points are null.
805 var isIsolated
= (!prevX
&& (j
== points
.length
- 1 ||
806 !isOK(points
[j
+1].canvasy
)));
809 prevX
= point
.canvasx
;
810 prevY
= point
.canvasy
;
812 // TODO(danvk): figure out why this conditional is necessary.
815 ctx
.strokeStyle
= color
;
816 ctx
.lineWidth
= strokeWidth
;
817 ctx
.moveTo(prevX
, prevY
);
819 ctx
.lineTo(point
.canvasx
, prevY
);
821 prevX
= point
.canvasx
;
822 prevY
= point
.canvasy
;
823 ctx
.lineTo(prevX
, prevY
);
828 if (drawPoints
|| isIsolated
) {
830 ctx
.fillStyle
= color
;
831 ctx
.arc(point
.canvasx
, point
.canvasy
, pointSize
,
832 0, 2 * Math
.PI
, false);