Bars.js 5.9 KB

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