Bars.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. define("dojox/charting/plot2d/Bars", ["dojo/_base/kernel", "dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "./Base", "./common",
  2. "dojox/gfx/fx", "dojox/lang/utils", "dojox/lang/functional", "dojox/lang/functional/reversed"],
  3. function(dojo, lang, arr, declare, Base, dc, fx, du, df, dfr){
  4. /*=====
  5. dojo.declare("dojox.charting.plot2d.__BarCtorArgs", dojox.charting.plot2d.__DefaultCtorArgs, {
  6. // summary:
  7. // Additional keyword arguments for bar charts.
  8. // minBarSize: Number?
  9. // The minimum size for a bar in pixels. Default is 1.
  10. minBarSize: 1,
  11. // maxBarSize: Number?
  12. // The maximum size for a bar in pixels. Default is 1.
  13. maxBarSize: 1,
  14. // enableCache: Boolean?
  15. // Whether the bars rect are cached from one rendering to another. This improves the rendering performance of
  16. // successive rendering but penalize the first rendering. Default false.
  17. enableCache: false
  18. });
  19. var Base = dojox.charting.plot2d.Base;
  20. =====*/
  21. var purgeGroup = dfr.lambda("item.purgeGroup()");
  22. return declare("dojox.charting.plot2d.Bars", Base, {
  23. // summary:
  24. // The plot object representing a bar chart (horizontal bars).
  25. defaultParams: {
  26. hAxis: "x", // use a horizontal axis named "x"
  27. vAxis: "y", // use a vertical axis named "y"
  28. gap: 0, // gap between columns in pixels
  29. animate: null, // animate bars into place
  30. enableCache: false
  31. },
  32. optionalParams: {
  33. minBarSize: 1, // minimal bar width in pixels
  34. maxBarSize: 1, // maximal bar width in pixels
  35. // theme component
  36. stroke: {},
  37. outline: {},
  38. shadow: {},
  39. fill: {},
  40. font: "",
  41. fontColor: ""
  42. },
  43. constructor: function(chart, kwArgs){
  44. // summary:
  45. // The constructor for a bar chart.
  46. // chart: dojox.charting.Chart
  47. // The chart this plot belongs to.
  48. // kwArgs: dojox.charting.plot2d.__BarCtorArgs?
  49. // An optional keyword arguments object to help define the plot.
  50. this.opt = lang.clone(this.defaultParams);
  51. du.updateWithObject(this.opt, kwArgs);
  52. du.updateWithPattern(this.opt, kwArgs, this.optionalParams);
  53. this.series = [];
  54. this.hAxis = this.opt.hAxis;
  55. this.vAxis = this.opt.vAxis;
  56. this.animate = this.opt.animate;
  57. },
  58. getSeriesStats: function(){
  59. // summary:
  60. // Calculate the min/max on all attached series in both directions.
  61. // returns: Object
  62. // {hmin, hmax, vmin, vmax} min/max in both directions.
  63. var stats = dc.collectSimpleStats(this.series), t;
  64. stats.hmin -= 0.5;
  65. stats.hmax += 0.5;
  66. t = stats.hmin, stats.hmin = stats.vmin, stats.vmin = t;
  67. t = stats.hmax, stats.hmax = stats.vmax, stats.vmax = t;
  68. return stats;
  69. },
  70. createRect: function(run, creator, params){
  71. var rect;
  72. if(this.opt.enableCache && run._rectFreePool.length > 0){
  73. rect = run._rectFreePool.pop();
  74. rect.setShape(params);
  75. // was cleared, add it back
  76. creator.add(rect);
  77. }else{
  78. rect = creator.createRect(params);
  79. }
  80. if(this.opt.enableCache){
  81. run._rectUsePool.push(rect);
  82. }
  83. return rect;
  84. },
  85. render: function(dim, offsets){
  86. // summary:
  87. // Run the calculations for any axes for this plot.
  88. // dim: Object
  89. // An object in the form of { width, height }
  90. // offsets: Object
  91. // An object of the form { l, r, t, b}.
  92. // returns: dojox.charting.plot2d.Bars
  93. // A reference to this plot for functional chaining.
  94. if(this.zoom && !this.isDataDirty()){
  95. return this.performZoom(dim, offsets);
  96. }
  97. this.dirty = this.isDirty();
  98. this.resetEvents();
  99. if(this.dirty){
  100. arr.forEach(this.series, purgeGroup);
  101. this._eventSeries = {};
  102. this.cleanGroup();
  103. var s = this.group;
  104. df.forEachRev(this.series, function(item){ item.cleanGroup(s); });
  105. }
  106. var t = this.chart.theme, f, gap, height,
  107. ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler),
  108. vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler),
  109. baseline = Math.max(0, this._hScaler.bounds.lower),
  110. baselineWidth = ht(baseline),
  111. events = this.events();
  112. f = dc.calculateBarSize(this._vScaler.bounds.scale, this.opt);
  113. gap = f.gap;
  114. height = f.size;
  115. for(var i = this.series.length - 1; i >= 0; --i){
  116. var run = this.series[i];
  117. if(!this.dirty && !run.dirty){
  118. t.skip();
  119. this._reconnectEvents(run.name);
  120. continue;
  121. }
  122. run.cleanGroup();
  123. if(this.opt.enableCache){
  124. run._rectFreePool = (run._rectFreePool?run._rectFreePool:[]).concat(run._rectUsePool?run._rectUsePool:[]);
  125. run._rectUsePool = [];
  126. }
  127. var theme = t.next("bar", [this.opt, run]), s = run.group,
  128. eventSeries = new Array(run.data.length);
  129. for(var j = 0; j < run.data.length; ++j){
  130. var value = run.data[j];
  131. if(value !== null){
  132. var v = typeof value == "number" ? value : value.y,
  133. hv = ht(v),
  134. width = hv - baselineWidth,
  135. w = Math.abs(width),
  136. finalTheme = typeof value != "number" ?
  137. t.addMixin(theme, "bar", value, true) :
  138. t.post(theme, "bar");
  139. if(w >= 0 && height >= 1){
  140. var rect = {
  141. x: offsets.l + (v < baseline ? hv : baselineWidth),
  142. y: dim.height - offsets.b - vt(j + 1.5) + gap,
  143. width: w, height: height
  144. };
  145. var specialFill = this._plotFill(finalTheme.series.fill, dim, offsets);
  146. specialFill = this._shapeFill(specialFill, rect);
  147. var shape = this.createRect(run, s, rect).setFill(specialFill).setStroke(finalTheme.series.stroke);
  148. run.dyn.fill = shape.getFill();
  149. run.dyn.stroke = shape.getStroke();
  150. if(events){
  151. var o = {
  152. element: "bar",
  153. index: j,
  154. run: run,
  155. shape: shape,
  156. x: v,
  157. y: j + 1.5
  158. };
  159. this._connectEvents(o);
  160. eventSeries[j] = o;
  161. }
  162. if(this.animate){
  163. this._animateBar(shape, offsets.l + baselineWidth, -w);
  164. }
  165. }
  166. }
  167. }
  168. this._eventSeries[run.name] = eventSeries;
  169. run.dirty = false;
  170. }
  171. this.dirty = false;
  172. return this; // dojox.charting.plot2d.Bars
  173. },
  174. _animateBar: function(shape, hoffset, hsize){
  175. fx.animateTransform(lang.delegate({
  176. shape: shape,
  177. duration: 1200,
  178. transform: [
  179. {name: "translate", start: [hoffset - (hoffset/hsize), 0], end: [0, 0]},
  180. {name: "scale", start: [1/hsize, 1], end: [1, 1]},
  181. {name: "original"}
  182. ]
  183. }, this.animate)).play();
  184. }
  185. });
  186. });