StackedBars.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. define("dojox/charting/plot2d/StackedBars", ["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "./Bars", "./common",
  2. "dojox/lang/functional", "dojox/lang/functional/reversed", "dojox/lang/functional/sequence"],
  3. function(lang, arr, declare, Bars, dc, df, dfr, dfs){
  4. var purgeGroup = dfr.lambda("item.purgeGroup()");
  5. /*=====
  6. var bars = dojox.charting.plot2d.Bars;
  7. =====*/
  8. return declare("dojox.charting.plot2d.StackedBars", Bars, {
  9. // summary:
  10. // The plot object representing a stacked bar chart (horizontal bars).
  11. getSeriesStats: function(){
  12. // summary:
  13. // Calculate the min/max on all attached series in both directions.
  14. // returns: Object
  15. // {hmin, hmax, vmin, vmax} min/max in both directions.
  16. var stats = dc.collectStackedStats(this.series), t;
  17. this._maxRunLength = stats.hmax;
  18. stats.hmin -= 0.5;
  19. stats.hmax += 0.5;
  20. t = stats.hmin, stats.hmin = stats.vmin, stats.vmin = t;
  21. t = stats.hmax, stats.hmax = stats.vmax, stats.vmax = t;
  22. return stats;
  23. },
  24. render: function(dim, offsets){
  25. // summary:
  26. // Run the calculations for any axes for this plot.
  27. // dim: Object
  28. // An object in the form of { width, height }
  29. // offsets: Object
  30. // An object of the form { l, r, t, b}.
  31. // returns: dojox.charting.plot2d.StackedBars
  32. // A reference to this plot for functional chaining.
  33. if(this._maxRunLength <= 0){
  34. return this;
  35. }
  36. // stack all values
  37. var acc = df.repeat(this._maxRunLength, "-> 0", 0);
  38. for(var i = 0; i < this.series.length; ++i){
  39. var run = this.series[i];
  40. for(var j = 0; j < run.data.length; ++j){
  41. var value = run.data[j];
  42. if(value !== null){
  43. var v = typeof value == "number" ? value : value.y;
  44. if(isNaN(v)){ v = 0; }
  45. acc[j] += v;
  46. }
  47. }
  48. }
  49. // draw runs in backwards
  50. if(this.zoom && !this.isDataDirty()){
  51. return this.performZoom(dim, offsets);
  52. }
  53. this.resetEvents();
  54. this.dirty = this.isDirty();
  55. if(this.dirty){
  56. arr.forEach(this.series, purgeGroup);
  57. this._eventSeries = {};
  58. this.cleanGroup();
  59. var s = this.group;
  60. df.forEachRev(this.series, function(item){ item.cleanGroup(s); });
  61. }
  62. var t = this.chart.theme, f, gap, height,
  63. ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler),
  64. vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler),
  65. events = this.events();
  66. f = dc.calculateBarSize(this._vScaler.bounds.scale, this.opt);
  67. gap = f.gap;
  68. height = f.size;
  69. for(var i = this.series.length - 1; i >= 0; --i){
  70. var run = this.series[i];
  71. if(!this.dirty && !run.dirty){
  72. t.skip();
  73. this._reconnectEvents(run.name);
  74. continue;
  75. }
  76. run.cleanGroup();
  77. var theme = t.next("bar", [this.opt, run]), s = run.group,
  78. eventSeries = new Array(acc.length);
  79. for(var j = 0; j < acc.length; ++j){
  80. var value = run.data[j];
  81. if(value !== null){
  82. var v = acc[j],
  83. width = ht(v),
  84. finalTheme = typeof value != "number" ?
  85. t.addMixin(theme, "bar", value, true) :
  86. t.post(theme, "bar");
  87. if(width >= 0 && height >= 1){
  88. var rect = {
  89. x: offsets.l,
  90. y: dim.height - offsets.b - vt(j + 1.5) + gap,
  91. width: width, height: height
  92. };
  93. var specialFill = this._plotFill(finalTheme.series.fill, dim, offsets);
  94. specialFill = this._shapeFill(specialFill, rect);
  95. var shape = s.createRect(rect).setFill(specialFill).setStroke(finalTheme.series.stroke);
  96. run.dyn.fill = shape.getFill();
  97. run.dyn.stroke = shape.getStroke();
  98. if(events){
  99. var o = {
  100. element: "bar",
  101. index: j,
  102. run: run,
  103. shape: shape,
  104. x: v,
  105. y: j + 1.5
  106. };
  107. this._connectEvents(o);
  108. eventSeries[j] = o;
  109. }
  110. if(this.animate){
  111. this._animateBar(shape, offsets.l, -width);
  112. }
  113. }
  114. }
  115. }
  116. this._eventSeries[run.name] = eventSeries;
  117. run.dirty = false;
  118. // update the accumulator
  119. for(var j = 0; j < run.data.length; ++j){
  120. var value = run.data[j];
  121. if(value !== null){
  122. var v = typeof value == "number" ? value : value.y;
  123. if(isNaN(v)){ v = 0; }
  124. acc[j] -= v;
  125. }
  126. }
  127. }
  128. this.dirty = false;
  129. return this; // dojox.charting.plot2d.StackedBars
  130. }
  131. });
  132. });