2150 |
mathias |
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(kw, series.kwArgs, {"query": "", "queryOptions": null, "start": 0, "count": 1, "sort": []}, true);
|
|
|
64 |
dojo.mixin(kw, {
|
|
|
65 |
onComplete: function(data){
|
|
|
66 |
var values;
|
|
|
67 |
if("valueFn" in series.kwArgs){
|
|
|
68 |
var fn = series.kwArgs.valueFn;
|
|
|
69 |
values = dojo.map(data, function(x){
|
|
|
70 |
return fn(series.data.getValue(x, series.field, 0));
|
|
|
71 |
});
|
|
|
72 |
}else{
|
|
|
73 |
values = dojo.map(data, function(x){
|
|
|
74 |
return series.data.getValue(x, series.field, 0);
|
|
|
75 |
});
|
|
|
76 |
}
|
|
|
77 |
c.addSeries(series.name, values, series.kwArgs).render();
|
|
|
78 |
}
|
|
|
79 |
});
|
|
|
80 |
series.data.fetch(kw);
|
|
|
81 |
}
|
|
|
82 |
return render;
|
|
|
83 |
}, false);
|
|
|
84 |
if(render){ c.render(); }
|
|
|
85 |
},
|
|
|
86 |
resize: function(box){
|
|
|
87 |
dojo.marginBox(this.domNode, box);
|
|
|
88 |
this.chart.resize();
|
|
|
89 |
}
|
|
|
90 |
});
|
|
|
91 |
|
|
|
92 |
collectAxisParams = function(node){
|
|
|
93 |
var name = node.getAttribute("name"), type = node.getAttribute("type");
|
|
|
94 |
if(!name){ return null; }
|
|
|
95 |
var o = {name: name, kwArgs: {}}, kw = o.kwArgs;
|
|
|
96 |
if(type){
|
|
|
97 |
if(dojox.charting.axis2d[type]){
|
|
|
98 |
type = "dojox.charting.axis2d." + type;
|
|
|
99 |
}
|
|
|
100 |
var axis = eval("(" + type + ")");
|
|
|
101 |
if(axis){ kw.type = axis; }
|
|
|
102 |
}else{
|
|
|
103 |
type = "dojox.charting.axis2d.Default";
|
|
|
104 |
}
|
|
|
105 |
var dp = eval("(" + type + ".prototype.defaultParams)");
|
|
|
106 |
for(var x in dp){
|
|
|
107 |
if(x in kw){ continue; }
|
|
|
108 |
var attr = node.getAttribute(x);
|
|
|
109 |
kw[x] = du.coerceType(dp[x], attr == null ? dp[x] : attr);
|
|
|
110 |
}
|
|
|
111 |
var op = eval("(" + type + ".prototype.optionalParams)");
|
|
|
112 |
for(var x in op){
|
|
|
113 |
if(x in kw){ continue; }
|
|
|
114 |
var attr = node.getAttribute(x);
|
|
|
115 |
if(attr != null){
|
|
|
116 |
kw[x] = du.coerceType(op[x], attr);
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
return o;
|
|
|
120 |
};
|
|
|
121 |
|
|
|
122 |
collectPlotParams = function(node){
|
|
|
123 |
var name = node.getAttribute("name"), type = node.getAttribute("type");
|
|
|
124 |
if(!name){ return null; }
|
|
|
125 |
var o = {name: name, kwArgs: {}}, kw = o.kwArgs;
|
|
|
126 |
if(type){
|
|
|
127 |
if(dojox.charting.plot2d[type]){
|
|
|
128 |
type = "dojox.charting.plot2d." + type;
|
|
|
129 |
}
|
|
|
130 |
var plot = eval("(" + type + ")");
|
|
|
131 |
if(plot){ kw.type = plot; }
|
|
|
132 |
}else{
|
|
|
133 |
type = "dojox.charting.plot2d.Default";
|
|
|
134 |
}
|
|
|
135 |
var dp = eval("(" + type + ".prototype.defaultParams)");
|
|
|
136 |
for(var x in dp){
|
|
|
137 |
if(x in kw){ continue; }
|
|
|
138 |
var attr = node.getAttribute(x);
|
|
|
139 |
kw[x] = du.coerceType(dp[x], attr == null ? dp[x] : attr);
|
|
|
140 |
}
|
|
|
141 |
var op = eval("(" + type + ".prototype.optionalParams)");
|
|
|
142 |
for(var x in op){
|
|
|
143 |
if(x in kw){ continue; }
|
|
|
144 |
var attr = node.getAttribute(x);
|
|
|
145 |
if(attr != null){
|
|
|
146 |
kw[x] = du.coerceType(op[x], attr);
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
return o;
|
|
|
150 |
};
|
|
|
151 |
|
|
|
152 |
collectDataParams = function(node){
|
|
|
153 |
var name = node.getAttribute("name");
|
|
|
154 |
if(!name){ return null; }
|
|
|
155 |
var o = {name: name, kwArgs: {}}, kw = o.kwArgs, t;
|
|
|
156 |
t = node.getAttribute("plot");
|
|
|
157 |
if(t != null){ kw.plot = t; }
|
|
|
158 |
t = node.getAttribute("marker");
|
|
|
159 |
if(t != null){ kw.marker = t; }
|
|
|
160 |
t = node.getAttribute("stroke");
|
|
|
161 |
if(t != null){ kw.stroke = eval("(" + t + ")"); }
|
|
|
162 |
t = node.getAttribute("fill");
|
|
|
163 |
if(t != null){ kw.fill = eval("(" + t + ")"); }
|
|
|
164 |
t = node.getAttribute("data");
|
|
|
165 |
if(t != null){
|
|
|
166 |
o.type = "data";
|
|
|
167 |
o.data = dojo.map(String(t).split(','), Number);
|
|
|
168 |
return o;
|
|
|
169 |
}
|
|
|
170 |
t = node.getAttribute("array");
|
|
|
171 |
if(t != null){
|
|
|
172 |
o.type = "data";
|
|
|
173 |
o.data = eval("(" + t + ")");
|
|
|
174 |
return o;
|
|
|
175 |
}
|
|
|
176 |
t = node.getAttribute("store");
|
|
|
177 |
if(t != null){
|
|
|
178 |
o.type = "store";
|
|
|
179 |
o.data = eval("(" + t + ")");
|
|
|
180 |
t = node.getAttribute("field");
|
|
|
181 |
o.field = t != null ? t : "value";
|
|
|
182 |
t = node.getAttribute("query");
|
|
|
183 |
if(t != null){ kw.query = t; }
|
|
|
184 |
t = node.getAttribute("queryOptions");
|
|
|
185 |
if(t != null){ kw.queryOptions = eval("(" + t + ")"); }
|
|
|
186 |
t = node.getAttribute("start");
|
|
|
187 |
if(t != null){ kw.start = Number(t); }
|
|
|
188 |
t = node.getAttribute("count");
|
|
|
189 |
if(t != null){ kw.count = Number(t); }
|
|
|
190 |
t = node.getAttribute("sort");
|
|
|
191 |
if(t != null){ kw.sort = eval("(" + t + ")"); }
|
|
|
192 |
t = node.getAttribute("valueFn");
|
|
|
193 |
if(t != null){ kw.valueFn = df.lambda(t); }
|
|
|
194 |
return o;
|
|
|
195 |
}
|
|
|
196 |
return null;
|
|
|
197 |
};
|
|
|
198 |
})();
|
|
|
199 |
|
|
|
200 |
}
|