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);
94 DygraphLayout
.prototype._evaluateLineCharts
= function() {
96 this.points
= new Array();
97 for (var setName
in this.datasets
) {
98 if (!this.datasets
.hasOwnProperty(setName
)) continue;
100 var dataset
= this.datasets
[setName
];
101 var axis
= this.options
.yAxes
[this.options
.seriesToAxisMap
[setName
]];
103 for (var j
= 0; j
< dataset
.length
; j
++) {
104 var item
= dataset
[j
];
107 x
: ((parseFloat(item
[0]) - this.minxval
) * this.xscale
),
108 y
: 1.0 - ((parseFloat(item
[1]) - axis
.minyval
) * axis
.yscale
),
109 xval
: parseFloat(item
[0]),
110 yval
: parseFloat(item
[1]),
114 // limit the x, y values so they do not overdraw
115 if (point
.y
<= 0.0) {
118 if (point
.y
>= 1.0) {
121 this.points
.push(point
);
126 DygraphLayout
.prototype._evaluateLineTicks
= function() {
127 this.xticks
= new Array();
128 for (var i
= 0; i
< this.options
.xTicks
.length
; i
++) {
129 var tick
= this.options
.xTicks
[i
];
130 var label
= tick
.label
;
131 var pos
= this.xscale
* (tick
.v
- this.minxval
);
132 if ((pos
>= 0.0) && (pos
<= 1.0)) {
133 this.xticks
.push([pos
, label
]);
137 this.yticks
= new Array();
138 for (var i
= 0; i
< this.options
.yAxes
.length
; i
++ ) {
139 var axis
= this.options
.yAxes
[i
];
140 for (var j
= 0; j
< axis
.ticks
.length
; j
++) {
141 var tick
= axis
.ticks
[j
];
142 var label
= tick
.label
;
143 var pos
= 1.0 - (axis
.yscale
* (tick
.v
- axis
.minyval
));
144 if ((pos
>= 0.0) && (pos
<= 1.0)) {
145 this.yticks
.push([i
, pos
, label
]);
153 * Behaves the same way as PlotKit.Layout, but also copies the errors
156 DygraphLayout
.prototype.evaluateWithError
= function() {
158 if (!this.options
.errorBars
) return;
160 // Copy over the error terms
161 var i
= 0; // index in this.points
162 for (var setName
in this.datasets
) {
163 if (!this.datasets
.hasOwnProperty(setName
)) continue;
165 var dataset
= this.datasets
[setName
];
166 for (var j
= 0; j
< dataset
.length
; j
++, i
++) {
167 var item
= dataset
[j
];
168 var xv
= parseFloat(item
[0]);
169 var yv
= parseFloat(item
[1]);
171 if (xv
== this.points
[i
].xval
&&
172 yv
== this.points
[i
].yval
) {
173 this.points
[i
].errorMinus
= parseFloat(item
[2]);
174 this.points
[i
].errorPlus
= parseFloat(item
[3]);
180 DygraphLayout
.prototype._evaluateAnnotations
= function() {
181 // Add the annotations to the point to which they belong.
182 // Make a map from (setName, xval) to annotation for quick lookups.
183 var annotations
= {};
184 for (var i
= 0; i
< this.annotations
.length
; i
++) {
185 var a
= this.annotations
[i
];
186 annotations
[a
.xval
+ "," + a
.series
] = a
;
189 this.annotated_points
= [];
190 for (var i
= 0; i
< this.points
.length
; i
++) {
191 var p
= this.points
[i
];
192 var k
= p
.xval
+ "," + p
.name
;
193 if (k
in annotations
) {
194 p
.annotation
= annotations
[k
];
195 this.annotated_points
.push(p
);
201 * Convenience function to remove all the data sets from a graph
203 DygraphLayout
.prototype.removeAllDatasets
= function() {
204 delete this.datasets
;
205 this.datasets
= new Array();
209 * Change the values of various layout options
210 * @param {Object} new_options an associative array of new properties
212 DygraphLayout
.prototype.updateOptions
= function(new_options
) {
213 Dygraph
.update(this.options
, new_options
? new_options
: {});
217 * Return a copy of the point at the indicated index, with its yval unstacked.
218 * @param int index of point in layout_.points
220 DygraphLayout
.prototype.unstackPointAtIndex
= function(idx
) {
221 var point
= this.points
[idx
];
223 // Clone the point since we modify it
224 var unstackedPoint
= {};
225 for (var i
in point
) {
226 unstackedPoint
[i
] = point
[i
];
229 if (!this.attr_("stackedGraph")) {
230 return unstackedPoint
;
233 // The unstacked yval is equal to the current yval minus the yval of the
234 // next point at the same xval.
235 for (var i
= idx
+1; i
< this.points
.length
; i
++) {
236 if (this.points
[i
].xval
== point
.xval
) {
237 unstackedPoint
.yval
-= this.points
[i
].yval
;
242 return unstackedPoint
;
245 // Subclass PlotKit.CanvasRenderer to add:
246 // 1. X/Y grid overlay
247 // 2. Ability to draw error bars (if required)
250 * Sets some PlotKit.CanvasRenderer options
251 * @param {Object} element The canvas to attach to
252 * @param {Layout} layout The DygraphLayout object for this graph.
253 * @param {Object} options Options to pass on to CanvasRenderer
255 DygraphCanvasRenderer
= function(dygraph
, element
, layout
, options
) {
256 // TODO(danvk): remove options, just use dygraph.attr_.
257 this.dygraph_
= dygraph
;
264 "axisLineColor": "black",
265 "axisLineWidth": 0.5,
267 "axisLabelColor": "black",
268 "axisLabelFont": "Arial",
269 "axisLabelFontSize": 9,
270 "axisLabelWidth": 50,
273 "gridLineColor": "rgb(128,128,128)",
275 "underlayCallback": null
277 Dygraph
.update(this.options
, options
);
279 this.layout
= layout
;
280 this.element
= element
;
281 this.container
= this.element
.parentNode
;
283 this.height
= this.element
.height
;
284 this.width
= this.element
.width
;
286 // --- check whether everything is ok before we return
287 if (!this.isIE
&& !(DygraphCanvasRenderer
.isSupported(this.element
)))
288 throw "Canvas is not supported.";
291 this.xlabels
= new Array();
292 this.ylabels
= new Array();
293 this.annotations
= new Array();
295 // TODO(danvk): consider all axes in this computation.
297 // TODO(danvk): per-axis setting.
298 x
: this.options
.yAxisLabelWidth
+ 2 * this.options
.axisTickSize
,
301 this.area
.w
= this.width
- this.area
.x
- this.options
.rightGap
;
302 this.area
.h
= this.height
- this.options
.axisLabelFontSize
-
303 2 * this.options
.axisTickSize
;
305 // Shrink the drawing area to accomodate additional y-axes.
306 if (this.dygraph_
.numAxes() == 2) {
307 // TODO(danvk): per-axis setting.
308 this.area
.w
-= (this.options
.yAxisLabelWidth
+ 2 * this.options
.axisTickSize
);
309 } else if (this.dygraph_
.numAxes() > 2) {
310 this.dygraph_
.error("Only two y-axes are supported at this time. (Trying " +
311 "to use " + this.dygraph_
.numAxes() + ")");
314 this.container
.style
.position
= "relative";
315 this.container
.style
.width
= this.width
+ "px";
317 // Set up a clipping area for the canvas (and the interaction canvas).
318 // This ensures that we don't overdraw.
319 var ctx
= this.element
.getContext("2d");
321 ctx
.rect(this.area
.x
, this.area
.y
, this.area
.w
, this.area
.h
);
324 var ctx
= this.dygraph_
.hidden_
.getContext("2d");
326 ctx
.rect(this.area
.x
, this.area
.y
, this.area
.w
, this.area
.h
);
330 DygraphCanvasRenderer
.prototype.clear
= function() {
332 // VML takes a while to start up, so we just poll every this.IEDelay
334 if (this.clearDelay
) {
335 this.clearDelay
.cancel();
336 this.clearDelay
= null;
338 var context
= this.element
.getContext("2d");
341 // TODO(danvk): this is broken, since MochiKit.Async is gone.
342 this.clearDelay
= MochiKit
.Async
.wait(this.IEDelay
);
343 this.clearDelay
.addCallback(bind(this.clear
, this));
348 var context
= this.element
.getContext("2d");
349 context
.clearRect(0, 0, this.width
, this.height
);
351 for (var i
= 0; i
< this.xlabels
.length
; i
++) {
352 var el
= this.xlabels
[i
];
353 el
.parentNode
.removeChild(el
);
355 for (var i
= 0; i
< this.ylabels
.length
; i
++) {
356 var el
= this.ylabels
[i
];
357 el
.parentNode
.removeChild(el
);
359 for (var i
= 0; i
< this.annotations
.length
; i
++) {
360 var el
= this.annotations
[i
];
361 el
.parentNode
.removeChild(el
);
363 this.xlabels
= new Array();
364 this.ylabels
= new Array();
365 this.annotations
= new Array();
369 DygraphCanvasRenderer
.isSupported
= function(canvasName
) {
372 if (typeof(canvasName
) == 'undefined' || canvasName
== null)
373 canvas
= document
.createElement("canvas");
376 var context
= canvas
.getContext("2d");
379 var ie
= navigator
.appVersion
.match(/MSIE (\d\.\d)/);
380 var opera
= (navigator
.userAgent
.toLowerCase().indexOf("opera") != -1);
381 if ((!ie
) || (ie
[1] < 6) || (opera
))
389 * Draw an X/Y grid on top of the existing plot
391 DygraphCanvasRenderer
.prototype.render
= function() {
392 // Draw the new X/Y grid
393 var ctx
= this.element
.getContext("2d");
395 if (this.options
.underlayCallback
) {
396 this.options
.underlayCallback(ctx
, this.area
, this.layout
, this.dygraph_
);
399 if (this.options
.drawYGrid
) {
400 var ticks
= this.layout
.yticks
;
402 ctx
.strokeStyle
= this.options
.gridLineColor
;
403 ctx
.lineWidth
= this.options
.axisLineWidth
;
404 for (var i
= 0; i
< ticks
.length
; i
++) {
405 // TODO(danvk): allow secondary axes to draw a grid, too.
406 if (ticks
[i
][0] != 0) continue;
408 var y
= this.area
.y
+ ticks
[i
][1] * this.area
.h
;
411 ctx
.lineTo(x
+ this.area
.w
, y
);
417 if (this.options
.drawXGrid
) {
418 var ticks
= this.layout
.xticks
;
420 ctx
.strokeStyle
= this.options
.gridLineColor
;
421 ctx
.lineWidth
= this.options
.axisLineWidth
;
422 for (var i
=0; i
<ticks
.length
; i
++) {
423 var x
= this.area
.x
+ ticks
[i
][0] * this.area
.w
;
424 var y
= this.area
.y
+ this.area
.h
;
427 ctx
.lineTo(x
, this.area
.y
);
433 // Do the ordinary rendering, as before
434 this._renderLineChart();
436 this._renderAnnotations();
440 DygraphCanvasRenderer
.prototype._renderAxis
= function() {
441 if (!this.options
.drawXAxis
&& !this.options
.drawYAxis
)
444 var context
= this.element
.getContext("2d");
447 "position": "absolute",
448 "fontSize": this.options
.axisLabelFontSize
+ "px",
450 "color": this.options
.axisLabelColor
,
451 "width": this.options
.axisLabelWidth
+ "px",
454 var makeDiv
= function(txt
) {
455 var div
= document
.createElement("div");
456 for (var name
in labelStyle
) {
457 if (labelStyle
.hasOwnProperty(name
)) {
458 div
.style
[name
] = labelStyle
[name
];
461 div
.appendChild(document
.createTextNode(txt
));
467 context
.strokeStyle
= this.options
.axisLineColor
;
468 context
.lineWidth
= this.options
.axisLineWidth
;
470 if (this.options
.drawYAxis
) {
471 if (this.layout
.yticks
&& this.layout
.yticks
.length
> 0) {
472 for (var i
= 0; i
< this.layout
.yticks
.length
; i
++) {
473 var tick
= this.layout
.yticks
[i
];
474 if (typeof(tick
) == "function") return;
477 if (tick
[0] == 1) { // right-side y-axis
478 x
= this.area
.x
+ this.area
.w
;
481 var y
= this.area
.y
+ tick
[1] * this.area
.h
;
483 context
.moveTo(x
, y
);
484 context
.lineTo(x
- sgn
* this.options
.axisTickSize
, y
);
488 var label
= makeDiv(tick
[2]);
489 var top
= (y
- this.options
.axisLabelFontSize
/ 2);
490 if (top
< 0) top
= 0;
492 if (top
+ this.options
.axisLabelFontSize
+ 3 > this.height
) {
493 label
.style
.bottom
= "0px";
495 label
.style
.top
= top
+ "px";
498 label
.style
.left
= "0px";
499 label
.style
.textAlign
= "right";
500 } else if (tick
[0] == 1) {
501 label
.style
.left
= (this.area
.x
+ this.area
.w
+
502 this.options
.axisTickSize
) + "px";
503 label
.style
.textAlign
= "left";
505 label
.style
.width
= this.options
.yAxisLabelWidth
+ "px";
506 this.container
.appendChild(label
);
507 this.ylabels
.push(label
);
510 // The lowest tick on the y-axis often overlaps with the leftmost
511 // tick on the x-axis. Shift the bottom tick up a little bit to
512 // compensate if necessary.
513 var bottomTick
= this.ylabels
[0];
514 var fontSize
= this.options
.axisLabelFontSize
;
515 var bottom
= parseInt(bottomTick
.style
.top
) + fontSize
;
516 if (bottom
> this.height
- fontSize
) {
517 bottomTick
.style
.top
= (parseInt(bottomTick
.style
.top
) -
518 fontSize
/ 2) + "px";
523 context
.moveTo(this.area
.x
, this.area
.y
);
524 context
.lineTo(this.area
.x
, this.area
.y
+ this.area
.h
);
528 if (this.dygraph_
.numAxes() == 2) {
530 context
.moveTo(this.area
.x
+ this.area
.w
, this.area
.y
);
531 context
.lineTo(this.area
.x
+ this.area
.w
, this.area
.y
+ this.area
.h
);
537 if (this.options
.drawXAxis
) {
538 if (this.layout
.xticks
) {
539 for (var i
= 0; i
< this.layout
.xticks
.length
; i
++) {
540 var tick
= this.layout
.xticks
[i
];
541 if (typeof(dataset
) == "function") return;
543 var x
= this.area
.x
+ tick
[0] * this.area
.w
;
544 var y
= this.area
.y
+ this.area
.h
;
546 context
.moveTo(x
, y
);
547 context
.lineTo(x
, y
+ this.options
.axisTickSize
);
551 var label
= makeDiv(tick
[1]);
552 label
.style
.textAlign
= "center";
553 label
.style
.bottom
= "0px";
555 var left
= (x
- this.options
.axisLabelWidth
/2);
556 if (left
+ this.options
.axisLabelWidth
> this.width
) {
557 left
= this.width
- this.options
.xAxisLabelWidth
;
558 label
.style
.textAlign
= "right";
562 label
.style
.textAlign
= "left";
565 label
.style
.left
= left
+ "px";
566 label
.style
.width
= this.options
.xAxisLabelWidth
+ "px";
567 this.container
.appendChild(label
);
568 this.xlabels
.push(label
);
573 context
.moveTo(this.area
.x
, this.area
.y
+ this.area
.h
);
574 context
.lineTo(this.area
.x
+ this.area
.w
, this.area
.y
+ this.area
.h
);
583 DygraphCanvasRenderer
.prototype._renderAnnotations
= function() {
584 var annotationStyle
= {
585 "position": "absolute",
586 "fontSize": this.options
.axisLabelFontSize
+ "px",
591 var bindEvt
= function(eventName
, classEventName
, p
, self
) {
593 var a
= p
.annotation
;
594 if (a
.hasOwnProperty(eventName
)) {
595 a
[eventName
](a
, p
, self
.dygraph_
, e
);
596 } else if (self
.dygraph_
.attr_(classEventName
)) {
597 self
.dygraph_
.attr_(classEventName
)(a
, p
, self
.dygraph_
,e
);
602 // Get a list of point with annotations.
603 var points
= this.layout
.annotated_points
;
604 for (var i
= 0; i
< points
.length
; i
++) {
606 if (p
.canvasx
< this.area
.x
|| p
.canvasx
> this.area
.x
+ this.area
.w
) {
610 var a
= p
.annotation
;
612 if (a
.hasOwnProperty("tickHeight")) {
613 tick_height
= a
.tickHeight
;
616 var div
= document
.createElement("div");
617 for (var name
in annotationStyle
) {
618 if (annotationStyle
.hasOwnProperty(name
)) {
619 div
.style
[name
] = annotationStyle
[name
];
622 if (!a
.hasOwnProperty('icon')) {
623 div
.className
= "dygraphDefaultAnnotation";
625 if (a
.hasOwnProperty('cssClass')) {
626 div
.className
+= " " + a
.cssClass
;
629 var width
= a
.hasOwnProperty('width') ? a
.width
: 16;
630 var height
= a
.hasOwnProperty('height') ? a
.height
: 16;
631 if (a
.hasOwnProperty('icon')) {
632 var img
= document
.createElement("img");
636 div
.appendChild(img
);
637 } else if (p
.annotation
.hasOwnProperty('shortText')) {
638 div
.appendChild(document
.createTextNode(p
.annotation
.shortText
));
640 div
.style
.left
= (p
.canvasx
- width
/ 2) + "px";
641 if (a
.attachAtBottom
) {
642 div
.style
.top
= (this.area
.h
- height
- tick_height
) + "px";
644 div
.style
.top
= (p
.canvasy
- height
- tick_height
) + "px";
646 div
.style
.width
= width
+ "px";
647 div
.style
.height
= height
+ "px";
648 div
.title
= p
.annotation
.text
;
649 div
.style
.color
= this.colors
[p
.name
];
650 div
.style
.borderColor
= this.colors
[p
.name
];
653 Dygraph
.addEvent(div
, 'click',
654 bindEvt('clickHandler', 'annotationClickHandler', p
, this));
655 Dygraph
.addEvent(div
, 'mouseover',
656 bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p
, this));
657 Dygraph
.addEvent(div
, 'mouseout',
658 bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p
, this));
659 Dygraph
.addEvent(div
, 'dblclick',
660 bindEvt('dblClickHandler', 'annotationDblClickHandler', p
, this));
662 this.container
.appendChild(div
);
663 this.annotations
.push(div
);
665 var ctx
= this.element
.getContext("2d");
666 ctx
.strokeStyle
= this.colors
[p
.name
];
668 if (!a
.attachAtBottom
) {
669 ctx
.moveTo(p
.canvasx
, p
.canvasy
);
670 ctx
.lineTo(p
.canvasx
, p
.canvasy
- 2 - tick_height
);
672 ctx
.moveTo(p
.canvasx
, this.area
.h
);
673 ctx
.lineTo(p
.canvasx
, this.area
.h
- 2 - tick_height
);
682 * Overrides the CanvasRenderer method to draw error bars
684 DygraphCanvasRenderer
.prototype._renderLineChart
= function() {
685 var context
= this.element
.getContext("2d");
686 var colorCount
= this.options
.colorScheme
.length
;
687 var colorScheme
= this.options
.colorScheme
;
688 var fillAlpha
= this.options
.fillAlpha
;
689 var errorBars
= this.layout
.options
.errorBars
;
690 var fillGraph
= this.layout
.options
.fillGraph
;
691 var stackedGraph
= this.layout
.options
.stackedGraph
;
692 var stepPlot
= this.layout
.options
.stepPlot
;
695 for (var name
in this.layout
.datasets
) {
696 if (this.layout
.datasets
.hasOwnProperty(name
)) {
700 var setCount
= setNames
.length
;
703 for (var i
= 0; i
< setCount
; i
++) {
704 this.colors
[setNames
[i
]] = colorScheme
[i
% colorCount
];
709 for (var i
= 0; i
< this.layout
.points
.length
; i
++) {
710 var point
= this.layout
.points
[i
];
711 point
.canvasx
= this.area
.w
* point
.x
+ this.area
.x
;
712 point
.canvasy
= this.area
.h
* point
.y
+ this.area
.y
;
716 var isOK
= function(x
) { return x
&& !isNaN(x
); };
721 this.dygraph_
.warn("Can't use fillGraph option with error bars");
724 for (var i
= 0; i
< setCount
; i
++) {
725 var setName
= setNames
[i
];
726 var axis
= this.layout
.options
.yAxes
[
727 this.layout
.options
.seriesToAxisMap
[setName
]];
728 var color
= this.colors
[setName
];
730 // setup graphics context
734 var prevYs
= [-1, -1];
735 var yscale
= axis
.yscale
;
736 // should be same color as the lines but only 15% opaque.
737 var rgb
= new RGBColor(color
);
738 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
740 ctx
.fillStyle
= err_color
;
742 for (var j
= 0; j
< this.layout
.points
.length
; j
++) {
743 var point
= this.layout
.points
[j
];
744 if (point
.name
== setName
) {
745 if (!isOK(point
.y
)) {
752 var newYs
= [ prevY
- point
.errorPlus
* yscale
,
753 prevY
+ point
.errorMinus
* yscale
];
756 var newYs
= [ point
.y
- point
.errorPlus
* yscale
,
757 point
.y
+ point
.errorMinus
* yscale
];
759 newYs
[0] = this.area
.h
* newYs
[0] + this.area
.y
;
760 newYs
[1] = this.area
.h
* newYs
[1] + this.area
.y
;
763 ctx
.moveTo(prevX
, newYs
[0]);
765 ctx
.moveTo(prevX
, prevYs
[0]);
767 ctx
.lineTo(point
.canvasx
, newYs
[0]);
768 ctx
.lineTo(point
.canvasx
, newYs
[1]);
770 ctx
.lineTo(prevX
, newYs
[1]);
772 ctx
.lineTo(prevX
, prevYs
[1]);
777 prevX
= point
.canvasx
;
782 } else if (fillGraph
) {
783 var baseline
= [] // for stacked graphs: baseline for filling
785 // process sets in reverse order (needed for stacked graphs)
786 for (var i
= setCount
- 1; i
>= 0; i
--) {
787 var setName
= setNames
[i
];
788 var color
= this.colors
[setName
];
789 var axis
= this.layout
.options
.yAxes
[
790 this.layout
.options
.seriesToAxisMap
[setName
]];
791 var axisY
= 1.0 + axis
.minyval
* axis
.yscale
;
792 if (axisY
< 0.0) axisY
= 0.0;
793 else if (axisY
> 1.0) axisY
= 1.0;
794 axisY
= this.area
.h
* axisY
+ this.area
.y
;
796 // setup graphics context
799 var prevYs
= [-1, -1];
800 var yscale
= axis
.yscale
;
801 // should be same color as the lines but only 15% opaque.
802 var rgb
= new RGBColor(color
);
803 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
805 ctx
.fillStyle
= err_color
;
807 for (var j
= 0; j
< this.layout
.points
.length
; j
++) {
808 var point
= this.layout
.points
[j
];
809 if (point
.name
== setName
) {
810 if (!isOK(point
.y
)) {
816 lastY
= baseline
[point
.canvasx
];
817 if (lastY
=== undefined
) lastY
= axisY
;
818 baseline
[point
.canvasx
] = point
.canvasy
;
819 newYs
= [ point
.canvasy
, lastY
];
821 newYs
= [ point
.canvasy
, axisY
];
824 ctx
.moveTo(prevX
, prevYs
[0]);
826 ctx
.lineTo(point
.canvasx
, prevYs
[0]);
828 ctx
.lineTo(point
.canvasx
, newYs
[0]);
830 ctx
.lineTo(point
.canvasx
, newYs
[1]);
831 ctx
.lineTo(prevX
, prevYs
[1]);
835 prevX
= point
.canvasx
;
842 for (var i
= 0; i
< setCount
; i
++) {
843 var setName
= setNames
[i
];
844 var color
= this.colors
[setName
];
845 var strokeWidth
= this.dygraph_
.attr_("strokeWidth", setName
);
847 // setup graphics context
849 var point
= this.layout
.points
[0];
850 var pointSize
= this.dygraph_
.attr_("pointSize", setName
);
851 var prevX
= null, prevY
= null;
852 var drawPoints
= this.dygraph_
.attr_("drawPoints", setName
);
853 var points
= this.layout
.points
;
854 for (var j
= 0; j
< points
.length
; j
++) {
855 var point
= points
[j
];
856 if (point
.name
== setName
) {
857 if (!isOK(point
.canvasy
)) {
858 if (stepPlot
&& prevX
!= null) {
859 // Draw a horizontal line to the start of the missing data
861 ctx
.strokeStyle
= color
;
862 ctx
.lineWidth
= this.options
.strokeWidth
;
863 ctx
.moveTo(prevX
, prevY
);
864 ctx
.lineTo(point
.canvasx
, prevY
);
867 // this will make us move to the next point, not draw a line to it.
868 prevX
= prevY
= null;
870 // A point is "isolated" if it is non-null but both the previous
871 // and next points are null.
872 var isIsolated
= (!prevX
&& (j
== points
.length
- 1 ||
873 !isOK(points
[j
+1].canvasy
)));
876 prevX
= point
.canvasx
;
877 prevY
= point
.canvasy
;
879 // TODO(danvk): figure out why this conditional is necessary.
882 ctx
.strokeStyle
= color
;
883 ctx
.lineWidth
= strokeWidth
;
884 ctx
.moveTo(prevX
, prevY
);
886 ctx
.lineTo(point
.canvasx
, prevY
);
888 prevX
= point
.canvasx
;
889 prevY
= point
.canvasy
;
890 ctx
.lineTo(prevX
, prevY
);
895 if (drawPoints
|| isIsolated
) {
897 ctx
.fillStyle
= color
;
898 ctx
.arc(point
.canvasx
, point
.canvasy
, pointSize
,
899 0, 2 * Math
.PI
, false);