Chart.js 7.5 KB

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