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 for (var i
= 0; i
< this.options
.yAxes
.length
; i
++) {
86 var axis
= this.options
.yAxes
[i
];
87 axis
.minyval
= axis
.computedValueRange
[0];
88 axis
.maxyval
= axis
.computedValueRange
[1];
89 axis
.yrange
= axis
.maxyval
- axis
.minyval
;
90 axis
.yscale
= (axis
.yrange
!= 0 ? 1.0 / axis
.yrange
: 1.0);
92 axis
.ylogrange
= Dygraph
.log10(axis
.maxyval
) - Dygraph
.log10(axis
.minyval
);
93 axis
.ylogscale
= (axis
.ylogrange
!= 0 ? 1.0 / axis
.ylogrange
: 1.0);
97 DygraphLayout
.prototype._evaluateLineCharts
= function() {
99 this.points
= new Array();
100 for (var setName
in this.datasets
) {
101 if (!this.datasets
.hasOwnProperty(setName
)) continue;
103 var dataset
= this.datasets
[setName
];
104 var axis
= this.options
.yAxes
[this.options
.seriesToAxisMap
[setName
]];
106 for (var j
= 0; j
< dataset
.length
; j
++) {
107 var item
= dataset
[j
];
111 yval
= 1.0 - ((Dygraph
.log10(parseFloat(item
[1])) - Dygraph
.log10(axis
.minyval
)) * axis
.ylogscale
); // really should just be yscale.
113 yval
= 1.0 - ((parseFloat(item
[1]) - axis
.minyval
) * axis
.yscale
);
117 x
: ((parseFloat(item
[0]) - this.minxval
) * this.xscale
),
119 xval
: parseFloat(item
[0]),
120 yval
: parseFloat(item
[1]),
124 this.points
.push(point
);
129 DygraphLayout
.prototype._evaluateLineTicks
= function() {
130 this.xticks
= new Array();
131 for (var i
= 0; i
< this.options
.xTicks
.length
; i
++) {
132 var tick
= this.options
.xTicks
[i
];
133 var label
= tick
.label
;
134 var pos
= this.xscale
* (tick
.v
- this.minxval
);
135 if ((pos
>= 0.0) && (pos
<= 1.0)) {
136 this.xticks
.push([pos
, label
]);
140 this.yticks
= new Array();
141 for (var i
= 0; i
< this.options
.yAxes
.length
; i
++ ) {
142 var axis
= this.options
.yAxes
[i
];
143 for (var j
= 0; j
< axis
.ticks
.length
; j
++) {
144 var tick
= axis
.ticks
[j
];
145 var label
= tick
.label
;
146 var pos
= this.dygraph_
.toPercentYCoord(tick
.v
, i
);
147 if ((pos
>= 0.0) && (pos
<= 1.0)) {
148 this.yticks
.push([i
, pos
, label
]);
156 * Behaves the same way as PlotKit.Layout, but also copies the errors
159 DygraphLayout
.prototype.evaluateWithError
= function() {
161 if (!this.options
.errorBars
) return;
163 // Copy over the error terms
164 var i
= 0; // index in this.points
165 for (var setName
in this.datasets
) {
166 if (!this.datasets
.hasOwnProperty(setName
)) continue;
168 var dataset
= this.datasets
[setName
];
169 for (var j
= 0; j
< dataset
.length
; j
++, i
++) {
170 var item
= dataset
[j
];
171 var xv
= parseFloat(item
[0]);
172 var yv
= parseFloat(item
[1]);
174 if (xv
== this.points
[i
].xval
&&
175 yv
== this.points
[i
].yval
) {
176 this.points
[i
].errorMinus
= parseFloat(item
[2]);
177 this.points
[i
].errorPlus
= parseFloat(item
[3]);
183 DygraphLayout
.prototype._evaluateAnnotations
= function() {
184 // Add the annotations to the point to which they belong.
185 // Make a map from (setName, xval) to annotation for quick lookups.
186 var annotations
= {};
187 for (var i
= 0; i
< this.annotations
.length
; i
++) {
188 var a
= this.annotations
[i
];
189 annotations
[a
.xval
+ "," + a
.series
] = a
;
192 this.annotated_points
= [];
193 for (var i
= 0; i
< this.points
.length
; i
++) {
194 var p
= this.points
[i
];
195 var k
= p
.xval
+ "," + p
.name
;
196 if (k
in annotations
) {
197 p
.annotation
= annotations
[k
];
198 this.annotated_points
.push(p
);
204 * Convenience function to remove all the data sets from a graph
206 DygraphLayout
.prototype.removeAllDatasets
= function() {
207 delete this.datasets
;
208 this.datasets
= new Array();
212 * Change the values of various layout options
213 * @param {Object} new_options an associative array of new properties
215 DygraphLayout
.prototype.updateOptions
= function(new_options
) {
216 Dygraph
.update(this.options
, new_options
? new_options
: {});
220 * Return a copy of the point at the indicated index, with its yval unstacked.
221 * @param int index of point in layout_.points
223 DygraphLayout
.prototype.unstackPointAtIndex
= function(idx
) {
224 var point
= this.points
[idx
];
226 // Clone the point since we modify it
227 var unstackedPoint
= {};
228 for (var i
in point
) {
229 unstackedPoint
[i
] = point
[i
];
232 if (!this.attr_("stackedGraph")) {
233 return unstackedPoint
;
236 // The unstacked yval is equal to the current yval minus the yval of the
237 // next point at the same xval.
238 for (var i
= idx
+1; i
< this.points
.length
; i
++) {
239 if (this.points
[i
].xval
== point
.xval
) {
240 unstackedPoint
.yval
-= this.points
[i
].yval
;
245 return unstackedPoint
;
248 // Subclass PlotKit.CanvasRenderer to add:
249 // 1. X/Y grid overlay
250 // 2. Ability to draw error bars (if required)
253 * Sets some PlotKit.CanvasRenderer options
254 * @param {Object} element The canvas to attach to
255 * @param {Layout} layout The DygraphLayout object for this graph.
256 * @param {Object} options Options to pass on to CanvasRenderer
258 DygraphCanvasRenderer
= function(dygraph
, element
, layout
, options
) {
259 // TODO(danvk): remove options, just use dygraph.attr_.
260 this.dygraph_
= dygraph
;
267 "axisLineColor": "black",
268 "axisLineWidth": 0.5,
270 "axisLabelColor": "black",
271 "axisLabelFont": "Arial",
272 "axisLabelFontSize": 9,
273 "axisLabelWidth": 50,
276 "gridLineColor": "rgb(128,128,128)",
278 "underlayCallback": null
280 Dygraph
.update(this.options
, options
);
282 this.layout
= layout
;
283 this.element
= element
;
284 this.container
= this.element
.parentNode
;
286 this.height
= this.element
.height
;
287 this.width
= this.element
.width
;
289 // --- check whether everything is ok before we return
290 if (!this.isIE
&& !(DygraphCanvasRenderer
.isSupported(this.element
)))
291 throw "Canvas is not supported.";
294 this.xlabels
= new Array();
295 this.ylabels
= new Array();
296 this.annotations
= new Array();
298 // TODO(danvk): consider all axes in this computation.
300 // TODO(danvk): per-axis setting.
301 x
: this.options
.yAxisLabelWidth
+ 2 * this.options
.axisTickSize
,
304 this.area
.w
= this.width
- this.area
.x
- this.options
.rightGap
;
305 this.area
.h
= this.height
- this.options
.axisLabelFontSize
-
306 2 * this.options
.axisTickSize
;
308 // Shrink the drawing area to accomodate additional y-axes.
309 if (this.dygraph_
.numAxes() == 2) {
310 // TODO(danvk): per-axis setting.
311 this.area
.w
-= (this.options
.yAxisLabelWidth
+ 2 * this.options
.axisTickSize
);
312 } else if (this.dygraph_
.numAxes() > 2) {
313 this.dygraph_
.error("Only two y-axes are supported at this time. (Trying " +
314 "to use " + this.dygraph_
.numAxes() + ")");
317 this.container
.style
.position
= "relative";
318 this.container
.style
.width
= this.width
+ "px";
320 // Set up a clipping area for the canvas (and the interaction canvas).
321 // This ensures that we don't overdraw.
322 var ctx
= this.dygraph_
.canvas_
.getContext("2d");
324 ctx
.rect(this.area
.x
, this.area
.y
, this.area
.w
, this.area
.h
);
327 ctx
= this.dygraph_
.hidden_
.getContext("2d");
329 ctx
.rect(this.area
.x
, this.area
.y
, this.area
.w
, this.area
.h
);
333 DygraphCanvasRenderer
.prototype.attr_
= function(x
) {
334 return this.dygraph_
.attr_(x
);
337 DygraphCanvasRenderer
.prototype.clear
= function() {
339 // VML takes a while to start up, so we just poll every this.IEDelay
341 if (this.clearDelay
) {
342 this.clearDelay
.cancel();
343 this.clearDelay
= null;
345 var context
= this.element
.getContext("2d");
348 // TODO(danvk): this is broken, since MochiKit.Async is gone.
349 this.clearDelay
= MochiKit
.Async
.wait(this.IEDelay
);
350 this.clearDelay
.addCallback(bind(this.clear
, this));
355 var context
= this.element
.getContext("2d");
356 context
.clearRect(0, 0, this.width
, this.height
);
358 for (var i
= 0; i
< this.xlabels
.length
; i
++) {
359 var el
= this.xlabels
[i
];
360 if (el
.parentNode
) el
.parentNode
.removeChild(el
);
362 for (var i
= 0; i
< this.ylabels
.length
; i
++) {
363 var el
= this.ylabels
[i
];
364 if (el
.parentNode
) el
.parentNode
.removeChild(el
);
366 for (var i
= 0; i
< this.annotations
.length
; i
++) {
367 var el
= this.annotations
[i
];
368 if (el
.parentNode
) el
.parentNode
.removeChild(el
);
370 this.xlabels
= new Array();
371 this.ylabels
= new Array();
372 this.annotations
= new Array();
376 DygraphCanvasRenderer
.isSupported
= function(canvasName
) {
379 if (typeof(canvasName
) == 'undefined' || canvasName
== null)
380 canvas
= document
.createElement("canvas");
383 var context
= canvas
.getContext("2d");
386 var ie
= navigator
.appVersion
.match(/MSIE (\d\.\d)/);
387 var opera
= (navigator
.userAgent
.toLowerCase().indexOf("opera") != -1);
388 if ((!ie
) || (ie
[1] < 6) || (opera
))
396 * Draw an X/Y grid on top of the existing plot
398 DygraphCanvasRenderer
.prototype.render
= function() {
399 // Draw the new X/Y grid
. Lines appear crisper when pixels are rounded to
400 // half-integers. This prevents them from drawing in two rows/cols.
401 var ctx
= this.element
.getContext("2d");
402 function halfUp(x
){return Math
.round(x
)+0.5};
403 function halfDown(y
){return Math
.round(y
)-0.5};
405 if (this.options
.underlayCallback
) {
406 // NOTE: we pass the dygraph object to this callback twice to avoid breaking
407 // users who expect a deprecated form of this callback.
408 this.options
.underlayCallback(ctx
, this.area
, this.dygraph_
, this.dygraph_
);
411 if (this.options
.drawYGrid
) {
412 var ticks
= this.layout
.yticks
;
414 ctx
.strokeStyle
= this.options
.gridLineColor
;
415 ctx
.lineWidth
= this.options
.axisLineWidth
;
416 for (var i
= 0; i
< ticks
.length
; i
++) {
417 // TODO(danvk): allow secondary axes to draw a grid, too.
418 if (ticks
[i
][0] != 0) continue;
419 var x
= halfUp(this.area
.x
);
420 var y
= halfDown(this.area
.y
+ ticks
[i
][1] * this.area
.h
);
423 ctx
.lineTo(x
+ this.area
.w
, y
);
429 if (this.options
.drawXGrid
) {
430 var ticks
= this.layout
.xticks
;
432 ctx
.strokeStyle
= this.options
.gridLineColor
;
433 ctx
.lineWidth
= this.options
.axisLineWidth
;
434 for (var i
=0; i
<ticks
.length
; i
++) {
435 var x
= halfUp(this.area
.x
+ ticks
[i
][0] * this.area
.w
);
436 var y
= halfDown(this.area
.y
+ this.area
.h
);
439 ctx
.lineTo(x
, this.area
.y
);
445 // Do the ordinary rendering, as before
446 this._renderLineChart();
448 this._renderAnnotations();
452 DygraphCanvasRenderer
.prototype._renderAxis
= function() {
453 if (!this.options
.drawXAxis
&& !this.options
.drawYAxis
)
456 // Round pixels to half-integer boundaries for crisper drawing.
457 function halfUp(x
){return Math
.round(x
)+0.5};
458 function halfDown(y
){return Math
.round(y
)-0.5};
460 var context
= this.element
.getContext("2d");
463 "position": "absolute",
464 "fontSize": this.options
.axisLabelFontSize
+ "px",
466 "color": this.options
.axisLabelColor
,
467 "width": this.options
.axisLabelWidth
+ "px",
470 var makeDiv
= function(txt
) {
471 var div
= document
.createElement("div");
472 for (var name
in labelStyle
) {
473 if (labelStyle
.hasOwnProperty(name
)) {
474 div
.style
[name
] = labelStyle
[name
];
477 div
.appendChild(document
.createTextNode(txt
));
483 context
.strokeStyle
= this.options
.axisLineColor
;
484 context
.lineWidth
= this.options
.axisLineWidth
;
486 if (this.options
.drawYAxis
) {
487 if (this.layout
.yticks
&& this.layout
.yticks
.length
> 0) {
488 for (var i
= 0; i
< this.layout
.yticks
.length
; i
++) {
489 var tick
= this.layout
.yticks
[i
];
490 if (typeof(tick
) == "function") return;
493 if (tick
[0] == 1) { // right-side y-axis
494 x
= this.area
.x
+ this.area
.w
;
497 var y
= this.area
.y
+ tick
[1] * this.area
.h
;
499 context
.moveTo(halfUp(x
), halfDown(y
));
500 context
.lineTo(halfUp(x
- sgn
* this.options
.axisTickSize
), halfDown(y
));
504 var label
= makeDiv(tick
[2]);
505 var top
= (y
- this.options
.axisLabelFontSize
/ 2);
506 if (top
< 0) top
= 0;
508 if (top
+ this.options
.axisLabelFontSize
+ 3 > this.height
) {
509 label
.style
.bottom
= "0px";
511 label
.style
.top
= top
+ "px";
514 label
.style
.left
= "0px";
515 label
.style
.textAlign
= "right";
516 } else if (tick
[0] == 1) {
517 label
.style
.left
= (this.area
.x
+ this.area
.w
+
518 this.options
.axisTickSize
) + "px";
519 label
.style
.textAlign
= "left";
521 label
.style
.width
= this.options
.yAxisLabelWidth
+ "px";
522 this.container
.appendChild(label
);
523 this.ylabels
.push(label
);
526 // The lowest tick on the y-axis often overlaps with the leftmost
527 // tick on the x-axis. Shift the bottom tick up a little bit to
528 // compensate if necessary.
529 var bottomTick
= this.ylabels
[0];
530 var fontSize
= this.options
.axisLabelFontSize
;
531 var bottom
= parseInt(bottomTick
.style
.top
) + fontSize
;
532 if (bottom
> this.height
- fontSize
) {
533 bottomTick
.style
.top
= (parseInt(bottomTick
.style
.top
) -
534 fontSize
/ 2) + "px";
538 // draw a vertical line on the left to separate the chart from the labels.
540 context
.moveTo(halfUp(this.area
.x
), halfDown(this.area
.y
));
541 context
.lineTo(halfUp(this.area
.x
), halfDown(this.area
.y
+ this.area
.h
));
545 // if there's a secondary y-axis, draw a vertical line for that, too.
546 if (this.dygraph_
.numAxes() == 2) {
548 context
.moveTo(halfDown(this.area
.x
+ this.area
.w
), halfDown(this.area
.y
));
549 context
.lineTo(halfDown(this.area
.x
+ this.area
.w
), halfDown(this.area
.y
+ this.area
.h
));
555 if (this.options
.drawXAxis
) {
556 if (this.layout
.xticks
) {
557 for (var i
= 0; i
< this.layout
.xticks
.length
; i
++) {
558 var tick
= this.layout
.xticks
[i
];
559 if (typeof(dataset
) == "function") return;
561 var x
= this.area
.x
+ tick
[0] * this.area
.w
;
562 var y
= this.area
.y
+ this.area
.h
;
564 context
.moveTo(halfUp(x
), halfDown(y
));
565 context
.lineTo(halfUp(x
), halfDown(y
+ this.options
.axisTickSize
));
569 var label
= makeDiv(tick
[1]);
570 label
.style
.textAlign
= "center";
571 label
.style
.bottom
= "0px";
573 var left
= (x
- this.options
.axisLabelWidth
/2);
574 if (left
+ this.options
.axisLabelWidth
> this.width
) {
575 left
= this.width
- this.options
.xAxisLabelWidth
;
576 label
.style
.textAlign
= "right";
580 label
.style
.textAlign
= "left";
583 label
.style
.left
= left
+ "px";
584 label
.style
.width
= this.options
.xAxisLabelWidth
+ "px";
585 this.container
.appendChild(label
);
586 this.xlabels
.push(label
);
591 context
.moveTo(halfUp(this.area
.x
), halfDown(this.area
.y
+ this.area
.h
));
592 context
.lineTo(halfUp(this.area
.x
+ this.area
.w
), halfDown(this.area
.y
+ this.area
.h
));
601 DygraphCanvasRenderer
.prototype._renderAnnotations
= function() {
602 var annotationStyle
= {
603 "position": "absolute",
604 "fontSize": this.options
.axisLabelFontSize
+ "px",
609 var bindEvt
= function(eventName
, classEventName
, p
, self
) {
611 var a
= p
.annotation
;
612 if (a
.hasOwnProperty(eventName
)) {
613 a
[eventName
](a
, p
, self
.dygraph_
, e
);
614 } else if (self
.dygraph_
.attr_(classEventName
)) {
615 self
.dygraph_
.attr_(classEventName
)(a
, p
, self
.dygraph_
,e
);
620 // Get a list of point with annotations.
621 var points
= this.layout
.annotated_points
;
622 for (var i
= 0; i
< points
.length
; i
++) {
624 if (p
.canvasx
< this.area
.x
|| p
.canvasx
> this.area
.x
+ this.area
.w
) {
628 var a
= p
.annotation
;
630 if (a
.hasOwnProperty("tickHeight")) {
631 tick_height
= a
.tickHeight
;
634 var div
= document
.createElement("div");
635 for (var name
in annotationStyle
) {
636 if (annotationStyle
.hasOwnProperty(name
)) {
637 div
.style
[name
] = annotationStyle
[name
];
640 if (!a
.hasOwnProperty('icon')) {
641 div
.className
= "dygraphDefaultAnnotation";
643 if (a
.hasOwnProperty('cssClass')) {
644 div
.className
+= " " + a
.cssClass
;
647 var width
= a
.hasOwnProperty('width') ? a
.width
: 16;
648 var height
= a
.hasOwnProperty('height') ? a
.height
: 16;
649 if (a
.hasOwnProperty('icon')) {
650 var img
= document
.createElement("img");
654 div
.appendChild(img
);
655 } else if (p
.annotation
.hasOwnProperty('shortText')) {
656 div
.appendChild(document
.createTextNode(p
.annotation
.shortText
));
658 div
.style
.left
= (p
.canvasx
- width
/ 2) + "px";
659 if (a
.attachAtBottom
) {
660 div
.style
.top
= (this.area
.h
- height
- tick_height
) + "px";
662 div
.style
.top
= (p
.canvasy
- height
- tick_height
) + "px";
664 div
.style
.width
= width
+ "px";
665 div
.style
.height
= height
+ "px";
666 div
.title
= p
.annotation
.text
;
667 div
.style
.color
= this.colors
[p
.name
];
668 div
.style
.borderColor
= this.colors
[p
.name
];
671 Dygraph
.addEvent(div
, 'click',
672 bindEvt('clickHandler', 'annotationClickHandler', p
, this));
673 Dygraph
.addEvent(div
, 'mouseover',
674 bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p
, this));
675 Dygraph
.addEvent(div
, 'mouseout',
676 bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p
, this));
677 Dygraph
.addEvent(div
, 'dblclick',
678 bindEvt('dblClickHandler', 'annotationDblClickHandler', p
, this));
680 this.container
.appendChild(div
);
681 this.annotations
.push(div
);
683 var ctx
= this.element
.getContext("2d");
684 ctx
.strokeStyle
= this.colors
[p
.name
];
686 if (!a
.attachAtBottom
) {
687 ctx
.moveTo(p
.canvasx
, p
.canvasy
);
688 ctx
.lineTo(p
.canvasx
, p
.canvasy
- 2 - tick_height
);
690 ctx
.moveTo(p
.canvasx
, this.area
.h
);
691 ctx
.lineTo(p
.canvasx
, this.area
.h
- 2 - tick_height
);
700 * Overrides the CanvasRenderer method to draw error bars
702 DygraphCanvasRenderer
.prototype._renderLineChart
= function() {
703 // TODO(danvk): use this.attr_ for many of these.
704 var context
= this.element
.getContext("2d");
705 var colorCount
= this.options
.colorScheme
.length
;
706 var colorScheme
= this.options
.colorScheme
;
707 var fillAlpha
= this.options
.fillAlpha
;
708 var errorBars
= this.layout
.options
.errorBars
;
709 var fillGraph
= this.attr_("fillGraph");
710 var stackedGraph
= this.layout
.options
.stackedGraph
;
711 var stepPlot
= this.layout
.options
.stepPlot
;
714 for (var name
in this.layout
.datasets
) {
715 if (this.layout
.datasets
.hasOwnProperty(name
)) {
719 var setCount
= setNames
.length
;
722 for (var i
= 0; i
< setCount
; i
++) {
723 this.colors
[setNames
[i
]] = colorScheme
[i
% colorCount
];
728 for (var i
= 0; i
< this.layout
.points
.length
; i
++) {
729 var point
= this.layout
.points
[i
];
730 point
.canvasx
= this.area
.w
* point
.x
+ this.area
.x
;
731 point
.canvasy
= this.area
.h
* point
.y
+ this.area
.y
;
735 var isOK
= function(x
) { return x
&& !isNaN(x
); };
740 this.dygraph_
.warn("Can't use fillGraph option with error bars");
743 for (var i
= 0; i
< setCount
; i
++) {
744 var setName
= setNames
[i
];
745 var axis
= this.layout
.options
.yAxes
[
746 this.layout
.options
.seriesToAxisMap
[setName
]];
747 var color
= this.colors
[setName
];
749 // setup graphics context
753 var prevYs
= [-1, -1];
754 var yscale
= axis
.yscale
;
755 // should be same color as the lines but only 15% opaque.
756 var rgb
= new RGBColor(color
);
757 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
759 ctx
.fillStyle
= err_color
;
761 for (var j
= 0; j
< this.layout
.points
.length
; j
++) {
762 var point
= this.layout
.points
[j
];
763 if (point
.name
== setName
) {
764 if (!isOK(point
.y
)) {
771 var newYs
= [ prevY
- point
.errorPlus
* yscale
,
772 prevY
+ point
.errorMinus
* yscale
];
775 var newYs
= [ point
.y
- point
.errorPlus
* yscale
,
776 point
.y
+ point
.errorMinus
* yscale
];
778 newYs
[0] = this.area
.h
* newYs
[0] + this.area
.y
;
779 newYs
[1] = this.area
.h
* newYs
[1] + this.area
.y
;
782 ctx
.moveTo(prevX
, newYs
[0]);
784 ctx
.moveTo(prevX
, prevYs
[0]);
786 ctx
.lineTo(point
.canvasx
, newYs
[0]);
787 ctx
.lineTo(point
.canvasx
, newYs
[1]);
789 ctx
.lineTo(prevX
, newYs
[1]);
791 ctx
.lineTo(prevX
, prevYs
[1]);
796 prevX
= point
.canvasx
;
801 } else if (fillGraph
) {
802 var baseline
= [] // for stacked graphs: baseline for filling
804 // process sets in reverse order (needed for stacked graphs)
805 for (var i
= setCount
- 1; i
>= 0; i
--) {
806 var setName
= setNames
[i
];
807 var color
= this.colors
[setName
];
808 var axis
= this.layout
.options
.yAxes
[
809 this.layout
.options
.seriesToAxisMap
[setName
]];
810 var axisY
= 1.0 + axis
.minyval
* axis
.yscale
;
811 if (axisY
< 0.0) axisY
= 0.0;
812 else if (axisY
> 1.0) axisY
= 1.0;
813 axisY
= this.area
.h
* axisY
+ this.area
.y
;
815 // setup graphics context
818 var prevYs
= [-1, -1];
819 var yscale
= axis
.yscale
;
820 // should be same color as the lines but only 15% opaque.
821 var rgb
= new RGBColor(color
);
822 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
824 ctx
.fillStyle
= err_color
;
826 for (var j
= 0; j
< this.layout
.points
.length
; j
++) {
827 var point
= this.layout
.points
[j
];
828 if (point
.name
== setName
) {
829 if (!isOK(point
.y
)) {
835 lastY
= baseline
[point
.canvasx
];
836 if (lastY
=== undefined
) lastY
= axisY
;
837 baseline
[point
.canvasx
] = point
.canvasy
;
838 newYs
= [ point
.canvasy
, lastY
];
840 newYs
= [ point
.canvasy
, axisY
];
843 ctx
.moveTo(prevX
, prevYs
[0]);
845 ctx
.lineTo(point
.canvasx
, prevYs
[0]);
847 ctx
.lineTo(point
.canvasx
, newYs
[0]);
849 ctx
.lineTo(point
.canvasx
, newYs
[1]);
850 ctx
.lineTo(prevX
, prevYs
[1]);
854 prevX
= point
.canvasx
;
861 for (var i
= 0; i
< setCount
; i
++) {
862 var setName
= setNames
[i
];
863 var color
= this.colors
[setName
];
864 var strokeWidth
= this.dygraph_
.attr_("strokeWidth", setName
);
866 // setup graphics context
868 var point
= this.layout
.points
[0];
869 var pointSize
= this.dygraph_
.attr_("pointSize", setName
);
870 var prevX
= null, prevY
= null;
871 var drawPoints
= this.dygraph_
.attr_("drawPoints", setName
);
872 var points
= this.layout
.points
;
873 for (var j
= 0; j
< points
.length
; j
++) {
874 var point
= points
[j
];
875 if (point
.name
== setName
) {
876 if (!isOK(point
.canvasy
)) {
877 if (stepPlot
&& prevX
!= null) {
878 // Draw a horizontal line to the start of the missing data
880 ctx
.strokeStyle
= color
;
881 ctx
.lineWidth
= this.options
.strokeWidth
;
882 ctx
.moveTo(prevX
, prevY
);
883 ctx
.lineTo(point
.canvasx
, prevY
);
886 // this will make us move to the next point, not draw a line to it.
887 prevX
= prevY
= null;
889 // A point is "isolated" if it is non-null but both the previous
890 // and next points are null.
891 var isIsolated
= (!prevX
&& (j
== points
.length
- 1 ||
892 !isOK(points
[j
+1].canvasy
)));
895 prevX
= point
.canvasx
;
896 prevY
= point
.canvasy
;
898 // TODO(danvk): figure out why this conditional is necessary.
901 ctx
.strokeStyle
= color
;
902 ctx
.lineWidth
= strokeWidth
;
903 ctx
.moveTo(prevX
, prevY
);
905 ctx
.lineTo(point
.canvasx
, prevY
);
907 prevX
= point
.canvasx
;
908 prevY
= point
.canvasy
;
909 ctx
.lineTo(prevX
, prevY
);
914 if (drawPoints
|| isIsolated
) {
916 ctx
.fillStyle
= color
;
917 ctx
.arc(point
.canvasx
, point
.canvasy
, pointSize
,
918 0, 2 * Math
.PI
, false);