1 // Copyright 2006 Dan Vanderkam (danvdk@gmail.com)
2 // All Rights Reserved.
5 * @fileoverview Based on PlotKit.CanvasRenderer, but modified to meet the
8 * In particular, support for:
11 * - dygraphs attribute system
15 * The DygraphCanvasRenderer class does the actual rendering of the chart onto
16 * a canvas. It's based on PlotKit.CanvasRenderer.
17 * @param {Object} element The canvas to attach to
18 * @param {Object} elementContext The 2d context of the canvas (injected so it
19 * can be mocked for testing.)
20 * @param {Layout} layout The DygraphLayout object for this graph.
23 DygraphCanvasRenderer
= function(dygraph
, element
, elementContext
, layout
) {
24 this.dygraph_
= dygraph
;
27 this.element
= element
;
28 this.elementContext
= elementContext
;
29 this.container
= this.element
.parentNode
;
31 this.height
= this.element
.height
;
32 this.width
= this.element
.width
;
34 // --- check whether everything is ok before we return
35 if (!this.isIE
&& !(DygraphCanvasRenderer
.isSupported(this.element
)))
36 throw "Canvas is not supported.";
39 this.xlabels
= new Array();
40 this.ylabels
= new Array();
41 this.annotations
= new Array();
42 this.chartLabels
= {};
44 this.area
= this.computeArea_();
45 this.container
.style
.position
= "relative";
46 this.container
.style
.width
= this.width
+ "px";
48 // Set up a clipping area for the canvas (and the interaction canvas).
49 // This ensures that we don't overdraw.
50 var ctx
= this.dygraph_
.canvas_ctx_
;
52 ctx
.rect(this.area
.x
, this.area
.y
, this.area
.w
, this.area
.h
);
55 ctx
= this.dygraph_
.hidden_ctx_
;
57 ctx
.rect(this.area
.x
, this.area
.y
, this.area
.w
, this.area
.h
);
61 DygraphCanvasRenderer
.prototype.attr_
= function(x
) {
62 return this.dygraph_
.attr_(x
);
65 // Compute the box which the chart should be drawn in. This is the canvas's
66 // box, less space needed for axis and chart labels.
67 // TODO(danvk): this belongs in DygraphLayout.
68 DygraphCanvasRenderer
.prototype.computeArea_
= function() {
70 // TODO(danvk): per-axis setting.
74 if (this.attr_('drawYAxis')) {
75 area
.x
= this.attr_('yAxisLabelWidth') + 2 * this.attr_('axisTickSize');
78 area
.w
= this.width
- area
.x
- this.attr_('rightGap');
80 if (this.attr_('drawXAxis')) {
81 if (this.attr_('xAxisHeight')) {
82 area
.h
-= this.attr_('xAxisHeight');
84 area
.h
-= this.attr_('axisLabelFontSize') + 2 * this.attr_('axisTickSize');
88 // Shrink the drawing area to accomodate additional y-axes.
89 if (this.dygraph_
.numAxes() == 2) {
90 // TODO(danvk): per-axis setting.
91 area
.w
-= (this.attr_('yAxisLabelWidth') + 2 * this.attr_('axisTickSize'));
92 } else if (this.dygraph_
.numAxes() > 2) {
93 this.dygraph_
.error("Only two y-axes are supported at this time. (Trying " +
94 "to use " + this.dygraph_
.numAxes() + ")");
97 // Add space for chart labels: title, xlabel and ylabel.
98 if (this.attr_('title')) {
99 area
.h
-= this.attr_('titleHeight');
100 area
.y
+= this.attr_('titleHeight');
102 if (this.attr_('xlabel')) {
103 area
.h
-= this.attr_('xLabelHeight');
105 if (this.attr_('ylabel')) {
106 // It would make sense to shift the chart here to make room for the y-axis
107 // label, but the default yAxisLabelWidth is large enough that this results
108 // in overly-padded charts. The y-axis label should fit fine. If it
109 // doesn't, the yAxisLabelWidth option can be increased.
115 DygraphCanvasRenderer
.prototype.clear
= function() {
117 // VML takes a while to start up, so we just poll every this.IEDelay
119 if (this.clearDelay
) {
120 this.clearDelay
.cancel();
121 this.clearDelay
= null;
123 var context
= this.elementContext
;
126 // TODO(danvk): this is broken, since MochiKit.Async is gone.
127 this.clearDelay
= MochiKit
.Async
.wait(this.IEDelay
);
128 this.clearDelay
.addCallback(bind(this.clear
, this));
133 var context
= this.elementContext
;
134 context
.clearRect(0, 0, this.width
, this.height
);
136 for (var i
= 0; i
< this.xlabels
.length
; i
++) {
137 var el
= this.xlabels
[i
];
138 if (el
.parentNode
) el
.parentNode
.removeChild(el
);
140 for (var i
= 0; i
< this.ylabels
.length
; i
++) {
141 var el
= this.ylabels
[i
];
142 if (el
.parentNode
) el
.parentNode
.removeChild(el
);
144 for (var i
= 0; i
< this.annotations
.length
; i
++) {
145 var el
= this.annotations
[i
];
146 if (el
.parentNode
) el
.parentNode
.removeChild(el
);
148 for (var k
in this.chartLabels
) {
149 if (!this.chartLabels
.hasOwnProperty(k
)) continue;
150 var el
= this.chartLabels
[k
];
151 if (el
.parentNode
) el
.parentNode
.removeChild(el
);
153 this.xlabels
= new Array();
154 this.ylabels
= new Array();
155 this.annotations
= new Array();
156 this.chartLabels
= {};
160 DygraphCanvasRenderer
.isSupported
= function(canvasName
) {
163 if (typeof(canvasName
) == 'undefined' || canvasName
== null)
164 canvas
= document
.createElement("canvas");
167 var context
= canvas
.getContext("2d");
170 var ie
= navigator
.appVersion
.match(/MSIE (\d\.\d)/);
171 var opera
= (navigator
.userAgent
.toLowerCase().indexOf("opera") != -1);
172 if ((!ie
) || (ie
[1] < 6) || (opera
))
180 * @param { [String] } colors Array of color strings. Should have one entry for
181 * each series to be rendered.
183 DygraphCanvasRenderer
.prototype.setColors
= function(colors
) {
184 this.colorScheme_
= colors
;
188 * Draw an X/Y grid on top of the existing plot
190 DygraphCanvasRenderer
.prototype.render
= function() {
191 // Draw the new X/Y grid
. Lines appear crisper when pixels are rounded to
192 // half-integers. This prevents them from drawing in two rows/cols.
193 var ctx
= this.elementContext
;
194 function halfUp(x
){return Math
.round(x
)+0.5};
195 function halfDown(y
){return Math
.round(y
)-0.5};
197 if (this.attr_('underlayCallback')) {
198 // NOTE: we pass the dygraph object to this callback twice to avoid breaking
199 // users who expect a deprecated form of this callback.
200 this.attr_('underlayCallback')(ctx
, this.area
, this.dygraph_
, this.dygraph_
);
203 if (this.attr_('drawYGrid')) {
204 var ticks
= this.layout
.yticks
;
206 ctx
.strokeStyle
= this.attr_('gridLineColor');
207 ctx
.lineWidth
= this.attr_('gridLineWidth');
208 for (var i
= 0; i
< ticks
.length
; i
++) {
209 // TODO(danvk): allow secondary axes to draw a grid, too.
210 if (ticks
[i
][0] != 0) continue;
211 var x
= halfUp(this.area
.x
);
212 var y
= halfDown(this.area
.y
+ ticks
[i
][1] * this.area
.h
);
215 ctx
.lineTo(x
+ this.area
.w
, y
);
221 if (this.attr_('drawXGrid')) {
222 var ticks
= this.layout
.xticks
;
224 ctx
.strokeStyle
= this.attr_('gridLineColor');
225 ctx
.lineWidth
= this.attr_('gridLineWidth');
226 for (var i
=0; i
<ticks
.length
; i
++) {
227 var x
= halfUp(this.area
.x
+ ticks
[i
][0] * this.area
.w
);
228 var y
= halfDown(this.area
.y
+ this.area
.h
);
231 ctx
.lineTo(x
, this.area
.y
);
237 // Do the ordinary rendering, as before
238 this._renderLineChart();
240 this._renderChartLabels();
241 this._renderAnnotations();
245 DygraphCanvasRenderer
.prototype._renderAxis
= function() {
246 if (!this.attr_('drawXAxis') && !this.attr_('drawYAxis')) return;
248 // Round pixels to half-integer boundaries for crisper drawing.
249 function halfUp(x
){return Math
.round(x
)+0.5};
250 function halfDown(y
){return Math
.round(y
)-0.5};
252 var context
= this.elementContext
;
255 position
: "absolute",
256 fontSize
: this.attr_('axisLabelFontSize') + "px",
258 color
: this.attr_('axisLabelColor'),
259 width
: this.attr_('axisLabelWidth') + "px",
260 // height: this.attr_('axisLabelFontSize') + 2 + "px",
263 var makeDiv
= function(txt
, axis
) {
264 var div
= document
.createElement("div");
265 for (var name
in labelStyle
) {
266 if (labelStyle
.hasOwnProperty(name
)) {
267 div
.style
[name
] = labelStyle
[name
];
270 var inner_div
= document
.createElement("div");
271 // TODO(danvk): separate class for secondary y-axis
272 inner_div
.className
= 'dygraph-axis-label dygraph-axis-label-' + axis
;
273 inner_div
.appendChild(document
.createTextNode(txt
));
274 div
.appendChild(inner_div
);
280 context
.strokeStyle
= this.attr_('axisLineColor');
281 context
.lineWidth
= this.attr_('axisLineWidth');
283 if (this.attr_('drawYAxis')) {
284 if (this.layout
.yticks
&& this.layout
.yticks
.length
> 0) {
285 for (var i
= 0; i
< this.layout
.yticks
.length
; i
++) {
286 var tick
= this.layout
.yticks
[i
];
287 if (typeof(tick
) == "function") return;
290 if (tick
[0] == 1) { // right-side y-axis
291 x
= this.area
.x
+ this.area
.w
;
294 var y
= this.area
.y
+ tick
[1] * this.area
.h
;
296 context
.moveTo(halfUp(x
), halfDown(y
));
297 context
.lineTo(halfUp(x
- sgn
* this.attr_('axisTickSize')), halfDown(y
));
301 var label
= makeDiv(tick
[2], 'y');
302 var top
= (y
- this.attr_('axisLabelFontSize') / 2);
303 if (top
< 0) top
= 0;
305 if (top
+ this.attr_('axisLabelFontSize') + 3 > this.height
) {
306 label
.style
.bottom
= "0px";
308 label
.style
.top
= top
+ "px";
311 label
.style
.left
= (this.area
.x
- this.attr_('yAxisLabelWidth') - this.attr_('axisTickSize')) + "px";
312 label
.style
.textAlign
= "right";
313 } else if (tick
[0] == 1) {
314 label
.style
.left
= (this.area
.x
+ this.area
.w
+
315 this.attr_('axisTickSize')) + "px";
316 label
.style
.textAlign
= "left";
318 label
.style
.width
= this.attr_('yAxisLabelWidth') + "px";
319 this.container
.appendChild(label
);
320 this.ylabels
.push(label
);
323 // The lowest tick on the y-axis often overlaps with the leftmost
324 // tick on the x-axis. Shift the bottom tick up a little bit to
325 // compensate if necessary.
326 var bottomTick
= this.ylabels
[0];
327 var fontSize
= this.attr_('axisLabelFontSize');
328 var bottom
= parseInt(bottomTick
.style
.top
) + fontSize
;
329 if (bottom
> this.height
- fontSize
) {
330 bottomTick
.style
.top
= (parseInt(bottomTick
.style
.top
) -
331 fontSize
/ 2) + "px";
335 // draw a vertical line on the left to separate the chart from the labels.
337 context
.moveTo(halfUp(this.area
.x
), halfDown(this.area
.y
));
338 context
.lineTo(halfUp(this.area
.x
), halfDown(this.area
.y
+ this.area
.h
));
342 // if there's a secondary y-axis, draw a vertical line for that, too.
343 if (this.dygraph_
.numAxes() == 2) {
345 context
.moveTo(halfDown(this.area
.x
+ this.area
.w
), halfDown(this.area
.y
));
346 context
.lineTo(halfDown(this.area
.x
+ this.area
.w
), halfDown(this.area
.y
+ this.area
.h
));
352 if (this.attr_('drawXAxis')) {
353 if (this.layout
.xticks
) {
354 for (var i
= 0; i
< this.layout
.xticks
.length
; i
++) {
355 var tick
= this.layout
.xticks
[i
];
356 if (typeof(dataset
) == "function") return;
358 var x
= this.area
.x
+ tick
[0] * this.area
.w
;
359 var y
= this.area
.y
+ this.area
.h
;
361 context
.moveTo(halfUp(x
), halfDown(y
));
362 context
.lineTo(halfUp(x
), halfDown(y
+ this.attr_('axisTickSize')));
366 var label
= makeDiv(tick
[1], 'x');
367 label
.style
.textAlign
= "center";
368 label
.style
.top
= (y
+ this.attr_('axisTickSize')) + 'px';
370 var left
= (x
- this.attr_('axisLabelWidth')/2);
371 if (left
+ this.attr_('axisLabelWidth') > this.width
) {
372 left
= this.width
- this.attr_('xAxisLabelWidth');
373 label
.style
.textAlign
= "right";
377 label
.style
.textAlign
= "left";
380 label
.style
.left
= left
+ "px";
381 label
.style
.width
= this.attr_('xAxisLabelWidth') + "px";
382 this.container
.appendChild(label
);
383 this.xlabels
.push(label
);
388 context
.moveTo(halfUp(this.area
.x
), halfDown(this.area
.y
+ this.area
.h
));
389 context
.lineTo(halfUp(this.area
.x
+ this.area
.w
), halfDown(this.area
.y
+ this.area
.h
));
398 DygraphCanvasRenderer
.prototype._renderChartLabels
= function() {
399 // Generate divs for the chart title, xlabel and ylabel.
400 // Space for these divs has already been taken away from the charting area in
401 // the DygraphCanvasRenderer constructor.
402 if (this.attr_('title')) {
403 var div
= document
.createElement("div");
404 div
.style
.position
= 'absolute';
405 div
.style
.top
= '0px';
406 div
.style
.left
= this.area
.x
+ 'px';
407 div
.style
.width
= this.area
.w
+ 'px';
408 div
.style
.height
= this.attr_('titleHeight') + 'px';
409 div
.style
.textAlign
= 'center';
410 div
.style
.fontSize
= (this.attr_('titleHeight') - 8) + 'px';
411 div
.style
.fontWeight
= 'bold';
412 var class_div
= document
.createElement("div");
413 class_div
.className
= 'dygraph-label dygraph-title';
414 class_div
.innerHTML
= this.attr_('title');
415 div
.appendChild(class_div
);
416 this.container
.appendChild(div
);
417 this.chartLabels
.title
= div
;
420 if (this.attr_('xlabel')) {
421 var div
= document
.createElement("div");
422 div
.style
.position
= 'absolute';
423 div
.style
.bottom
= 0; // TODO(danvk): this is lazy. Calculate style.top.
424 div
.style
.left
= this.area
.x
+ 'px';
425 div
.style
.width
= this.area
.w
+ 'px';
426 div
.style
.height
= this.attr_('xLabelHeight') + 'px';
427 div
.style
.textAlign
= 'center';
428 div
.style
.fontSize
= (this.attr_('xLabelHeight') - 2) + 'px';
430 var class_div
= document
.createElement("div");
431 class_div
.className
= 'dygraph-label dygraph-xlabel';
432 class_div
.innerHTML
= this.attr_('xlabel');
433 div
.appendChild(class_div
);
434 this.container
.appendChild(div
);
435 this.chartLabels
.xlabel
= div
;
438 if (this.attr_('ylabel')) {
442 width
: this.attr_('yLabelWidth'),
445 // TODO(danvk): is this outer div actually necessary?
446 var div
= document
.createElement("div");
447 div
.style
.position
= 'absolute';
448 div
.style
.left
= box
.left
;
449 div
.style
.top
= box
.top
+ 'px';
450 div
.style
.width
= box
.width
+ 'px';
451 div
.style
.height
= box
.height
+ 'px';
452 div
.style
.fontSize
= (this.attr_('yLabelWidth') - 2) + 'px';
454 var inner_div
= document
.createElement("div");
455 inner_div
.style
.position
= 'absolute';
456 inner_div
.style
.width
= box
.height
+ 'px';
457 inner_div
.style
.height
= box
.width
+ 'px';
458 inner_div
.style
.top
= (box
.height
/ 2 - box.width / 2) + 'px';
459 inner_div
.style
.left
= (box
.width
/ 2 - box.height / 2) + 'px';
460 inner_div
.style
.textAlign
= 'center';
462 // CSS rotation is an HTML5 feature which is not standardized. Hence every
463 // browser has its own name for the CSS style.
464 inner_div
.style
.transform
= 'rotate(-90deg)'; // HTML5
465 inner_div
.style
.WebkitTransform
= 'rotate(-90deg)'; // Safari/Chrome
466 inner_div
.style
.MozTransform
= 'rotate(-90deg)'; // Firefox
467 inner_div
.style
.OTransform
= 'rotate(-90deg)'; // Opera
468 inner_div
.style
.msTransform
= 'rotate(-90deg)'; // IE9
470 if (typeof(document
.documentMode
) !== 'undefined' &&
471 document
.documentMode
< 9) {
472 // We're dealing w/ an old version of IE
, so we have to rotate the text
473 // using a BasicImage transform. This uses a different origin of rotation
474 // than HTML5 rotation (top left of div vs. its center).
475 inner_div
.style
.filter
=
476 'progid:DXImageTransform.Microsoft.BasicImage(rotation=3)';
477 inner_div
.style
.left
= '0px';
478 inner_div
.style
.top
= '0px';
481 var class_div
= document
.createElement("div");
482 class_div
.className
= 'dygraph-label dygraph-ylabel';
483 class_div
.innerHTML
= this.attr_('ylabel');
485 inner_div
.appendChild(class_div
);
486 div
.appendChild(inner_div
);
487 this.container
.appendChild(div
);
488 this.chartLabels
.ylabel
= div
;
493 DygraphCanvasRenderer
.prototype._renderAnnotations
= function() {
494 var annotationStyle
= {
495 "position": "absolute",
496 "fontSize": this.attr_('axisLabelFontSize') + "px",
501 var bindEvt
= function(eventName
, classEventName
, p
, self
) {
503 var a
= p
.annotation
;
504 if (a
.hasOwnProperty(eventName
)) {
505 a
[eventName
](a
, p
, self
.dygraph_
, e
);
506 } else if (self
.dygraph_
.attr_(classEventName
)) {
507 self
.dygraph_
.attr_(classEventName
)(a
, p
, self
.dygraph_
,e
);
512 // Get a list of point with annotations.
513 var points
= this.layout
.annotated_points
;
514 for (var i
= 0; i
< points
.length
; i
++) {
516 if (p
.canvasx
< this.area
.x
|| p
.canvasx
> this.area
.x
+ this.area
.w
) {
520 var a
= p
.annotation
;
522 if (a
.hasOwnProperty("tickHeight")) {
523 tick_height
= a
.tickHeight
;
526 var div
= document
.createElement("div");
527 for (var name
in annotationStyle
) {
528 if (annotationStyle
.hasOwnProperty(name
)) {
529 div
.style
[name
] = annotationStyle
[name
];
532 if (!a
.hasOwnProperty('icon')) {
533 div
.className
= "dygraphDefaultAnnotation";
535 if (a
.hasOwnProperty('cssClass')) {
536 div
.className
+= " " + a
.cssClass
;
539 var width
= a
.hasOwnProperty('width') ? a
.width
: 16;
540 var height
= a
.hasOwnProperty('height') ? a
.height
: 16;
541 if (a
.hasOwnProperty('icon')) {
542 var img
= document
.createElement("img");
546 div
.appendChild(img
);
547 } else if (p
.annotation
.hasOwnProperty('shortText')) {
548 div
.appendChild(document
.createTextNode(p
.annotation
.shortText
));
550 div
.style
.left
= (p
.canvasx
- width
/ 2) + "px";
551 if (a
.attachAtBottom
) {
552 div
.style
.top
= (this.area
.h
- height
- tick_height
) + "px";
554 div
.style
.top
= (p
.canvasy
- height
- tick_height
) + "px";
556 div
.style
.width
= width
+ "px";
557 div
.style
.height
= height
+ "px";
558 div
.title
= p
.annotation
.text
;
559 div
.style
.color
= this.colors
[p
.name
];
560 div
.style
.borderColor
= this.colors
[p
.name
];
563 Dygraph
.addEvent(div
, 'click',
564 bindEvt('clickHandler', 'annotationClickHandler', p
, this));
565 Dygraph
.addEvent(div
, 'mouseover',
566 bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p
, this));
567 Dygraph
.addEvent(div
, 'mouseout',
568 bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p
, this));
569 Dygraph
.addEvent(div
, 'dblclick',
570 bindEvt('dblClickHandler', 'annotationDblClickHandler', p
, this));
572 this.container
.appendChild(div
);
573 this.annotations
.push(div
);
575 var ctx
= this.elementContext
;
576 ctx
.strokeStyle
= this.colors
[p
.name
];
578 if (!a
.attachAtBottom
) {
579 ctx
.moveTo(p
.canvasx
, p
.canvasy
);
580 ctx
.lineTo(p
.canvasx
, p
.canvasy
- 2 - tick_height
);
582 ctx
.moveTo(p
.canvasx
, this.area
.h
);
583 ctx
.lineTo(p
.canvasx
, this.area
.h
- 2 - tick_height
);
592 * Overrides the CanvasRenderer method to draw error bars
594 DygraphCanvasRenderer
.prototype._renderLineChart
= function() {
595 // TODO(danvk): use this.attr_ for many of these.
596 var context
= this.elementContext
;
597 var fillAlpha
= this.attr_('fillAlpha');
598 var errorBars
= this.attr_("errorBars") || this.attr_("customBars");
599 var fillGraph
= this.attr_("fillGraph");
600 var stackedGraph
= this.attr_("stackedGraph");
601 var stepPlot
= this.attr_("stepPlot");
604 for (var name
in this.layout
.datasets
) {
605 if (this.layout
.datasets
.hasOwnProperty(name
)) {
609 var setCount
= setNames
.length
;
611 // TODO(danvk): Move this mapping into Dygraph and get it out of here.
613 for (var i
= 0; i
< setCount
; i
++) {
614 this.colors
[setNames
[i
]] = this.colorScheme_
[i
% this.colorScheme_
.length
];
619 for (var i
= 0; i
< this.layout
.points
.length
; i
++) {
620 var point
= this.layout
.points
[i
];
621 point
.canvasx
= this.area
.w
* point
.x
+ this.area
.x
;
622 point
.canvasy
= this.area
.h
* point
.y
+ this.area
.y
;
629 this.dygraph_
.warn("Can't use fillGraph option with error bars");
632 for (var i
= 0; i
< setCount
; i
++) {
633 var setName
= setNames
[i
];
634 var axis
= this.dygraph_
.axisPropertiesForSeries(setName
);
635 var color
= this.colors
[setName
];
637 // setup graphics context
641 var prevYs
= [-1, -1];
642 var yscale
= axis
.yscale
;
643 // should be same color as the lines but only 15% opaque.
644 var rgb
= new RGBColor(color
);
645 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
647 ctx
.fillStyle
= err_color
;
649 for (var j
= 0; j
< this.layout
.points
.length
; j
++) {
650 var point
= this.layout
.points
[j
];
651 if (point
.name
== setName
) {
652 if (!Dygraph
.isOK(point
.y
)) {
659 var newYs
= [ prevY
- point
.errorPlus
* yscale
,
660 prevY
+ point
.errorMinus
* yscale
];
663 var newYs
= [ point
.y
- point
.errorPlus
* yscale
,
664 point
.y
+ point
.errorMinus
* yscale
];
666 newYs
[0] = this.area
.h
* newYs
[0] + this.area
.y
;
667 newYs
[1] = this.area
.h
* newYs
[1] + this.area
.y
;
670 ctx
.moveTo(prevX
, newYs
[0]);
672 ctx
.moveTo(prevX
, prevYs
[0]);
674 ctx
.lineTo(point
.canvasx
, newYs
[0]);
675 ctx
.lineTo(point
.canvasx
, newYs
[1]);
677 ctx
.lineTo(prevX
, newYs
[1]);
679 ctx
.lineTo(prevX
, prevYs
[1]);
684 prevX
= point
.canvasx
;
689 } else if (fillGraph
) {
690 var baseline
= [] // for stacked graphs: baseline for filling
692 // process sets in reverse order (needed for stacked graphs)
693 for (var i
= setCount
- 1; i
>= 0; i
--) {
694 var setName
= setNames
[i
];
695 var color
= this.colors
[setName
];
696 var axis
= this.dygraph_
.axisPropertiesForSeries(setName
);
697 var axisY
= 1.0 + axis
.minyval
* axis
.yscale
;
698 if (axisY
< 0.0) axisY
= 0.0;
699 else if (axisY
> 1.0) axisY
= 1.0;
700 axisY
= this.area
.h
* axisY
+ this.area
.y
;
702 // setup graphics context
705 var prevYs
= [-1, -1];
706 var yscale
= axis
.yscale
;
707 // should be same color as the lines but only 15% opaque.
708 var rgb
= new RGBColor(color
);
709 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
711 ctx
.fillStyle
= err_color
;
713 for (var j
= 0; j
< this.layout
.points
.length
; j
++) {
714 var point
= this.layout
.points
[j
];
715 if (point
.name
== setName
) {
716 if (!Dygraph
.isOK(point
.y
)) {
722 lastY
= baseline
[point
.canvasx
];
723 if (lastY
=== undefined
) lastY
= axisY
;
724 baseline
[point
.canvasx
] = point
.canvasy
;
725 newYs
= [ point
.canvasy
, lastY
];
727 newYs
= [ point
.canvasy
, axisY
];
730 ctx
.moveTo(prevX
, prevYs
[0]);
732 ctx
.lineTo(point
.canvasx
, prevYs
[0]);
734 ctx
.lineTo(point
.canvasx
, newYs
[0]);
736 ctx
.lineTo(point
.canvasx
, newYs
[1]);
737 ctx
.lineTo(prevX
, prevYs
[1]);
741 prevX
= point
.canvasx
;
748 var isNullOrNaN
= function(x
) {
749 return (x
=== null || isNaN(x
));
752 for (var i
= 0; i
< setCount
; i
++) {
753 var setName
= setNames
[i
];
754 var color
= this.colors
[setName
];
755 var strokeWidth
= this.dygraph_
.attr_("strokeWidth", setName
);
757 // setup graphics context
759 var point
= this.layout
.points
[0];
760 var pointSize
= this.dygraph_
.attr_("pointSize", setName
);
761 var prevX
= null, prevY
= null;
762 var drawPoints
= this.dygraph_
.attr_("drawPoints", setName
);
763 var points
= this.layout
.points
;
764 for (var j
= 0; j
< points
.length
; j
++) {
765 var point
= points
[j
];
766 if (point
.name
== setName
) {
767 if (isNullOrNaN(point
.canvasy
)) {
768 if (stepPlot
&& prevX
!= null) {
769 // Draw a horizontal line to the start of the missing data
771 ctx
.strokeStyle
= color
;
772 ctx
.lineWidth
= this.attr_('strokeWidth');
773 ctx
.moveTo(prevX
, prevY
);
774 ctx
.lineTo(point
.canvasx
, prevY
);
777 // this will make us move to the next point, not draw a line to it.
778 prevX
= prevY
= null;
780 // A point is "isolated" if it is non-null but both the previous
781 // and next points are null.
782 var isIsolated
= (!prevX
&& (j
== points
.length
- 1 ||
783 isNullOrNaN(points
[j
+1].canvasy
)));
785 if (prevX
=== null) {
786 prevX
= point
.canvasx
;
787 prevY
= point
.canvasy
;
789 // TODO(danvk): figure out why this conditional is necessary.
792 ctx
.strokeStyle
= color
;
793 ctx
.lineWidth
= strokeWidth
;
794 ctx
.moveTo(prevX
, prevY
);
796 ctx
.lineTo(point
.canvasx
, prevY
);
798 prevX
= point
.canvasx
;
799 prevY
= point
.canvasy
;
800 ctx
.lineTo(prevX
, prevY
);
805 if (drawPoints
|| isIsolated
) {
807 ctx
.fillStyle
= color
;
808 ctx
.arc(point
.canvasx
, point
.canvasy
, pointSize
,
809 0, 2 * Math
.PI
, false);