d8d5950cdf07251fb2ed11ca8790ad06ce862749
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 MochiKit
.Base
.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 for (var name
in this.datasets
) {
41 var series
= this.datasets
[name
];
42 var x1
= series
[0][0];
43 if (!this.minxval
|| x1
< this.minxval
) this.minxval
= x1
;
45 var x2
= series
[series
.length
- 1][0];
46 if (!this.maxxval
|| x2
> this.maxxval
) this.maxxval
= x2
;
48 this.xrange
= this.maxxval
- this.minxval
;
49 this.xscale
= (this.xrange
!= 0 ? 1/this.xrange
: 1.0);
51 this.minyval
= this.options
.yAxis
[0];
52 this.maxyval
= this.options
.yAxis
[1];
53 this.yrange
= this.maxyval
- this.minyval
;
54 this.yscale
= (this.yrange
!= 0 ? 1/this.yrange
: 1.0);
57 DygraphLayout
.prototype._evaluateLineCharts
= function() {
59 this.points
= new Array();
60 for (var setName
in this.datasets
) {
61 var dataset
= this.datasets
[setName
];
62 for (var j
= 0; j
< dataset
.length
; j
++) {
63 var item
= dataset
[j
];
65 x
: ((parseFloat(item
[0]) - this.minxval
) * this.xscale
),
66 y
: 1.0 - ((parseFloat(item
[1]) - this.minyval
) * this.yscale
),
67 xval
: parseFloat(item
[0]),
68 yval
: parseFloat(item
[1]),
72 // limit the x, y values so they do not overdraw
79 if ((point
.x
>= 0.0) && (point
.x
<= 1.0)) {
80 this.points
.push(point
);
86 DygraphLayout
.prototype._evaluateLineTicks
= function() {
87 this.xticks
= new Array();
88 for (var i
= 0; i
< this.options
.xTicks
.length
; i
++) {
89 var tick
= this.options
.xTicks
[i
];
90 var label
= tick
.label
;
91 var pos
= this.xscale
* (tick
.v
- this.minxval
);
92 if ((pos
>= 0.0) && (pos
<= 1.0)) {
93 this.xticks
.push([pos
, label
]);
97 this.yticks
= new Array();
98 for (var i
= 0; i
< this.options
.yTicks
.length
; i
++) {
99 var tick
= this.options
.yTicks
[i
];
100 var label
= tick
.label
;
101 var pos
= 1.0 - (this.yscale
* (tick
.v
- this.minyval
));
102 if ((pos
>= 0.0) && (pos
<= 1.0)) {
103 this.yticks
.push([pos
, label
]);
110 * Behaves the same way as PlotKit.Layout, but also copies the errors
113 DygraphLayout
.prototype.evaluateWithError
= function() {
115 if (!this.options
.errorBars
) return;
117 // Copy over the error terms
118 var i
= 0; // index in this.points
119 for (var setName
in this.datasets
) {
121 var dataset
= this.datasets
[setName
];
122 for (var j
= 0; j
< dataset
.length
; j
++, i
++) {
123 var item
= dataset
[j
];
124 var xv
= parseFloat(item
[0]);
125 var yv
= parseFloat(item
[1]);
127 if (xv
== this.points
[i
].xval
&&
128 yv
== this.points
[i
].yval
) {
129 this.points
[i
].errorMinus
= parseFloat(item
[2]);
130 this.points
[i
].errorPlus
= parseFloat(item
[3]);
137 * Convenience function to remove all the data sets from a graph
139 DygraphLayout
.prototype.removeAllDatasets
= function() {
140 delete this.datasets
;
141 this.datasets
= new Array();
145 * Change the values of various layout options
146 * @param {Object} new_options an associative array of new properties
148 DygraphLayout
.prototype.updateOptions
= function(new_options
) {
149 MochiKit
.Base
.update(this.options
, new_options
? new_options
: {});
152 // Subclass PlotKit.CanvasRenderer to add:
153 // 1. X/Y grid overlay
154 // 2. Ability to draw error bars (if required)
157 * Sets some PlotKit.CanvasRenderer options
158 * @param {Object} element The canvas to attach to
159 * @param {Layout} layout The DygraphLayout object for this graph.
160 * @param {Object} options Options to pass on to CanvasRenderer
162 DygraphCanvasRenderer
= function(dygraph
, element
, layout
, options
) {
163 // TODO(danvk): remove options, just use dygraph.attr_.
164 this.dygraph_
= dygraph
;
171 "axisLineColor": Color
.blackColor(),
172 "axisLineWidth": 0.5,
174 "axisLabelColor": Color
.blackColor(),
175 "axisLabelFont": "Arial",
176 "axisLabelFontSize": 9,
177 "axisLabelWidth": 50,
180 "gridLineColor": MochiKit
.Color
.Color
.grayColor()
182 MochiKit
.Base
.update(this.options
, options
);
184 this.layout
= layout
;
185 this.element
= element
;
186 this.container
= this.element
.parentNode
;
188 // Stuff relating to Canvas on IE support
189 this.isIE
= (/MSIE/.test(navigator
.userAgent
) && !window
.opera
);
191 if (this.isIE
&& !isNil(G_vmlCanvasManager
)) {
194 this.renderDelay
= null;
195 this.clearDelay
= null;
196 this.element
= G_vmlCanvasManager
.initElement(this.element
);
199 this.height
= this.element
.height
;
200 this.width
= this.element
.width
;
202 // --- check whether everything is ok before we return
203 if (!this.isIE
&& !(DygraphCanvasRenderer
.isSupported(this.element
)))
204 throw "Canvas is not supported.";
207 this.xlabels
= new Array();
208 this.ylabels
= new Array();
211 x
: this.options
.yAxisLabelWidth
+ 2 * this.options
.axisTickSize
,
214 this.area
.w
= this.width
- this.area
.x
- this.options
.rightGap
;
215 this.area
.h
= this.height
- this.options
.axisLabelFontSize
-
216 2 * this.options
.axisTickSize
;
218 this.container
.style
.position
= "relative";
219 this.container
.style
.width
= this.width
+ "px";
222 DygraphCanvasRenderer
.prototype.clear
= function() {
224 // VML takes a while to start up, so we just poll every this.IEDelay
226 if (this.clearDelay
) {
227 this.clearDelay
.cancel();
228 this.clearDelay
= null;
230 var context
= this.element
.getContext("2d");
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 (MochiKit
.Base
.isUndefinedOrNull(canvasName
))
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");
280 if (this.options
.drawYGrid
) {
281 var ticks
= this.layout
.yticks
;
283 ctx
.strokeStyle
= this.options
.gridLineColor
.toRGBString();
284 ctx
.lineWidth
= this.options
.axisLineWidth
;
285 for (var i
= 0; i
< ticks
.length
; i
++) {
287 var y
= this.area
.y
+ ticks
[i
][0] * this.area
.h
;
290 ctx
.lineTo(x
+ this.area
.w
, y
);
296 if (this.options
.drawXGrid
) {
297 var ticks
= this.layout
.xticks
;
299 ctx
.strokeStyle
= this.options
.gridLineColor
.toRGBString();
300 ctx
.lineWidth
= this.options
.axisLineWidth
;
301 for (var i
=0; i
<ticks
.length
; i
++) {
302 var x
= this.area
.x
+ ticks
[i
][0] * this.area
.w
;
303 var y
= this.area
.y
+ this.area
.h
;
306 ctx
.lineTo(x
, this.area
.y
);
312 // Do the ordinary rendering, as before
313 // TODO(danvk) Call super.render()
314 this._renderLineChart();
319 DygraphCanvasRenderer
.prototype._renderAxis
= function() {
320 if (!this.options
.drawXAxis
&& !this.options
.drawYAxis
)
323 var context
= this.element
.getContext("2d");
325 var labelStyle
= {"style":
326 {"position": "absolute",
327 "fontSize": this.options
.axisLabelFontSize
+ "px",
329 "color": this.options
.axisLabelColor
.toRGBString(),
330 "width": this.options
.axisLabelWidth
+ "px",
337 context
.strokeStyle
= this.options
.axisLineColor
.toRGBString();
338 context
.lineWidth
= this.options
.axisLineWidth
;
341 if (this.options
.drawYAxis
) {
342 if (this.layout
.yticks
) {
343 for (var i
= 0; i
< this.layout
.yticks
.length
; i
++) {
344 var tick
= this.layout
.yticks
[i
];
345 if (typeof(tick
) == "function") return;
347 var y
= this.area
.y
+ tick
[0] * this.area
.h
;
349 context
.moveTo(x
, y
);
350 context
.lineTo(x
- this.options
.axisTickSize
, y
);
354 var label
= DIV(labelStyle
, tick
[1]);
355 var top
= (y
- this.options
.axisLabelFontSize
/ 2);
356 if (top
< 0) top
= 0;
358 if (top
+ this.options
.axisLabelFontSize
+ 3 > this.height
) {
359 label
.style
.bottom
= "0px";
361 label
.style
.top
= top
+ "px";
363 label
.style
.left
= "0px";
364 label
.style
.textAlign
= "right";
365 label
.style
.width
= this.options
.yAxisLabelWidth
+ "px";
366 this.container
.appendChild(label
);
367 this.ylabels
.push(label
);
370 // The lowest tick on the y-axis often overlaps with the leftmost
371 // tick on the x-axis. Shift the bottom tick up a little bit to
372 // compensate if necessary.
373 var bottomTick
= this.ylabels
[0];
374 var fontSize
= this.options
.axisLabelFontSize
;
375 var bottom
= parseInt(bottomTick
.style
.top
) + fontSize
;
376 if (bottom
> this.height
- fontSize
) {
377 bottomTick
.style
.top
= (parseInt(bottomTick
.style
.top
) -
378 fontSize
/ 2) + "px";
383 context
.moveTo(this.area
.x
, this.area
.y
);
384 context
.lineTo(this.area
.x
, this.area
.y
+ this.area
.h
);
389 if (this.options
.drawXAxis
) {
390 if (this.layout
.xticks
) {
391 for (var i
= 0; i
< this.layout
.xticks
.length
; i
++) {
392 var tick
= this.layout
.xticks
[i
];
393 if (typeof(dataset
) == "function") return;
395 var x
= this.area
.x
+ tick
[0] * this.area
.w
;
396 var y
= this.area
.y
+ this.area
.h
;
398 context
.moveTo(x
, y
);
399 context
.lineTo(x
, y
+ this.options
.axisTickSize
);
403 var label
= DIV(labelStyle
, tick
[1]);
404 label
.style
.textAlign
= "center";
405 label
.style
.bottom
= "0px";
407 var left
= (x
- this.options
.axisLabelWidth
/2);
408 if (left
+ this.options
.axisLabelWidth
> this.width
) {
409 left
= this.width
- this.options
.xAxisLabelWidth
;
410 label
.style
.textAlign
= "right";
414 label
.style
.textAlign
= "left";
417 label
.style
.left
= left
+ "px";
418 label
.style
.width
= this.options
.xAxisLabelWidth
+ "px";
419 this.container
.appendChild(label
);
420 this.xlabels
.push(label
);
425 context
.moveTo(this.area
.x
, this.area
.y
+ this.area
.h
);
426 context
.lineTo(this.area
.x
+ this.area
.w
, this.area
.y
+ this.area
.h
);
436 * Overrides the CanvasRenderer method to draw error bars
438 DygraphCanvasRenderer
.prototype._renderLineChart
= function() {
439 var context
= this.element
.getContext("2d");
440 var colorCount
= this.options
.colorScheme
.length
;
441 var colorScheme
= this.options
.colorScheme
;
442 var setNames
= MochiKit
.Base
.keys(this.layout
.datasets
);
443 var errorBars
= this.layout
.options
.errorBars
;
444 var setCount
= setNames
.length
;
445 var bind
= MochiKit
.Base
.bind
;
446 var partial
= MochiKit
.Base
.partial
;
449 for (var i
= 0; i
< this.layout
.points
.length
; i
++) {
450 var point
= this.layout
.points
[i
];
451 point
.canvasx
= this.area
.w
* point
.x
+ this.area
.x
;
452 point
.canvasy
= this.area
.h
* point
.y
+ this.area
.y
;
456 var isOK
= function(x
) { return x
&& !isNaN(x
); };
457 var makePath
= function(ctx
) {
458 for (var i
= 0; i
< setCount
; i
++) {
459 var setName
= setNames
[i
];
460 var color
= colorScheme
[i
%colorCount
];
461 var strokeX
= this.options
.strokeColorTransform
;
463 // setup graphics context
465 context
.strokeStyle
= color
.toRGBString();
466 context
.lineWidth
= this.options
.strokeWidth
;
467 var point
= this.layout
.points
[0];
468 var pointSize
= this.dygraph_
.attr_("pointSize");
469 var prevX
= null, prevY
= null;
470 var drawPoints
= this.dygraph_
.attr_("drawPoints");
471 var points
= this.layout
.points
;
472 for (var j
= 0; j
< points
.length
; j
++) {
473 var point
= points
[j
];
474 if (point
.name
== setName
) {
475 if (!isOK(point
.canvasy
)) {
476 // this will make us move to the next point, not draw a line to it.
477 prevX
= prevY
= null;
479 // A point is "isolated" if it is non-null but both the previous
480 // and next points are null.
481 var isIsolated
= (!prevX
&& (j
== points
.length
- 1 ||
482 !isOK(points
[j
+1].canvasy
)));
485 prevX
= point
.canvasx
;
486 prevY
= point
.canvasy
;
489 ctx
.moveTo(prevX
, prevY
);
490 prevX
= point
.canvasx
;
491 prevY
= point
.canvasy
;
492 ctx
.lineTo(prevX
, prevY
);
496 if (drawPoints
|| isIsolated
) {
498 ctx
.fillStyle
= color
.toRGBString();
499 ctx
.arc(point
.canvasx
, point
.canvasy
, pointSize
, 0, 360, false);
508 var makeErrorBars
= function(ctx
) {
509 for (var i
= 0; i
< setCount
; i
++) {
510 var setName
= setNames
[i
];
511 var color
= colorScheme
[i
% colorCount
];
512 var strokeX
= this.options
.strokeColorTransform
;
514 // setup graphics context
516 context
.strokeStyle
= color
.toRGBString();
517 context
.lineWidth
= this.options
.strokeWidth
;
519 var prevYs
= [-1, -1];
521 var yscale
= this.layout
.yscale
;
522 // should be same color as the lines
523 var err_color
= color
.colorWithAlpha(0.15);
524 ctx
.fillStyle
= err_color
.toRGBString();
526 for (var j
= 0; j
< this.layout
.points
.length
; j
++) {
527 var point
= this.layout
.points
[j
];
529 if (point
.name
== setName
) {
530 if (!point
.y
|| isNaN(point
.y
)) {
534 var newYs
= [ point
.y
- point
.errorPlus
* yscale
,
535 point
.y
+ point
.errorMinus
* yscale
];
536 newYs
[0] = this.area
.h
* newYs
[0] + this.area
.y
;
537 newYs
[1] = this.area
.h
* newYs
[1] + this.area
.y
;
539 ctx
.moveTo(prevX
, prevYs
[0]);
540 ctx
.lineTo(point
.canvasx
, newYs
[0]);
541 ctx
.lineTo(point
.canvasx
, newYs
[1]);
542 ctx
.lineTo(prevX
, prevYs
[1]);
545 prevYs
[0] = newYs
[0];
546 prevYs
[1] = newYs
[1];
547 prevX
= point
.canvasx
;
555 bind(makeErrorBars
, this)(context
);
556 bind(makePath
, this)(context
);