5 Provides HTML Canvas Renderer. This is supported under:
10 - IE 6 (via VML Emulation)
12 It uses DIVs for labels.
16 Copyright 2005,2006 (c) Alastair Tse <alastair^liquidx.net>
17 For use under the BSD license. <http://www.liquidx.net/plotkit>
20 // --------------------------------------------------------------------
21 // Check required components
22 // --------------------------------------------------------------------
25 if ((typeof(PlotKit
.Base
) == 'undefined') ||
26 (typeof(PlotKit
.Layout
) == 'undefined'))
32 throw "PlotKit.Layout depends on MochiKit.{Base,Color,DOM,Format} and PlotKit.{Base,Layout}"
36 // ------------------------------------------------------------------------
37 // Defines the renderer class
38 // ------------------------------------------------------------------------
40 if (typeof(PlotKit
.CanvasRenderer
) == 'undefined') {
41 PlotKit
.CanvasRenderer
= {};
44 PlotKit
.CanvasRenderer
.NAME
= "PlotKit.CanvasRenderer";
45 PlotKit
.CanvasRenderer
.VERSION
= PlotKit
.VERSION
;
47 PlotKit
.CanvasRenderer
.__repr__
= function() {
48 return "[" + this.NAME
+ " " + this.VERSION
+ "]";
51 PlotKit
.CanvasRenderer
.toString
= function() {
52 return this.__repr__();
55 PlotKit
.CanvasRenderer
= function(element
, layout
, options
) {
56 if (arguments
.length
> 0)
57 this.__init__(element
, layout
, options
);
60 PlotKit
.CanvasRenderer
.prototype.__init__
= function(element
, layout
, options
) {
61 var isNil
= MochiKit
.Base
.isUndefinedOrNull
;
62 var Color
= MochiKit
.Color
.Color
;
66 "drawBackground": true,
67 "backgroundColor": Color
.whiteColor(),
68 "colorScheme": PlotKit
.Base
.palette(PlotKit
.Base
.baseColors()[0]),
69 "strokeColor": Color
.whiteColor(),
70 "strokeColorTransform": "asStrokeColor",
76 "axisLineColor": Color
.blackColor(),
79 "axisLabelColor": Color
.blackColor(),
80 "axisLabelFont": "Arial",
81 "axisLabelFontSize": 9,
86 MochiKit
.Base
.update(this.options
, options
? options
: {});
89 this.element
= MochiKit
.DOM
.getElement(element
);
90 this.container
= this.element
.parentNode
;
92 // Stuff relating to Canvas on IE support
93 this.isIE
= PlotKit
.Base
.excanvasSupported();
95 if (this.isIE
&& !isNil(G_vmlCanvasManager
)) {
98 this.renderDelay
= null;
99 this.clearDelay
= null;
100 this.element
= G_vmlCanvasManager
.initElement(this.element
);
103 this.height
= this.element
.height
;
104 this.width
= this.element
.width
;
106 // --- check whether everything is ok before we return
108 if (isNil(this.element
))
109 throw "CanvasRenderer() - passed canvas is not found";
111 if (!this.isIE
&& !(PlotKit
.CanvasRenderer
.isSupported(this.element
)))
112 throw "CanvasRenderer() - Canvas is not supported.";
114 if (isNil(this.container
) || (this.container
.nodeName
.toLowerCase() != "div"))
115 throw "CanvasRenderer() - <canvas> needs to be enclosed in <div>";
118 this.xlabels
= new Array();
119 this.ylabels
= new Array();
120 this.isFirstRender
= true;
123 x
: this.options
.yAxisLabelWidth
+ 2 * this.options
.axisTickSize
,
126 this.area
.w
= this.width
- this.area
.x
- this.options
.rightGap
;
127 this.area
.h
= this.height
- this.options
.axisLabelFontSize
-
128 2 * this.options
.axisTickSize
;
130 MochiKit
.DOM
.updateNodeAttributes(this.container
,
131 {"style":{ "position": "relative", "width": this.width
+ "px"}});
135 PlotKit
.CanvasRenderer
.prototype._renderLineAxis
= function() {
140 PlotKit
.CanvasRenderer
.prototype._renderAxis
= function() {
141 if (!this.options
.drawXAxis
&& !this.options
.drawYAxis
)
144 var context
= this.element
.getContext("2d");
146 var labelStyle
= {"style":
147 {"position": "absolute",
148 "fontSize": this.options
.axisLabelFontSize
+ "px",
150 "color": this.options
.axisLabelColor
.toRGBString(),
151 "width": this.options
.axisLabelWidth
+ "px",
158 context
.strokeStyle
= this.options
.axisLineColor
.toRGBString();
159 context
.lineWidth
= this.options
.axisLineWidth
;
162 if (this.options
.drawYAxis
) {
163 if (this.layout
.yticks
) {
164 var drawTick
= function(tick
) {
165 if (typeof(tick
) == "function") return;
167 var y
= this.area
.y
+ tick
[0] * this.area
.h
;
169 context
.moveTo(x
, y
);
170 context
.lineTo(x
- this.options
.axisTickSize
, y
);
174 var label
= DIV(labelStyle
, tick
[1]);
175 var top
= (y
- this.options
.axisLabelFontSize
/ 2);
176 if (top
< 0) top
= 0;
178 if (top
+ this.options
.axisLabelFontSize
+ 3 > this.height
) {
179 label
.style
.bottom
= "0px";
181 label
.style
.top
= top
+ "px";
183 label
.style
.left
= "0px";
184 label
.style
.textAlign
= "right";
185 label
.style
.width
= this.options
.yAxisLabelWidth
+ "px";
186 MochiKit
.DOM
.appendChildNodes(this.container
, label
);
187 this.ylabels
.push(label
);
190 MochiKit
.Iter
.forEach(this.layout
.yticks
, bind(drawTick
, this));
192 // The lowest tick on the y-axis often overlaps with the leftmost
193 // tick on the x-axis. Shift the bottom tick up a little bit to
194 // compensate if necessary.
195 var bottomTick
= this.ylabels
[0];
196 var fontSize
= this.options
.axisLabelFontSize
;
197 var bottom
= parseInt(bottomTick
.style
.top
) + fontSize
;
198 if (bottom
> this.height
- fontSize
) {
199 bottomTick
.style
.top
= (parseInt(bottomTick
.style
.top
) -
200 fontSize
/ 2) + "px";
205 context
.moveTo(this.area
.x
, this.area
.y
);
206 context
.lineTo(this.area
.x
, this.area
.y
+ this.area
.h
);
211 if (this.options
.drawXAxis
) {
212 if (this.layout
.xticks
) {
213 var drawTick
= function(tick
) {
214 if (typeof(dataset
) == "function") return;
216 var x
= this.area
.x
+ tick
[0] * this.area
.w
;
217 var y
= this.area
.y
+ this.area
.h
;
219 context
.moveTo(x
, y
);
220 context
.lineTo(x
, y
+ this.options
.axisTickSize
);
224 var label
= DIV(labelStyle
, tick
[1]);
225 label
.style
.textAlign
= "center";
226 label
.style
.bottom
= "0px";
228 var left
= (x
- this.options
.axisLabelWidth
/2);
229 if (left
+ this.options
.axisLabelWidth
> this.width
) {
230 left
= this.width
- this.options
.xAxisLabelWidth
;
231 label
.style
.textAlign
= "right";
235 label
.style
.textAlign
= "left";
238 label
.style
.left
= left
+ "px";
239 label
.style
.width
= this.options
.xAxisLabelWidth
+ "px";
240 MochiKit
.DOM
.appendChildNodes(this.container
, label
);
241 this.xlabels
.push(label
);
244 MochiKit
.Iter
.forEach(this.layout
.xticks
, bind(drawTick
, this));
248 context
.moveTo(this.area
.x
, this.area
.y
+ this.area
.h
);
249 context
.lineTo(this.area
.x
+ this.area
.w
, this.area
.y
+ this.area
.h
);
258 PlotKit
.CanvasRenderer
.prototype.clear
= function() {
260 // VML takes a while to start up, so we just poll every this.IEDelay
262 if (this.clearDelay
) {
263 this.clearDelay
.cancel();
264 this.clearDelay
= null;
266 var context
= this.element
.getContext("2d");
269 this.isFirstRender
= false;
270 this.clearDelay
= MochiKit
.Async
.wait(this.IEDelay
);
271 this.clearDelay
.addCallback(bind(this.clear
, this));
276 var context
= this.element
.getContext("2d");
277 context
.clearRect(0, 0, this.width
, this.height
);
279 MochiKit
.Iter
.forEach(this.xlabels
, MochiKit
.DOM
.removeElement
);
280 MochiKit
.Iter
.forEach(this.ylabels
, MochiKit
.DOM
.removeElement
);
281 this.xlabels
= new Array();
282 this.ylabels
= new Array();
285 // ----------------------------------------------------------------
286 // Everything below here is experimental and undocumented.
287 // ----------------------------------------------------------------
290 PlotKit
.CanvasRenderer
.isSupported
= function(canvasName
) {
293 if (MochiKit
.Base
.isUndefinedOrNull(canvasName
))
294 canvas
= MochiKit
.DOM
.CANVAS({});
296 canvas
= MochiKit
.DOM
.getElement(canvasName
);
297 var context
= canvas
.getContext("2d");
300 var ie
= navigator
.appVersion
.match(/MSIE (\d\.\d)/);
301 var opera
= (navigator
.userAgent
.toLowerCase().indexOf("opera") != -1);
302 if ((!ie
) || (ie
[1] < 6) || (opera
))
309 // Namespace Iniitialisation
312 PlotKit
.Canvas
.CanvasRenderer
= PlotKit
.CanvasRenderer
;
314 PlotKit
.Canvas
.EXPORT
= [
318 PlotKit
.Canvas
.EXPORT_OK
= [
322 PlotKit
.Canvas
.__new__
= function() {
323 var m
= MochiKit
.Base
;
325 m
.nameFunctions(this);
328 ":common": this.EXPORT
,
329 ":all": m
.concat(this.EXPORT
, this.EXPORT_OK
)
333 PlotKit
.Canvas
.__new__();
334 MochiKit
.Base
._exportSymbols(this, PlotKit
.Canvas
);