Chart.js 7.4 KB

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