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