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();
24 DygraphLayout
.prototype.attr_
= function(name
) {
25 return this.dygraph_
.attr_(name
);
28 DygraphLayout
.prototype.addDataset
= function(setname
, set_xy
) {
29 this.datasets
[setname
] = set_xy
;
32 DygraphLayout
.prototype.evaluate
= function() {
33 this._evaluateLimits();
34 this._evaluateLineCharts();
35 this._evaluateLineTicks();
38 DygraphLayout
.prototype._evaluateLimits
= function() {
39 this.minxval
= this.maxxval
= null;
40 if (this.options
.dateWindow
) {
41 this.minxval
= this.options
.dateWindow
[0];
42 this.maxxval
= this.options
.dateWindow
[1];
44 for (var name
in this.datasets
) {
45 if (!this.datasets
.hasOwnProperty(name
)) continue;
46 var series
= this.datasets
[name
];
47 var x1
= series
[0][0];
48 if (!this.minxval
|| x1
< this.minxval
) this.minxval
= x1
;
50 var x2
= series
[series
.length
- 1][0];
51 if (!this.maxxval
|| x2
> this.maxxval
) this.maxxval
= x2
;
54 this.xrange
= this.maxxval
- this.minxval
;
55 this.xscale
= (this.xrange
!= 0 ? 1/this.xrange
: 1.0);
57 this.minyval
= this.options
.yAxis
[0];
58 this.maxyval
= this.options
.yAxis
[1];
59 this.yrange
= this.maxyval
- this.minyval
;
60 this.yscale
= (this.yrange
!= 0 ? 1/this.yrange
: 1.0);
63 DygraphLayout
.prototype._evaluateLineCharts
= function() {
65 this.points
= new Array();
66 for (var setName
in this.datasets
) {
67 if (!this.datasets
.hasOwnProperty(setName
)) continue;
69 var dataset
= this.datasets
[setName
];
70 for (var j
= 0; j
< dataset
.length
; j
++) {
71 var item
= dataset
[j
];
74 x
: ((parseFloat(item
[0]) - this.minxval
) * this.xscale
),
75 y
: 1.0 - ((parseFloat(item
[1]) - this.minyval
) * this.yscale
),
76 xval
: parseFloat(item
[0]),
77 yval
: parseFloat(item
[1]),
81 // limit the x, y values so they do not overdraw
88 this.points
.push(point
);
93 DygraphLayout
.prototype._evaluateLineTicks
= function() {
94 this.xticks
= new Array();
95 for (var i
= 0; i
< this.options
.xTicks
.length
; i
++) {
96 var tick
= this.options
.xTicks
[i
];
97 var label
= tick
.label
;
98 var pos
= this.xscale
* (tick
.v
- this.minxval
);
99 if ((pos
>= 0.0) && (pos
<= 1.0)) {
100 this.xticks
.push([pos
, label
]);
104 this.yticks
= new Array();
105 for (var i
= 0; i
< this.options
.yTicks
.length
; i
++) {
106 var tick
= this.options
.yTicks
[i
];
107 var label
= tick
.label
;
108 var pos
= 1.0 - (this.yscale
* (tick
.v
- this.minyval
));
109 if ((pos
>= 0.0) && (pos
<= 1.0)) {
110 this.yticks
.push([pos
, label
]);
117 * Behaves the same way as PlotKit.Layout, but also copies the errors
120 DygraphLayout
.prototype.evaluateWithError
= function() {
122 if (!this.options
.errorBars
) return;
124 // Copy over the error terms
125 var i
= 0; // index in this.points
126 for (var setName
in this.datasets
) {
127 if (!this.datasets
.hasOwnProperty(setName
)) continue;
129 var dataset
= this.datasets
[setName
];
130 for (var j
= 0; j
< dataset
.length
; j
++, i
++) {
131 var item
= dataset
[j
];
132 var xv
= parseFloat(item
[0]);
133 var yv
= parseFloat(item
[1]);
135 if (xv
== this.points
[i
].xval
&&
136 yv
== this.points
[i
].yval
) {
137 this.points
[i
].errorMinus
= parseFloat(item
[2]);
138 this.points
[i
].errorPlus
= parseFloat(item
[3]);
145 * Convenience function to remove all the data sets from a graph
147 DygraphLayout
.prototype.removeAllDatasets
= function() {
148 delete this.datasets
;
149 this.datasets
= new Array();
153 * Change the values of various layout options
154 * @param {Object} new_options an associative array of new properties
156 DygraphLayout
.prototype.updateOptions
= function(new_options
) {
157 Dygraph
.update(this.options
, new_options
? new_options
: {});
160 // Subclass PlotKit.CanvasRenderer to add:
161 // 1. X/Y grid overlay
162 // 2. Ability to draw error bars (if required)
165 * Sets some PlotKit.CanvasRenderer options
166 * @param {Object} element The canvas to attach to
167 * @param {Layout} layout The DygraphLayout object for this graph.
168 * @param {Object} options Options to pass on to CanvasRenderer
170 DygraphCanvasRenderer
= function(dygraph
, element
, layout
, options
) {
171 // TODO(danvk): remove options, just use dygraph.attr_.
172 this.dygraph_
= dygraph
;
179 "axisLineColor": "black",
180 "axisLineWidth": 0.5,
182 "axisLabelColor": "black",
183 "axisLabelFont": "Arial",
184 "axisLabelFontSize": 9,
185 "axisLabelWidth": 50,
188 "gridLineColor": "rgb(128,128,128)",
190 "underlayCallback": null
192 Dygraph
.update(this.options
, options
);
194 this.layout
= layout
;
195 this.element
= element
;
196 this.container
= this.element
.parentNode
;
198 this.height
= this.element
.height
;
199 this.width
= this.element
.width
;
201 // --- check whether everything is ok before we return
202 if (!this.isIE
&& !(DygraphCanvasRenderer
.isSupported(this.element
)))
203 throw "Canvas is not supported.";
206 this.xlabels
= new Array();
207 this.ylabels
= new Array();
210 x
: this.options
.yAxisLabelWidth
+ 2 * this.options
.axisTickSize
,
213 this.area
.w
= this.width
- this.area
.x
- this.options
.rightGap
;
214 this.area
.h
= this.height
- this.options
.axisLabelFontSize
-
215 2 * this.options
.axisTickSize
;
217 this.container
.style
.position
= "relative";
218 this.container
.style
.width
= this.width
+ "px";
221 DygraphCanvasRenderer
.prototype.clear
= function() {
223 // VML takes a while to start up, so we just poll every this.IEDelay
225 if (this.clearDelay
) {
226 this.clearDelay
.cancel();
227 this.clearDelay
= null;
229 var context
= this.element
.getContext("2d");
232 // TODO(danvk): this is broken, since MochiKit.Async is gone.
233 this.clearDelay
= MochiKit
.Async
.wait(this.IEDelay
);
234 this.clearDelay
.addCallback(bind(this.clear
, this));
239 var context
= this.element
.getContext("2d");
240 context
.clearRect(0, 0, this.width
, this.height
);
242 for (var i
= 0; i
< this.xlabels
.length
; i
++) {
243 var el
= this.xlabels
[i
];
244 el
.parentNode
.removeChild(el
);
246 for (var i
= 0; i
< this.ylabels
.length
; i
++) {
247 var el
= this.ylabels
[i
];
248 el
.parentNode
.removeChild(el
);
250 this.xlabels
= new Array();
251 this.ylabels
= new Array();
255 DygraphCanvasRenderer
.isSupported
= function(canvasName
) {
258 if (typeof(canvasName
) == 'undefined' || canvasName
== null)
259 canvas
= document
.createElement("canvas");
262 var context
= canvas
.getContext("2d");
265 var ie
= navigator
.appVersion
.match(/MSIE (\d\.\d)/);
266 var opera
= (navigator
.userAgent
.toLowerCase().indexOf("opera") != -1);
267 if ((!ie
) || (ie
[1] < 6) || (opera
))
275 * Draw an X/Y grid on top of the existing plot
277 DygraphCanvasRenderer
.prototype.render
= function() {
278 // Draw the new X/Y grid
279 var ctx
= this.element
.getContext("2d");
281 if (this.options
.underlayCallback
) {
282 this.options
.underlayCallback(ctx
, this.area
, this.layout
);
285 if (this.options
.drawYGrid
) {
286 var ticks
= this.layout
.yticks
;
288 ctx
.strokeStyle
= this.options
.gridLineColor
;
289 ctx
.lineWidth
= this.options
.axisLineWidth
;
290 for (var i
= 0; i
< ticks
.length
; i
++) {
292 var y
= this.area
.y
+ ticks
[i
][0] * this.area
.h
;
295 ctx
.lineTo(x
+ this.area
.w
, y
);
301 if (this.options
.drawXGrid
) {
302 var ticks
= this.layout
.xticks
;
304 ctx
.strokeStyle
= this.options
.gridLineColor
;
305 ctx
.lineWidth
= this.options
.axisLineWidth
;
306 for (var i
=0; i
<ticks
.length
; i
++) {
307 var x
= this.area
.x
+ ticks
[i
][0] * this.area
.w
;
308 var y
= this.area
.y
+ this.area
.h
;
311 ctx
.lineTo(x
, this.area
.y
);
317 // Do the ordinary rendering, as before
318 this._renderLineChart();
323 DygraphCanvasRenderer
.prototype._renderAxis
= function() {
324 if (!this.options
.drawXAxis
&& !this.options
.drawYAxis
)
327 var context
= this.element
.getContext("2d");
330 "position": "absolute",
331 "fontSize": this.options
.axisLabelFontSize
+ "px",
333 "color": this.options
.axisLabelColor
,
334 "width": this.options
.axisLabelWidth
+ "px",
337 var makeDiv
= function(txt
) {
338 var div
= document
.createElement("div");
339 for (var name
in labelStyle
) {
340 if (labelStyle
.hasOwnProperty(name
)) {
341 div
.style
[name
] = labelStyle
[name
];
344 div
.appendChild(document
.createTextNode(txt
));
350 context
.strokeStyle
= this.options
.axisLineColor
;
351 context
.lineWidth
= this.options
.axisLineWidth
;
353 if (this.options
.drawYAxis
) {
354 if (this.layout
.yticks
&& this.layout
.yticks
.length
> 0) {
355 for (var i
= 0; i
< this.layout
.yticks
.length
; i
++) {
356 var tick
= this.layout
.yticks
[i
];
357 if (typeof(tick
) == "function") return;
359 var y
= this.area
.y
+ tick
[0] * this.area
.h
;
361 context
.moveTo(x
, y
);
362 context
.lineTo(x
- this.options
.axisTickSize
, y
);
366 var label
= makeDiv(tick
[1]);
367 var top
= (y
- this.options
.axisLabelFontSize
/ 2);
368 if (top
< 0) top
= 0;
370 if (top
+ this.options
.axisLabelFontSize
+ 3 > this.height
) {
371 label
.style
.bottom
= "0px";
373 label
.style
.top
= top
+ "px";
375 label
.style
.left
= "0px";
376 label
.style
.textAlign
= "right";
377 label
.style
.width
= this.options
.yAxisLabelWidth
+ "px";
378 this.container
.appendChild(label
);
379 this.ylabels
.push(label
);
382 // The lowest tick on the y-axis often overlaps with the leftmost
383 // tick on the x-axis. Shift the bottom tick up a little bit to
384 // compensate if necessary.
385 var bottomTick
= this.ylabels
[0];
386 var fontSize
= this.options
.axisLabelFontSize
;
387 var bottom
= parseInt(bottomTick
.style
.top
) + fontSize
;
388 if (bottom
> this.height
- fontSize
) {
389 bottomTick
.style
.top
= (parseInt(bottomTick
.style
.top
) -
390 fontSize
/ 2) + "px";
395 context
.moveTo(this.area
.x
, this.area
.y
);
396 context
.lineTo(this.area
.x
, this.area
.y
+ this.area
.h
);
401 if (this.options
.drawXAxis
) {
402 if (this.layout
.xticks
) {
403 for (var i
= 0; i
< this.layout
.xticks
.length
; i
++) {
404 var tick
= this.layout
.xticks
[i
];
405 if (typeof(dataset
) == "function") return;
407 var x
= this.area
.x
+ tick
[0] * this.area
.w
;
408 var y
= this.area
.y
+ this.area
.h
;
410 context
.moveTo(x
, y
);
411 context
.lineTo(x
, y
+ this.options
.axisTickSize
);
415 var label
= makeDiv(tick
[1]);
416 label
.style
.textAlign
= "center";
417 label
.style
.bottom
= "0px";
419 var left
= (x
- this.options
.axisLabelWidth
/2);
420 if (left
+ this.options
.axisLabelWidth
> this.width
) {
421 left
= this.width
- this.options
.xAxisLabelWidth
;
422 label
.style
.textAlign
= "right";
426 label
.style
.textAlign
= "left";
429 label
.style
.left
= left
+ "px";
430 label
.style
.width
= this.options
.xAxisLabelWidth
+ "px";
431 this.container
.appendChild(label
);
432 this.xlabels
.push(label
);
437 context
.moveTo(this.area
.x
, this.area
.y
+ this.area
.h
);
438 context
.lineTo(this.area
.x
+ this.area
.w
, this.area
.y
+ this.area
.h
);
448 * Overrides the CanvasRenderer method to draw error bars
450 DygraphCanvasRenderer
.prototype._renderLineChart
= function() {
451 var context
= this.element
.getContext("2d");
452 var colorCount
= this.options
.colorScheme
.length
;
453 var colorScheme
= this.options
.colorScheme
;
454 var fillAlpha
= this.options
.fillAlpha
;
455 var errorBars
= this.layout
.options
.errorBars
;
456 var fillGraph
= this.layout
.options
.fillGraph
;
459 for (var name
in this.layout
.datasets
) {
460 if (this.layout
.datasets
.hasOwnProperty(name
)) {
464 var setCount
= setNames
.length
;
468 for (var i
= 0; i
< this.layout
.points
.length
; i
++) {
469 var point
= this.layout
.points
[i
];
470 point
.canvasx
= this.area
.w
* point
.x
+ this.area
.x
;
471 point
.canvasy
= this.area
.h
* point
.y
+ this.area
.y
;
475 var isOK
= function(x
) { return x
&& !isNaN(x
); };
480 this.dygraph_
.warn("Can't use fillGraph option with error bars");
483 for (var i
= 0; i
< setCount
; i
++) {
484 var setName
= setNames
[i
];
485 var color
= colorScheme
[i
% colorCount
];
487 // setup graphics context
489 ctx
.strokeStyle
= color
;
490 ctx
.lineWidth
= this.options
.strokeWidth
;
492 var prevYs
= [-1, -1];
494 var yscale
= this.layout
.yscale
;
495 // should be same color as the lines but only 15% opaque.
496 var rgb
= new RGBColor(color
);
497 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
499 ctx
.fillStyle
= err_color
;
501 for (var j
= 0; j
< this.layout
.points
.length
; j
++) {
502 var point
= this.layout
.points
[j
];
504 if (point
.name
== setName
) {
505 if (!isOK(point
.y
)) {
510 var newYs
= [ point
.y
- point
.errorPlus
* yscale
,
511 point
.y
+ point
.errorMinus
* yscale
];
512 newYs
[0] = this.area
.h
* newYs
[0] + this.area
.y
;
513 newYs
[1] = this.area
.h
* newYs
[1] + this.area
.y
;
515 ctx
.moveTo(prevX
, prevYs
[0]);
516 ctx
.lineTo(point
.canvasx
, newYs
[0]);
517 ctx
.lineTo(point
.canvasx
, newYs
[1]);
518 ctx
.lineTo(prevX
, prevYs
[1]);
521 prevYs
[0] = newYs
[0];
522 prevYs
[1] = newYs
[1];
523 prevX
= point
.canvasx
;
528 } else if (fillGraph
) {
529 // TODO(danvk): merge this code with the logic above; they're very similar.
530 for (var i
= 0; i
< setCount
; i
++) {
531 var setName
= setNames
[i
];
533 if (i
>0) setNameLast
= setNames
[i
-1];
534 var color
= colorScheme
[i
% colorCount
];
536 // setup graphics context
538 ctx
.strokeStyle
= color
;
539 ctx
.lineWidth
= this.options
.strokeWidth
;
541 var prevYs
= [-1, -1];
543 var yscale
= this.layout
.yscale
;
544 // should be same color as the lines but only 15% opaque.
545 var rgb
= new RGBColor(color
);
546 var err_color
= 'rgba(' + rgb
.r
+ ',' + rgb
.g
+ ',' + rgb
.b
+ ',' +
548 ctx
.fillStyle
= err_color
;
550 for (var j
= 0; j
< this.layout
.points
.length
; j
++) {
551 var point
= this.layout
.points
[j
];
553 if (point
.name
== setName
) {
554 if (!isOK(point
.y
)) {
558 var pX
= 1.0 + this.layout
.minyval
* this.layout
.yscale
;
559 if (pX
< 0.0) pX
= 0.0;
560 else if (pX
> 1.0) pX
= 1.0;
561 var newYs
= [ point
.y
, pX
];
562 newYs
[0] = this.area
.h
* newYs
[0] + this.area
.y
;
563 newYs
[1] = this.area
.h
* newYs
[1] + this.area
.y
;
565 ctx
.moveTo(prevX
, prevYs
[0]);
566 ctx
.lineTo(point
.canvasx
, newYs
[0]);
567 ctx
.lineTo(point
.canvasx
, newYs
[1]);
568 ctx
.lineTo(prevX
, prevYs
[1]);
571 prevYs
[0] = newYs
[0];
572 prevYs
[1] = newYs
[1];
573 prevX
= point
.canvasx
;
580 for (var i
= 0; i
< setCount
; i
++) {
581 var setName
= setNames
[i
];
582 var color
= colorScheme
[i
%colorCount
];
584 // setup graphics context
586 var point
= this.layout
.points
[0];
587 var pointSize
= this.dygraph_
.attr_("pointSize");
588 var prevX
= null, prevY
= null;
589 var drawPoints
= this.dygraph_
.attr_("drawPoints");
590 var points
= this.layout
.points
;
591 for (var j
= 0; j
< points
.length
; j
++) {
592 var point
= points
[j
];
593 if (point
.name
== setName
) {
594 if (!isOK(point
.canvasy
)) {
595 // this will make us move to the next point, not draw a line to it.
596 prevX
= prevY
= null;
598 // A point is "isolated" if it is non-null but both the previous
599 // and next points are null.
600 var isIsolated
= (!prevX
&& (j
== points
.length
- 1 ||
601 !isOK(points
[j
+1].canvasy
)));
604 prevX
= point
.canvasx
;
605 prevY
= point
.canvasy
;
608 ctx
.strokeStyle
= color
;
609 ctx
.lineWidth
= this.options
.strokeWidth
;
610 ctx
.moveTo(prevX
, prevY
);
611 prevX
= point
.canvasx
;
612 prevY
= point
.canvasy
;
613 ctx
.lineTo(prevX
, prevY
);
617 if (drawPoints
|| isIsolated
) {
619 ctx
.fillStyle
= color
;
620 ctx
.arc(point
.canvasx
, point
.canvasy
, pointSize
,
621 0, 2 * Math
.PI
, false);