StackedBars.js 4.7 KB

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