760773117708e16855721454a0af66b6639fdb4c
5 User friendly wrapper around the common plotting functions.
9 Copyright 2005,2006 (c) Alastair Tse <alastair^liquidx.net>
10 For use under the BSD license. <http://www.liquidx.net/plotkit>
15 if (typeof(PlotKit
.CanvasRenderer
) == 'undefined')
21 throw "PlotKit.EasyPlot depends on all of PlotKit's components";
24 // --------------------------------------------------------------------
25 // Start of EasyPlot definition
26 // --------------------------------------------------------------------
28 if (typeof(PlotKit
.EasyPlot
) == 'undefined') {
29 PlotKit
.EasyPlot
= {};
32 PlotKit
.EasyPlot
.NAME
= "PlotKit.EasyPlot";
33 PlotKit
.EasyPlot
.VERSION
= PlotKit
.VERSION
;
35 PlotKit
.EasyPlot
.__repr__
= function() {
36 return "[" + this.NAME
+ " " + this.VERSION
+ "]";
39 PlotKit
.EasyPlot
.toString
= function() {
40 return this.__repr__();
43 // --------------------------------------------------------------------
44 // Start of EasyPlot definition
45 // --------------------------------------------------------------------
47 PlotKit
.EasyPlot
= function(style
, options
, divElem
, datasources
) {
48 this.layout
= new Layout(style
, options
);
49 this.divElem
= divElem
;
50 this.width
= parseInt(divElem
.getAttribute('width'));
51 this.height
= parseInt(divElem
.getAttribute('height'));
52 this.deferredCount
= 0;
54 // make sure we have non-zero width
56 this.width
= this.divElem
.width
? this.divElem
.width
: 300;
59 if (this.height
< 1) {
60 this.height
= this.divElem
.height
? this.divElem
.height
: 300;
64 if (isArrayLike(datasources
)) {
65 for (var i
= 0; i
< datasources
.length
; i
++) {
66 if (typeof(datasources
[i
]) == "string") {
69 var d
= MochiKit
.Async
.doSimpleXMLHttpRequest(datasources
[i
]);
70 d
.addCallback(MochiKit
.Base
.bind(PlotKit
.EasyPlot
.onDataLoaded
, this));
72 else if (isArrayLike(datasources
[i
])) {
73 this.layout
.addDataset("data-" + i
, datasources
[i
]);
77 else if (!isUndefinedOrNull(datasources
)) {
78 throw "Passed datasources are not Array like";
81 // setup canvas to render
83 if (CanvasRenderer
.isSupported()) {
84 this.element
= CANVAS({"id": this.divElem
.getAttribute("id") + "-canvas",
86 "height": this.height
}, "");
87 this.divElem
.appendChild(this.element
);
88 this.renderer
= new SweetCanvasRenderer(this.element
, this.layout
, options
);
90 else if (SVGRenderer
.isSupported()) {
91 this.element
= SVGRenderer
.SVG({"id": this.divElem
.getAttribute("id") + "-svg",
93 "height": this.height
,
95 "baseProfile": "full"}, "");
96 this.divElem
.appendChild(this.element
);
97 this.renderer
= new SweetSVGRenderer(this.element
, this.layout
, options
);
100 if ((this.deferredCount
== 0) && (PlotKit
.Base
.keys(this.layout
.datasets
).length
> 0)) {
101 this.layout
.evaluate();
102 this.renderer
.clear();
103 this.renderer
.render();
108 PlotKit
.EasyPlot
.onDataLoaded
= function(request
) {
110 // very primitive CSV parser, should fix to make it more compliant.
111 var table
= new Array();
112 var lines
= request
.responseText
.split('\n');
113 for (var i
= 0; i
< lines
.length
; i
++) {
114 var stripped
= MochiKit
.Format
.strip(lines
[i
]);
115 if ((stripped
.length
> 1) && (stripped
.charAt(0) != '#')) {
116 table
.push(stripped
.split(','));
120 this.layout
.addDataset("data-ajax-" + this.deferredCount
, table
);
121 this.deferredCount
--;
123 if ((this.deferredCount
== 0) && (PlotKit
.Base
.keys(this.layout
.datasets
).length
> 0)) {
124 this.layout
.evaluate();
125 this.renderer
.clear();
126 this.renderer
.render();
130 PlotKit
.EasyPlot
.prototype.reload
= function() {
131 this.layout
.evaluate();
132 this.renderer
.clear();
133 this.renderer
.render();
136 // Namespace Iniitialisation
138 PlotKit
.EasyPlotModule
= {};
139 PlotKit
.EasyPlotModule
.EasyPlot
= PlotKit
.EasyPlot
;
141 PlotKit
.EasyPlotModule
.EXPORT
= [
145 PlotKit
.EasyPlotModule
.EXPORT_OK
= [];
147 PlotKit
.EasyPlotModule
.__new__
= function() {
148 var m
= MochiKit
.Base
;
150 m
.nameFunctions(this);
153 ":common": this.EXPORT
,
154 ":all": m
.concat(this.EXPORT
, this.EXPORT_OK
)
158 PlotKit
.EasyPlotModule
.__new__();
159 MochiKit
.Base
._exportSymbols(this, PlotKit
.EasyPlotModule
);