]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/charting/widget/Chart2D.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojox / charting / widget / Chart2D.js
1 if(!dojo._hasResource["dojox.charting.widget.Chart2D"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.charting.widget.Chart2D"] = true;
3 dojo.provide("dojox.charting.widget.Chart2D");
4
5 dojo.require("dijit._Widget");
6 dojo.require("dojox.charting.Chart2D");
7 dojo.require("dojox.lang.functional");
8
9 (function(){
10         var collectAxisParams, collectPlotParams, collectDataParams,
11                 notNull = function(o){ return o; },
12                 df = dojox.lang.functional,
13                 du = dojox.lang.utils;
14         
15         dojo.declare("dojox.charting.widget.Chart2D", dijit._Widget, {
16                 // parameters for the markup
17                 
18                 // theme for the chart
19                 theme: null,
20                 
21                 // margins for the chart: {l: 10, r: 10, t: 10, b: 10}
22                 margins: null,
23                 
24                 // chart area
25                 stroke: null,
26                 fill:   null,
27                 
28                 // methods
29                 
30                 buildRendering: function(){
31                         var n = this.domNode = this.srcNodeRef;
32                         
33                         // collect chart parameters
34                         var axes   = dojo.filter(dojo.query("> .axis",   n).map(collectAxisParams), notNull);
35                         var plots  = dojo.filter(dojo.query("> .plot",   n).map(collectPlotParams), notNull);
36                         var series = dojo.filter(dojo.query("> .series", n).map(collectDataParams), notNull);
37                         
38                         // build the chart
39                         n.innerHTML = "";
40                         var c = this.chart = new dojox.charting.Chart2D(n, {
41                                 margins: this.margins, 
42                                 stroke:  this.stroke,
43                                 fill:    this.fill
44                         });
45                         
46                         // add collected parameters
47                         if(this.theme){
48                                 c.setTheme(this.theme);
49                         }
50                         dojo.forEach(axes, function(axis){
51                                 c.addAxis(axis.name, axis.kwArgs);
52                         });
53                         dojo.forEach(plots, function(plot){
54                                 c.addPlot(plot.name, plot.kwArgs);
55                         });
56                         var render = df.foldl(series, function(render, series){
57                                 if(series.type == "data"){
58                                         c.addSeries(series.name, series.data, series.kwArgs);
59                                         render = true;
60                                 }else{
61                                         c.addSeries(series.name, [0], series.kwArgs);
62                                         var kw = {};
63                                         du.updateWithPattern(
64                                                 kw, 
65                                                 series.kwArgs, 
66                                                 {
67                                                         "query": "", 
68                                                         "queryOptions": null, 
69                                                         "start": 0, 
70                                                         "count": 1 //, 
71                                                         // "sort": []
72                                                 }, 
73                                                 true
74                                         );
75                                         if(series.kwArgs.sort){
76                                                 // sort is a complex object type and doesn't survive coercian
77                                                 kw.sort = dojo.clone(series.kwArgs.sort);
78                                         }
79                                         dojo.mixin(kw, {
80                                                 onComplete: function(data){
81                                                         var values;
82                                                         if("valueFn" in series.kwArgs){
83                                                                 var fn = series.kwArgs.valueFn;
84                                                                 values = dojo.map(data, function(x){
85                                                                         return fn(series.data.getValue(x, series.field, 0));
86                                                                 });
87                                                         }else{
88                                                                 values = dojo.map(data, function(x){
89                                                                         return series.data.getValue(x, series.field, 0);
90                                                                 });
91                                                         }
92                                                         c.addSeries(series.name, values, series.kwArgs).render();
93                                                 }
94                                         });
95                                         series.data.fetch(kw);
96                                 }
97                                 return render;
98                         }, false);
99                         if(render){ c.render(); }
100                 },
101                 resize: function(box){
102                         dojo.marginBox(this.domNode, box);
103                         this.chart.resize();
104                 }
105         });
106         
107         collectAxisParams = function(node){
108                 var name = node.getAttribute("name"), type = node.getAttribute("type");
109                 if(!name){ return null; }
110                 var o = {name: name, kwArgs: {}}, kw = o.kwArgs;
111                 if(type){
112                         if(dojox.charting.axis2d[type]){
113                                 type = dojox._scopeName + ".charting.axis2d." + type;
114                         }
115                         var axis = eval("(" + type + ")");
116                         if(axis){ kw.type = axis; } 
117                 }else{
118                         type = dojox._scopeName + ".charting.axis2d.Default";
119                 }
120                 var dp = eval("(" + type + ".prototype.defaultParams)");
121                 for(var x in dp){
122                         if(x in kw){ continue; }
123                         var attr = node.getAttribute(x);
124                         kw[x] = du.coerceType(dp[x], attr == null ? dp[x] : attr);
125                 }
126                 var op = eval("(" + type + ".prototype.optionalParams)");
127                 for(var x in op){
128                         if(x in kw){ continue; }
129                         var attr = node.getAttribute(x);
130                         if(attr != null){
131                                 kw[x] = du.coerceType(op[x], attr);
132                         }
133                 }
134                 return o;
135         };
136         
137         collectPlotParams = function(node){
138                 var name = node.getAttribute("name"), type = node.getAttribute("type");
139                 if(!name){ return null; }
140                 var o = {name: name, kwArgs: {}}, kw = o.kwArgs;
141                 if(type){
142                         if(dojox.charting.plot2d[type]){
143                                 type = dojox._scopeName + ".charting.plot2d." + type;
144                         }
145                         var plot = eval("(" + type + ")");
146                         if(plot){ kw.type = plot; } 
147                 }else{
148                         type = dojox._scopeName + ".charting.plot2d.Default";
149                 }
150                 var dp = eval("(" + type + ".prototype.defaultParams)");
151                 for(var x in dp){
152                         if(x in kw){ continue; }
153                         var attr = node.getAttribute(x);
154                         kw[x] = du.coerceType(dp[x], attr == null ? dp[x] : attr);
155                 }
156                 var op = eval("(" + type + ".prototype.optionalParams)");
157                 for(var x in op){
158                         if(x in kw){ continue; }
159                         var attr = node.getAttribute(x);
160                         if(attr != null){
161                                 kw[x] = du.coerceType(op[x], attr);
162                         }
163                 }
164                 return o;
165         };
166         
167         collectDataParams = function(node){
168                 var name = node.getAttribute("name");
169                 if(!name){ return null; }
170                 var o = {name: name, kwArgs: {}}, kw = o.kwArgs, t;
171                 t = node.getAttribute("plot");
172                 if(t != null){ kw.plot = t; }
173                 t = node.getAttribute("marker");
174                 if(t != null){ kw.marker = t; }
175                 t = node.getAttribute("stroke");
176                 if(t != null){ kw.stroke = eval("(" + t + ")"); }
177                 t = node.getAttribute("fill");
178                 if(t != null){ kw.fill = eval("(" + t + ")"); }
179                 t = node.getAttribute("data");
180                 if(t != null){
181                         o.type = "data";
182                         o.data = dojo.map(String(t).split(','), Number);
183                         return o;
184                 }
185                 t = node.getAttribute("array");
186                 if(t != null){
187                         o.type = "data";
188                         o.data = eval("(" + t + ")");
189                         return o;
190                 }
191                 t = node.getAttribute("store");
192                 if(t != null){
193                         o.type = "store";
194                         o.data = eval("(" + t + ")");
195                         t = node.getAttribute("field");
196                         o.field = t != null ? t : "value";
197                         t = node.getAttribute("query");
198                         if(!!t){ kw.query = t; }
199                         t = node.getAttribute("queryOptions");
200                         if(!!t){ kw.queryOptions = eval("(" + t + ")"); }
201                         t = node.getAttribute("start");
202                         if(!!t){ kw.start = Number(t); }
203                         t = node.getAttribute("count");
204                         if(!!t){ kw.count = Number(t); }
205                         t = node.getAttribute("sort");
206                         if(!!t){ kw.sort = eval("("+t+")"); }
207                         t = node.getAttribute("valueFn");
208                         if(!!t){ kw.valueFn = df.lambda(t); }
209                         return o;
210                 }
211                 return null;
212         };
213 })();
214
215 }