Columns.js 5.5 KB

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