1 | define(["dojo/_base/kernel", "dojo/_base/lang", "dojo/_base/array","dojo/_base/html","dojo/_base/declare", "dojo/query", |
---|
2 | "dijit/_Widget", "../Chart", "dojox/lang/utils", "dojox/lang/functional","dojox/lang/functional/lambda", |
---|
3 | "dijit/_base/manager"], |
---|
4 | function(kernel, lang, arr, html, declare, query, Widget, Chart, du, df, dfl){ |
---|
5 | /*===== |
---|
6 | var Widget = dijit._Widget; |
---|
7 | =====*/ |
---|
8 | var collectParams, collectAxisParams, collectPlotParams, |
---|
9 | collectActionParams, collectDataParams, |
---|
10 | notNull = function(o){ return o; }, |
---|
11 | dc = lang.getObject("dojox.charting"); |
---|
12 | |
---|
13 | var ChartWidget = declare("dojox.charting.widget.Chart", Widget, { |
---|
14 | // parameters for the markup |
---|
15 | |
---|
16 | // theme for the chart |
---|
17 | theme: null, |
---|
18 | |
---|
19 | // margins for the chart: {l: 10, r: 10, t: 10, b: 10} |
---|
20 | margins: null, |
---|
21 | |
---|
22 | // chart area, define them as undefined to: |
---|
23 | // allow the parser to take them into account |
---|
24 | // but make sure they have no defined value to not override theme |
---|
25 | stroke: undefined, |
---|
26 | fill: undefined, |
---|
27 | |
---|
28 | // methods |
---|
29 | |
---|
30 | buildRendering: function(){ |
---|
31 | this.inherited(arguments); |
---|
32 | |
---|
33 | n = this.domNode; |
---|
34 | |
---|
35 | // collect chart parameters |
---|
36 | var axes = query("> .axis", n).map(collectAxisParams).filter(notNull), |
---|
37 | plots = query("> .plot", n).map(collectPlotParams).filter(notNull), |
---|
38 | actions = query("> .action", n).map(collectActionParams).filter(notNull), |
---|
39 | series = query("> .series", n).map(collectDataParams).filter(notNull); |
---|
40 | |
---|
41 | // build the chart |
---|
42 | n.innerHTML = ""; |
---|
43 | var c = this.chart = new Chart(n, { |
---|
44 | margins: this.margins, |
---|
45 | stroke: this.stroke, |
---|
46 | fill: this.fill, |
---|
47 | textDir: this.textDir |
---|
48 | }); |
---|
49 | |
---|
50 | // add collected parameters |
---|
51 | if(this.theme){ |
---|
52 | c.setTheme(this.theme); |
---|
53 | } |
---|
54 | axes.forEach(function(axis){ |
---|
55 | c.addAxis(axis.name, axis.kwArgs); |
---|
56 | }); |
---|
57 | plots.forEach(function(plot){ |
---|
58 | c.addPlot(plot.name, plot.kwArgs); |
---|
59 | }); |
---|
60 | |
---|
61 | this.actions = actions.map(function(action){ |
---|
62 | return new action.action(c, action.plot, action.kwArgs); |
---|
63 | }); |
---|
64 | |
---|
65 | var render = df.foldl(series, function(render, series){ |
---|
66 | if(series.type == "data"){ |
---|
67 | c.addSeries(series.name, series.data, series.kwArgs); |
---|
68 | render = true; |
---|
69 | }else{ |
---|
70 | c.addSeries(series.name, [0], series.kwArgs); |
---|
71 | var kw = {}; |
---|
72 | du.updateWithPattern( |
---|
73 | kw, |
---|
74 | series.kwArgs, |
---|
75 | { |
---|
76 | "query": "", |
---|
77 | "queryOptions": null, |
---|
78 | "start": 0, |
---|
79 | "count": 1 //, |
---|
80 | // "sort": [] |
---|
81 | }, |
---|
82 | true |
---|
83 | ); |
---|
84 | if(series.kwArgs.sort){ |
---|
85 | // sort is a complex object type and doesn't survive coercian |
---|
86 | kw.sort = lang.clone(series.kwArgs.sort); |
---|
87 | } |
---|
88 | lang.mixin(kw, { |
---|
89 | onComplete: function(data){ |
---|
90 | var values; |
---|
91 | if("valueFn" in series.kwArgs){ |
---|
92 | var fn = series.kwArgs.valueFn; |
---|
93 | values = arr.map(data, function(x){ |
---|
94 | return fn(series.data.getValue(x, series.field, 0)); |
---|
95 | }); |
---|
96 | }else{ |
---|
97 | values = arr.map(data, function(x){ |
---|
98 | return series.data.getValue(x, series.field, 0); |
---|
99 | }); |
---|
100 | } |
---|
101 | c.addSeries(series.name, values, series.kwArgs).render(); |
---|
102 | } |
---|
103 | }); |
---|
104 | series.data.fetch(kw); |
---|
105 | } |
---|
106 | return render; |
---|
107 | }, false); |
---|
108 | if(render){ c.render(); } |
---|
109 | }, |
---|
110 | destroy: function(){ |
---|
111 | // summary: properly destroy the widget |
---|
112 | this.chart.destroy(); |
---|
113 | this.inherited(arguments); |
---|
114 | }, |
---|
115 | resize: function(box){ |
---|
116 | // summary: |
---|
117 | // Resize the widget. |
---|
118 | // description: |
---|
119 | // Resize the domNode and the widget surface to the dimensions of a box of the following form: |
---|
120 | // `{ l: 50, t: 200, w: 300: h: 150 }` |
---|
121 | // If no box is provided, resize the surface to the marginBox of the domNode. |
---|
122 | // box: |
---|
123 | // If passed, denotes the new size of the widget. |
---|
124 | this.chart.resize(box); |
---|
125 | } |
---|
126 | }); |
---|
127 | |
---|
128 | collectParams = function(node, type, kw){ |
---|
129 | var dp = eval("(" + type + ".prototype.defaultParams)"); |
---|
130 | var x, attr; |
---|
131 | for(x in dp){ |
---|
132 | if(x in kw){ continue; } |
---|
133 | attr = node.getAttribute(x); |
---|
134 | kw[x] = du.coerceType(dp[x], attr == null || typeof attr == "undefined" ? dp[x] : attr); |
---|
135 | } |
---|
136 | var op = eval("(" + type + ".prototype.optionalParams)"); |
---|
137 | for(x in op){ |
---|
138 | if(x in kw){ continue; } |
---|
139 | attr = node.getAttribute(x); |
---|
140 | if(attr != null){ |
---|
141 | kw[x] = du.coerceType(op[x], attr); |
---|
142 | } |
---|
143 | } |
---|
144 | }; |
---|
145 | |
---|
146 | collectAxisParams = function(node){ |
---|
147 | var name = node.getAttribute("name"), type = node.getAttribute("type"); |
---|
148 | if(!name){ return null; } |
---|
149 | var o = {name: name, kwArgs: {}}, kw = o.kwArgs; |
---|
150 | if(type){ |
---|
151 | if(dc.axis2d[type]){ |
---|
152 | type = dojo._scopeName + "x.charting.axis2d." + type; |
---|
153 | } |
---|
154 | var axis = eval("(" + type + ")"); |
---|
155 | if(axis){ kw.type = axis; } |
---|
156 | }else{ |
---|
157 | type = dojo._scopeName + "x.charting.axis2d.Default"; |
---|
158 | } |
---|
159 | collectParams(node, type, kw); |
---|
160 | // compatibility conversions |
---|
161 | if(kw.font || kw.fontColor){ |
---|
162 | if(!kw.tick){ |
---|
163 | kw.tick = {}; |
---|
164 | } |
---|
165 | if(kw.font){ |
---|
166 | kw.tick.font = kw.font; |
---|
167 | } |
---|
168 | if(kw.fontColor){ |
---|
169 | kw.tick.fontColor = kw.fontColor; |
---|
170 | } |
---|
171 | } |
---|
172 | return o; |
---|
173 | }; |
---|
174 | |
---|
175 | collectPlotParams = function(node){ |
---|
176 | // var name = d.attr(node, "name"), type = d.attr(node, "type"); |
---|
177 | var name = node.getAttribute("name"), type = node.getAttribute("type"); |
---|
178 | if(!name){ return null; } |
---|
179 | var o = {name: name, kwArgs: {}}, kw = o.kwArgs; |
---|
180 | if(type){ |
---|
181 | if(dc.plot2d && dc.plot2d[type]){ |
---|
182 | type = dojo._scopeName + "x.charting.plot2d." + type; |
---|
183 | } |
---|
184 | var plot = eval("(" + type + ")"); |
---|
185 | if(plot){ kw.type = plot; } |
---|
186 | }else{ |
---|
187 | type = dojo._scopeName + "x.charting.plot2d.Default"; |
---|
188 | } |
---|
189 | collectParams(node, type, kw); |
---|
190 | return o; |
---|
191 | }; |
---|
192 | |
---|
193 | collectActionParams = function(node){ |
---|
194 | // var plot = d.attr(node, "plot"), type = d.attr(node, "type"); |
---|
195 | var plot = node.getAttribute("plot"), type = node.getAttribute("type"); |
---|
196 | if(!plot){ plot = "default"; } |
---|
197 | var o = {plot: plot, kwArgs: {}}, kw = o.kwArgs; |
---|
198 | if(type){ |
---|
199 | if(dc.action2d[type]){ |
---|
200 | type = dojo._scopeName + "x.charting.action2d." + type; |
---|
201 | } |
---|
202 | var action = eval("(" + type + ")"); |
---|
203 | if(!action){ return null; } |
---|
204 | o.action = action; |
---|
205 | }else{ |
---|
206 | return null; |
---|
207 | } |
---|
208 | collectParams(node, type, kw); |
---|
209 | return o; |
---|
210 | }; |
---|
211 | |
---|
212 | collectDataParams = function(node){ |
---|
213 | var ga = lang.partial(html.attr, node); |
---|
214 | var name = ga("name"); |
---|
215 | if(!name){ return null; } |
---|
216 | var o = { name: name, kwArgs: {} }, kw = o.kwArgs, t; |
---|
217 | t = ga("plot"); |
---|
218 | if(t != null){ kw.plot = t; } |
---|
219 | t = ga("marker"); |
---|
220 | if(t != null){ kw.marker = t; } |
---|
221 | t = ga("stroke"); |
---|
222 | if(t != null){ kw.stroke = eval("(" + t + ")"); } |
---|
223 | t = ga("outline"); |
---|
224 | if(t != null){ kw.outline = eval("(" + t + ")"); } |
---|
225 | t = ga("shadow"); |
---|
226 | if(t != null){ kw.shadow = eval("(" + t + ")"); } |
---|
227 | t = ga("fill"); |
---|
228 | if(t != null){ kw.fill = eval("(" + t + ")"); } |
---|
229 | t = ga("font"); |
---|
230 | if(t != null){ kw.font = t; } |
---|
231 | t = ga("fontColor"); |
---|
232 | if(t != null){ kw.fontColor = eval("(" + t + ")"); } |
---|
233 | t = ga("legend"); |
---|
234 | if(t != null){ kw.legend = t; } |
---|
235 | t = ga("data"); |
---|
236 | if(t != null){ |
---|
237 | o.type = "data"; |
---|
238 | o.data = t ? arr.map(String(t).split(','), Number) : []; |
---|
239 | return o; |
---|
240 | } |
---|
241 | t = ga("array"); |
---|
242 | if(t != null){ |
---|
243 | o.type = "data"; |
---|
244 | o.data = eval("(" + t + ")"); |
---|
245 | return o; |
---|
246 | } |
---|
247 | t = ga("store"); |
---|
248 | if(t != null){ |
---|
249 | o.type = "store"; |
---|
250 | o.data = eval("(" + t + ")"); |
---|
251 | t = ga("field"); |
---|
252 | o.field = t != null ? t : "value"; |
---|
253 | t = ga("query"); |
---|
254 | if(!!t){ kw.query = t; } |
---|
255 | t = ga("queryOptions"); |
---|
256 | if(!!t){ kw.queryOptions = eval("(" + t + ")"); } |
---|
257 | t = ga("start"); |
---|
258 | if(!!t){ kw.start = Number(t); } |
---|
259 | t = ga("count"); |
---|
260 | if(!!t){ kw.count = Number(t); } |
---|
261 | t = ga("sort"); |
---|
262 | if(!!t){ kw.sort = eval("("+t+")"); } |
---|
263 | t = ga("valueFn"); |
---|
264 | if(!!t){ kw.valueFn = dfl.lambda(t); } |
---|
265 | return o; |
---|
266 | } |
---|
267 | return null; |
---|
268 | }; |
---|
269 | |
---|
270 | return ChartWidget; |
---|
271 | }); |
---|