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 this.annotations
= [];
37 var parse
= this.attr_('xValueParser');
38 for (var i
= 0; i
< ann
.length
; i
++) {
40 if (!ann
[i
].xval
&& !ann
[i
].x
) {
41 this.dygraph_
.error("Annotations must have an 'x' property");
45 !(ann
[i
].hasOwnProperty('width') &&
46 ann
[i
].hasOwnProperty('height'))) {
47 this.dygraph_
.error("Must set width and height when setting " +
48 "annotation.icon property");
51 Dygraph
.update(a
, ann
[i
]);
52 if (!a
.xval
) a
.xval
= parse(a
.x
);
53 this.annotations
.push(a
);
57 DygraphLayout
.prototype.evaluate
= function() {
58 this._evaluateLimits();
59 this._evaluateLineCharts();
60 this._evaluateLineTicks();
61 this._evaluateAnnotations();
64 DygraphLayout
.prototype._evaluateLimits
= function() {
65 this.minxval
= this.maxxval
= null;
66 if (this.options
.dateWindow
) {
67 this.minxval
= this.options
.dateWindow
[0];
68 this.maxxval
= this.options
.dateWindow
[1];
70 for (var name
in this.datasets
) {
71 if (!this.datasets
.hasOwnProperty(name
)) continue;
72 var series
= this.datasets
[name
];
73 if (series
.length
> 1) {
74 var x1
= series
[0][0];
75 if (!this.minxval
|| x1
< this.minxval
) this.minxval
= x1
;
77 var x2
= series
[series
.length
- 1][0];
78 if (!this.maxxval
|| x2
> this.maxxval
) this.maxxval
= x2
;
82 this.xrange
= this.maxxval
- this.minxval
;
83 this.xscale
= (this.xrange
!= 0 ? 1/this.xrange
: 1.0);
85 this.minyval
= this.options
.yAxis
[0];
86 this.maxyval
= this.options
.yAxis
[1];
87 this.yrange
= this.maxyval
- this.minyval
;
88 this.yscale
= (this.yrange
!= 0 ? 1/this.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 for (var j
= 0; j
< dataset
.length
; j
++) {
99 var item
= dataset
[j
];
102 x
: ((parseFloat(item
[0]) - this.minxval
) * this.xscale
),
103 y
: 1.0 - ((parseFloat(item
[1]) - this.minyval
) * this.yscale
),
104 xval
: parseFloat(item
[0]),
105 yval
: parseFloat(item
[1]),
109 // limit the x, y values so they do not overdraw
110 if (point
.y
<= 0.0) {
113 if (point
.y
>= 1.0) {
116 this.points
.push(point
);
121 DygraphLayout
.prototype._evaluateLineTicks
= function() {
122 this.xticks
= new Array();
123 for (var i
= 0; i
< this.options
.xTicks
.length
; i
++) {
124 var tick
= this.options
.xTicks
[i
];
125 var label
= tick
.label
;
126 var pos
= this.xscale
* (tick
.v
- this.minxval
);
127 if ((pos
>= 0.0) && (pos
<= 1.0)) {
128 this.xticks
.push([pos
, label
]);
132 this.yticks
= new Array();
133 for (var i
= 0; i
< this.options
.yTicks
.length
; i
++) {
134 var tick
= this.options
.yTicks
[i
];
135 var label
= tick
.label
;
136 var pos
= 1.0 - (this.yscale
* (tick
.v
- this.minyval
));
137 if ((pos
>= 0.0) && (pos
<= 1.0)) {
138 this.yticks
.push([pos
, label
]);
145 * Behaves the same way as PlotKit.Layout, but also copies the errors
148 DygraphLayout
.prototype.evaluateWithError
= function() {
150 if (!this.options
.errorBars
) return;
152 // Copy over the error terms
153 var i
= 0; // index in this.points
154 for (var setName
in this.datasets
) {
155 if (!this.datasets
.hasOwnProperty(setName
)) continue;
157 var dataset
= this.datasets
[setName
];
158 for (var j
= 0; j
< dataset
.length
; j
++, i
++) {
159 var item
= dataset
[j
];
160 var xv
= parseFloat(item
[0]);
161 var yv
= parseFloat(item
[1]);
163 if (xv
== this.points
[i
].xval
&&
164 yv
== this.points
[i
].yval
) {
165 this.points
[i
].errorMinus
= parseFloat(item
[2]);
166 this.points
[i
].errorPlus
= parseFloat(item
[3]);
172 DygraphLayout
.prototype._evaluateAnnotations
= function() {
173 // Add the annotations to the point to which they belong.
174 // Make a map from (setName, xval) to annotation for quick lookups.
175 var annotations
= {};
176 for (var i
= 0; i
< this.annotations
.length
; i
++) {
177 var a
= this.annotations
[i
];
178 annotations
[a
.xval
+ "," + a
.series
] = a
;
181 this.annotated_points
= [];
182 for (var i
= 0; i
< this.points
.length
; i
++) {
183 var p
= this.points
[i
];
184 var k
= p
.xval
+ "," + p
.name
;
185 if (k
in annotations
) {
186 p
.annotation
= annotations
[k
];
187 this.annotated_points
.push(p
);
193 * Convenience function to remove all the data sets from a graph
195 DygraphLayout
.prototype.removeAllDatasets
= function() {
196 delete this.datasets
;
197 this.datasets
= new Array();
201 * Change the values of various layout options
202 * @param {Object} new_options an associative array of new properties
204 DygraphLayout
.prototype.updateOptions
= function(new_options
) {
205 Dygraph
.update(this.options
, new_options
? new_options
: {});
209 * Return a copy of the point at the indicated index, with its yval unstacked.
210 * @param int index of point in layout_.points
212 DygraphLayout
.prototype.unstackPointAtIndex
= function(idx
) {
213 var point
= this.points
[idx
];
215 // Clone the point since we modify it
216 var unstackedPoint
= {};
217 for (var i
in point
) {
218 unstackedPoint
[i
] = point
[i
];
221 if (!this.attr_("stackedGraph")) {
222 return unstackedPoint
;
225 // The unstacked yval is equal to the current yval minus the yval of the
226 // next point at the same xval.
227 for (var i
= idx
+1; i
< this.points
.length
; i
++) {
228 if (this.points
[i
].xval
== point
.xval
) {
229 unstackedPoint
.yval
-= this.points
[i
].yval
;
234 return unstackedPoint
;
237 // Subclass PlotKit.CanvasRenderer to add:
238 // 1. X/Y grid overlay
239 // 2. Ability to draw error bars (if required)
242 * Sets some PlotKit.CanvasRenderer options
243 * @param {Object} element The canvas to attach to
244 * @param {Layout} layout The DygraphLayout object for this graph.
245 * @param {Object} options Options to pass on to CanvasRenderer
247 DygraphCanvasRenderer
= function(dygraph
, element
, layout
, options
) {
248 // TODO(danvk): remove options, just use dygraph.attr_.
249 this.dygraph_
= dygraph
;
256 "axisLineColor": "black",
257 "axisLineWidth": 0.5,
259 "axisLabelColor": "black",
260 "axisLabelFont": "Arial",
261 "axisLabelFontSize": 9,
262 "axisLabelWidth": 50,
265 "gridLineColor": "rgb(128,128,128)",
267 "underlayCallback": null
269 Dygraph
.update(this.options
, options
);
271 this.layout
= layout
;
272 this.element
= element
;
273 this.container
= this.element
.parentNode
;
275 this.height
= this.element
.height
;
276 this.width
= this.element
.width
;
278 // --- check whether everything is ok before we return
279 if (!this.isIE
&& !(DygraphCanvasRenderer
.isSupported(this.element
)))
280 throw "Canvas is not supported.";
283 this.xlabels
= new Array();
284 this.ylabels
= new Array();
285 this.annotations
= new Array();
288 x
: this.options
.yAxisLabelWidth
+ 2 * this.options
.axisTickSize
,
291 this.area
.w
= this.width
- this.area
.x
- this.options
.rightGap
;
292 this.area
.h
= this.height
- this.options
.axisLabelFontSize
-
293 2 * this.options
.axisTickSize
;
295 this.container
.style
.position
= "relative";
296 this.container
.style
.width
= this.width
+ "px";
299 DygraphCanvasRenderer
.prototype.clear
= function() {
301 // VML takes a while to start up, so we just poll every this.IEDelay
303 if (this.clearDelay
) {
304 this.clearDelay
.cancel();
305 this.clearDelay
= null;
307 var context
= this.element
.getContext("2d");
310 // TODO(danvk): this is broken, since MochiKit.Async is gone.
311 this.clearDelay
= MochiKit
.Async
.wait(this.IEDelay
);
312 this.clearDelay
.addCallback(bind(this.clear
, this));
317 var context
= this.element
.getContext("2d");
318 context
.clearRect(0, 0, this.width
, this.height
);
320 for (var i
= 0; i
< this.xlabels
.length
; i
++) {
321 var el
= this.xlabels
[i
];
322 el
.parentNode
.removeChild(el
);
324 for (var i
= 0; i
< this.ylabels
.length
; i
++) {
325 var el
= this.ylabels
[i
];
326 el
.parentNode
.removeChild(el
);
328 for (var i
= 0; i
< this.annotations
.length
; i
++) {
329 var el
= this.annotations
[i
];
330 el
.parentNode
.removeChild(el
);
332 this.xlabels
= new Array();
333 this.ylabels
= new Array();
334 this.annotations
= new Array();
338 DygraphCanvasRenderer
.isSupported
= function(canvasName
) {
341 if (typeof(canvasName
) == 'undefined' || canvasName
== null)
342 canvas
= document
.createElement("canvas");
345 var context
= canvas
.getContext("2d");
348 var ie
= navigator
.appVersion
.match(/MSIE (\d\.\d)/);
349 var opera
= (navigator
.userAgent
.toLowerCase().indexOf("opera") != -1);
350 if ((!ie
) || (ie
[1] < 6) || (opera
))
358 * Draw an X/Y grid on top of the existing plot
360 DygraphCanvasRenderer
.prototype.render
= function() {
361 // Draw the new X/Y grid
362 var ctx
= this.element
.getContext("2d");
364 if (this.options
.underlayCallback
) {
365 this.options
.underlayCallback(ctx
, this.area
, this.layout
, this.dygraph_
);
368 if (this.options
.drawYGrid
) {
369 var ticks
= this.layout
.yticks
;
371 ctx
.strokeStyle
= this.options
.gridLineColor
;
372 ctx
.lineWidth
= this.options
.axisLineWidth
;
373 for (var i
= 0; i
< ticks
.length
; i
++) {
375 var y
= this.area
.y
+ ticks
[i
][0] * this.area
.h
;
378 ctx
.lineTo(x
+ this.area
.w
, y
);
384 if (this.options
.drawXGrid
) {
385 var ticks
= this.layout
.xticks
;
387 ctx
.strokeStyle
= this.options
.gridLineColor
;
388 ctx
.lineWidth
= this.options
.axisLineWidth
;
389 for (var i
=0; i
<ticks
.length
; i
++) {
390 var x
= this.area
.x
+ ticks
[i
][0] * this.area
.w
;
391 var y
= this.area
.y
+ this.area
.h
;
394 ctx
.lineTo(x
, this.area
.y
);
400 // Do the ordinary rendering, as before
401 this._renderLineChart();
403 this._renderAnnotations();
407 DygraphCanvasRenderer
.prototype._renderAxis
= function() {
408 if (!this.options
.drawXAxis
&& !this.options
.drawYAxis
)
411 var context
= this.element
.getContext("2d");
414 "position": "absolute",
415 "fontSize": this.options
.axisLabelFontSize
+ "px",
417 "color": this.options
.axisLabelColor
,
418 "width": this.options
.axisLabelWidth
+ "px",
421 var makeDiv
= function(txt
) {
422 var div
= document
.createElement("div");
423 for (var name
in labelStyle
) {
424 if (labelStyle
.hasOwnProperty(name
)) {
425 div
.style
[name
] = labelStyle
[name
];
428 div
.appendChild(document
.createTextNode(txt
));
434 context
.strokeStyle
= this.options
.axisLineColor
;
435 context
.lineWidth
= this.options
.axisLineWidth
;
437 if (this.options
.drawYAxis
) {
438 if (this.layout
.yticks
&& this.layout
.yticks
.length
> 0) {
439 for (var i
= 0; i
< this.layout
.yticks
.length
; i
++) {
440 var tick
= this.layout
.yticks
[i
];
441 if (typeof(tick
) == "function") return;
443 var y
= this.area
.y
+ tick
[0] * this.area
.h
;
445 context
.moveTo(x
, y
);
446 context
.lineTo(x
- this.options
.axisTickSize
, y
);
450 var label
= makeDiv(tick
[1]);
451 var top
= (y
- this.options
.axisLabelFontSize
/ 2);
452 if (top
< 0) top
= 0;
454 if (top
+ this.options
.axisLabelFontSize
+ 3 > this.height
) {
455 label
.style
.bottom
= "0px";
457 label
.style
.top
= top
+ "px";
459 label
.style
.left
= "0px";
460 label
.style
.textAlign
= "right";
461 label
.style
.width
= this.options
.yAxisLabelWidth
+ "px";
462 this.container
.appendChild(label
);
463 this.ylabels
.push(label
);
466 // The lowest tick on the y-axis often overlaps with the leftmost
467 // tick on the x-axis. Shift the bottom tick up a little bit to
468 // compensate if necessary.
469 var bottomTick
= this.ylabels
[0];
470 var fontSize
= this.options
.axisLabelFontSize
;
471 var bottom
= parseInt(bottomTick
.style
.top
) + fontSize
;
472 if (bottom
> this.height
- fontSize
) {
473 bottomTick
.style
.top
= (parseInt(bottomTick
.style
.top
) -
474 fontSize
/ 2) + "px";
479 context
.moveTo(this.area
.x
, this.area
.y
);
480 context
.lineTo(this.area
.x
, this.area
.y
+ this.area
.h
);
485 if (this.options
.drawXAxis
) {
486 if (this.layout
.xticks
) {
487 for (var i
= 0; i
< this.layout
.xticks
.length
; i
++) {
488 var tick
= this.layout
.xticks
[i
];
489 if (typeof(dataset
) == "function") return;
491 var x
= this.area
.x
+ tick
[0] * this.area
.w
;
492 var y
= this.area
.y
+ this.area
.h
;
494 context
.moveTo(x
, y
);
495 context
.lineTo(x
, y
+ this.options
.axisTickSize
);
499 var label
= makeDiv(tick
[1]);
500 label
.style
.textAlign
= "center";
501 label
.style
.bottom
= "0px";
503 var left
= (x
- this.options
.axisLabelWidth
/2);
504 if (left
+ this.options
.axisLabelWidth
> this.width
) {
505 left
= this.width
- this.options
.xAxisLabelWidth
;
506 label
.style
.textAlign
= "right";
510 label
.style
.textAlign
= "left";
513 label
.style
.left
= left
+ "px";
514 label
.style
.width
= this.options
.xAxisLabelWidth
+ "px";
515 this.container
.appendChild(label
);
516 this.xlabels
.push(label
);
521 context
.moveTo(this.area
.x
, this.area
.y
+ this.area
.h
);
522 context
.lineTo(this.area
.x
+ this.area
.w
, this.area
.y
+ this.area
.h
);
531 DygraphCanvasRenderer
.prototype._renderAnnotations
= function() {
532 var annotationStyle
= {
533 "position": "absolute",
534 "fontSize": this.options
.axisLabelFontSize
+ "px",
539 var bindEvt
= function(eventName
, classEventName
, p
, self
) {
541 var a
= p
.annotation
;
542 if (a
.hasOwnProperty(eventName
)) {
543 a
[eventName
](a
, p
, self
.dygraph_
, e
);
544 } else if (self
.dygraph_
.attr_(classEventName
)) {
545 self
.dygraph_
.attr_(classEventName
)(a
, p
, self
.dygraph_
,e
);
550 // Get a list of point with annotations.
551 var points
= this.layout
.annotated_points
;
552 for (var i
= 0; i
< points
.length
; i
++) {
554 if (p
.canvasx
< this.area
.x
|| p
.canvasx
> this.area
.x
+ this.area
.w
) {
558 var a
= p
.annotation
;
560 if (a
.hasOwnProperty("tickHeight")) {
561 tick_height
= a
.tickHeight
;
564 var div
= document
.createElement("div");
565 for (var name
in annotationStyle
) {
566 if (annotationStyle
.hasOwnProperty(name
)) {
567 div
.style
[name
] = annotationStyle
[name
];
570 if (!a
.hasOwnProperty('icon')) {
571 div
.className
= "dygraphDefaultAnnotation";
573 if (a
.hasOwnProperty('cssClass')) {
574 div
.className
+= " " + a
.cssClass
;
577 var width
= a
.hasOwnProperty('width') ? a
.width
: 16;
578 var height
= a
.hasOwnProperty('height') ? a
.height
: 16;
579 if (a
.hasOwnProperty('icon')) {
580 var img
= document
.createElement("img");
584 div
.appendChild(img
);
585 } else if (p
.annotation
.hasOwnProperty('shortText')) {
586 div
.appendChild(document
.createTextNode(p
.annotation
.shortText
));
588 div
.style
.left
= (p
.canvasx
- width
/ 2) + "px";
589 if (a
.attachAtBottom
) {
590 div
.style
.top
= (this.area
.h
- height
- tick_height
) + "px";
592 div
.style
.top
= (p
.canvasy
- height
- tick_height
) + "px";
594 div
.style
.width
= width
+ "px";
595 div
.style
.height
= height
+ "px";
596 div
.title
= p
.annotation
.text
;
597 div
.style
.color
= this.colors
[p
.name
];
598 div
.style
.borderColor
= this.colors
[p
.name
];
601 Dygraph
.addEvent(div
, 'click',
602 bindEvt('clickHandler', 'annotationClickHandler', p
, this));
603 Dygraph
.addEvent(div
, 'mouseover',
604 bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p
, this));
605 Dygraph
.addEvent(div
, 'mouseout',
606 bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p
, this));
607 Dygraph
.addEvent(div
, 'dblclick',
608 bindEvt('dblClickHandler', 'annotationDblClickHandler', p
, this));
610 this.container
.appendChild(div
);
611 this.annotations
.push(div
);
613 var ctx
= this.element
.getContext("2d");
614 ctx
.strokeStyle
= this.colors
[p
.name
];
616 if (!a
.attachAtBottom
) {
617 ctx
.moveTo(p
.canvasx
, p
.canvasy
);
618 ctx
.lineTo(p
.canvasx
, p
.canvasy
- 2 - tick_height
);
620 ctx
.moveTo(p
.canvasx
, this.area
.h
);
621 ctx
.lineTo(p
.canvasx
, this.area
.h
- 2 - tick_height
);
630 * Overrides the CanvasRenderer method to draw error bars
632 DygraphCanvasRenderer
.prototype._renderLineChart
= function() {
633 var context
= this.element
.getContext("2d");
634 var colorCount
= this.options
.colorScheme
.length
;
635 var colorScheme
= this.options
.colorScheme
;
636 var fillAlpha
= this.options
.fillAlpha
;
637 var errorBars
= this.layout
.options
.errorBars
;
638 var fillGraph
= this.layout
.options
.fillGraph
;
639 var stackedGraph
= this.layout
.options
.stackedGraph
;
640 var stepPlot
= this.layout
.options
.stepPlot
;
643 for (var name
in this.layout
.datasets
) {
644 if (this.layout
.datasets
.hasOwnProperty(name
)) {
648 var setCount
= setNames
.length
;
651 for (var i
= 0; i
< setCount
; i
++) {
652 this.colors
[setNames
[i
]] = colorScheme
[i
% colorCount
];
657 for (var i
= 0; i
< this.layout
.points
.length
; i
++) {
658 var point
= this.layout
.points
[i
];
659 point
.canvasx
= this.area
.w
* point
.x
+ this.area
.x
;
660 point
.canvasy
= this.area
.h
* point
.y
+ this.area
.y
;
664 var isOK
= function(x
) { return x
&& !isNaN(x
); };
669 this.dygraph_
.warn("Can't use fillGraph option with error bars");
672 for (var i
= 0; i
< setCount
; i
++) {
673 var setName
= setNames
[i
];
674 var color
= this.colors
[setName
];
676 // setup graphics context
680 var prevYs
= [-1, -1];
681 var yscale
= this.layout
.yscale
;
682 // should be same color as the lines but only 15% opaque.
683 var rgb
= new RGBColor(color
);
684 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
686 ctx
.fillStyle
= err_color
;
688 for (var j
= 0; j
< this.layout
.points
.length
; j
++) {
689 var point
= this.layout
.points
[j
];
690 if (point
.name
== setName
) {
691 if (!isOK(point
.y
)) {
698 var newYs
= [ prevY
- point
.errorPlus
* yscale
,
699 prevY
+ point
.errorMinus
* yscale
];
702 var newYs
= [ point
.y
- point
.errorPlus
* yscale
,
703 point
.y
+ point
.errorMinus
* yscale
];
705 newYs
[0] = this.area
.h
* newYs
[0] + this.area
.y
;
706 newYs
[1] = this.area
.h
* newYs
[1] + this.area
.y
;
709 ctx
.moveTo(prevX
, newYs
[0]);
711 ctx
.moveTo(prevX
, prevYs
[0]);
713 ctx
.lineTo(point
.canvasx
, newYs
[0]);
714 ctx
.lineTo(point
.canvasx
, newYs
[1]);
716 ctx
.lineTo(prevX
, newYs
[1]);
718 ctx
.lineTo(prevX
, prevYs
[1]);
723 prevX
= point
.canvasx
;
728 } else if (fillGraph
) {
729 var axisY
= 1.0 + this.layout
.minyval
* this.layout
.yscale
;
730 if (axisY
< 0.0) axisY
= 0.0;
731 else if (axisY
> 1.0) axisY
= 1.0;
732 axisY
= this.area
.h
* axisY
+ this.area
.y
;
734 var baseline
= [] // for stacked graphs: baseline for filling
736 // process sets in reverse order (needed for stacked graphs)
737 for (var i
= setCount
- 1; i
>= 0; i
--) {
738 var setName
= setNames
[i
];
739 var color
= this.colors
[setName
];
741 // setup graphics context
744 var prevYs
= [-1, -1];
745 var yscale
= this.layout
.yscale
;
746 // should be same color as the lines but only 15% opaque.
747 var rgb
= new RGBColor(color
);
748 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
750 ctx
.fillStyle
= err_color
;
752 for (var j
= 0; j
< this.layout
.points
.length
; j
++) {
753 var point
= this.layout
.points
[j
];
754 if (point
.name
== setName
) {
755 if (!isOK(point
.y
)) {
761 lastY
= baseline
[point
.canvasx
];
762 if (lastY
=== undefined
) lastY
= axisY
;
763 baseline
[point
.canvasx
] = point
.canvasy
;
764 newYs
= [ point
.canvasy
, lastY
];
766 newYs
= [ point
.canvasy
, axisY
];
769 ctx
.moveTo(prevX
, prevYs
[0]);
771 ctx
.lineTo(point
.canvasx
, prevYs
[0]);
773 ctx
.lineTo(point
.canvasx
, newYs
[0]);
775 ctx
.lineTo(point
.canvasx
, newYs
[1]);
776 ctx
.lineTo(prevX
, prevYs
[1]);
780 prevX
= point
.canvasx
;
787 for (var i
= 0; i
< setCount
; i
++) {
788 var setName
= setNames
[i
];
789 var color
= this.colors
[setName
];
790 var strokeWidth
= this.dygraph_
.attr_("strokeWidth", setName
);
792 // setup graphics context
794 var point
= this.layout
.points
[0];
795 var pointSize
= this.dygraph_
.attr_("pointSize", setName
);
796 var prevX
= null, prevY
= null;
797 var drawPoints
= this.dygraph_
.attr_("drawPoints", setName
);
798 var points
= this.layout
.points
;
799 for (var j
= 0; j
< points
.length
; j
++) {
800 var point
= points
[j
];
801 if (point
.name
== setName
) {
802 if (!isOK(point
.canvasy
)) {
803 if (stepPlot
&& prevX
!= null) {
804 // Draw a horizontal line to the start of the missing data
806 ctx
.strokeStyle
= color
;
807 ctx
.lineWidth
= this.options
.strokeWidth
;
808 ctx
.moveTo(prevX
, prevY
);
809 ctx
.lineTo(point
.canvasx
, prevY
);
812 // this will make us move to the next point, not draw a line to it.
813 prevX
= prevY
= null;
815 // A point is "isolated" if it is non-null but both the previous
816 // and next points are null.
817 var isIsolated
= (!prevX
&& (j
== points
.length
- 1 ||
818 !isOK(points
[j
+1].canvasy
)));
821 prevX
= point
.canvasx
;
822 prevY
= point
.canvasy
;
824 // TODO(danvk): figure out why this conditional is necessary.
827 ctx
.strokeStyle
= color
;
828 ctx
.lineWidth
= strokeWidth
;
829 ctx
.moveTo(prevX
, prevY
);
831 ctx
.lineTo(point
.canvasx
, prevY
);
833 prevX
= point
.canvasx
;
834 prevY
= point
.canvasy
;
835 ctx
.lineTo(prevX
, prevY
);
840 if (drawPoints
|| isIsolated
) {
842 ctx
.fillStyle
= color
;
843 ctx
.arc(point
.canvasx
, point
.canvasy
, pointSize
,
844 0, 2 * Math
.PI
, false);