StackedColumns.js 4.6 KB

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