Stacked.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. define("dojox/charting/plot2d/Stacked", ["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "./Default", "./common",
  2. "dojox/lang/functional", "dojox/lang/functional/reversed", "dojox/lang/functional/sequence"],
  3. function(lang, declare, arr, Default, dc, df, dfr, dfs){
  4. /*=====
  5. var Default = dojox.charting.plot2d.Default;
  6. =====*/
  7. var purgeGroup = dfr.lambda("item.purgeGroup()");
  8. return declare("dojox.charting.plot2d.Stacked", Default, {
  9. // summary:
  10. // Like the default plot, Stacked sets up lines, areas and markers
  11. // in a stacked fashion (values on the y axis added to each other)
  12. // as opposed to a direct one.
  13. getSeriesStats: function(){
  14. // summary:
  15. // Calculate the min/max on all attached series in both directions.
  16. // returns: Object
  17. // {hmin, hmax, vmin, vmax} min/max in both directions.
  18. var stats = dc.collectStackedStats(this.series);
  19. this._maxRunLength = stats.hmax;
  20. return stats;
  21. },
  22. render: function(dim, offsets){
  23. // summary:
  24. // Run the calculations for any axes for this plot.
  25. // dim: Object
  26. // An object in the form of { width, height }
  27. // offsets: Object
  28. // An object of the form { l, r, t, b}.
  29. // returns: dojox.charting.plot2d.Stacked
  30. // A reference to this plot for functional chaining.
  31. if(this._maxRunLength <= 0){
  32. return this;
  33. }
  34. // stack all values
  35. var acc = df.repeat(this._maxRunLength, "-> 0", 0);
  36. for(var i = 0; i < this.series.length; ++i){
  37. var run = this.series[i];
  38. for(var j = 0; j < run.data.length; ++j){
  39. var v = run.data[j];
  40. if(v !== null){
  41. if(isNaN(v)){ v = 0; }
  42. acc[j] += v;
  43. }
  44. }
  45. }
  46. // draw runs in backwards
  47. if(this.zoom && !this.isDataDirty()){
  48. return this.performZoom(dim, offsets);
  49. }
  50. this.resetEvents();
  51. this.dirty = this.isDirty();
  52. if(this.dirty){
  53. arr.forEach(this.series, purgeGroup);
  54. this._eventSeries = {};
  55. this.cleanGroup();
  56. var s = this.group;
  57. df.forEachRev(this.series, function(item){ item.cleanGroup(s); });
  58. }
  59. var t = this.chart.theme, events = this.events(),
  60. ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler),
  61. vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler);
  62. for(var i = this.series.length - 1; i >= 0; --i){
  63. var run = this.series[i];
  64. if(!this.dirty && !run.dirty){
  65. t.skip();
  66. this._reconnectEvents(run.name);
  67. continue;
  68. }
  69. run.cleanGroup();
  70. var theme = t.next(this.opt.areas ? "area" : "line", [this.opt, run], true),
  71. s = run.group, outline,
  72. lpoly = arr.map(acc, function(v, i){
  73. return {
  74. x: ht(i + 1) + offsets.l,
  75. y: dim.height - offsets.b - vt(v)
  76. };
  77. }, this);
  78. var lpath = this.opt.tension ? dc.curve(lpoly, this.opt.tension) : "";
  79. if(this.opt.areas){
  80. var apoly = lang.clone(lpoly);
  81. if(this.opt.tension){
  82. var p=dc.curve(apoly, this.opt.tension);
  83. p += " L" + lpoly[lpoly.length - 1].x + "," + (dim.height - offsets.b) +
  84. " L" + lpoly[0].x + "," + (dim.height - offsets.b) +
  85. " L" + lpoly[0].x + "," + lpoly[0].y;
  86. run.dyn.fill = s.createPath(p).setFill(theme.series.fill).getFill();
  87. } else {
  88. apoly.push({x: lpoly[lpoly.length - 1].x, y: dim.height - offsets.b});
  89. apoly.push({x: lpoly[0].x, y: dim.height - offsets.b});
  90. apoly.push(lpoly[0]);
  91. run.dyn.fill = s.createPolyline(apoly).setFill(theme.series.fill).getFill();
  92. }
  93. }
  94. if(this.opt.lines || this.opt.markers){
  95. if(theme.series.outline){
  96. outline = dc.makeStroke(theme.series.outline);
  97. outline.width = 2 * outline.width + theme.series.stroke.width;
  98. }
  99. }
  100. if(this.opt.markers){
  101. run.dyn.marker = theme.symbol;
  102. }
  103. var frontMarkers, outlineMarkers, shadowMarkers;
  104. if(theme.series.shadow && theme.series.stroke){
  105. var shadow = theme.series.shadow,
  106. spoly = arr.map(lpoly, function(c){
  107. return {x: c.x + shadow.dx, y: c.y + shadow.dy};
  108. });
  109. if(this.opt.lines){
  110. if(this.opt.tension){
  111. run.dyn.shadow = s.createPath(dc.curve(spoly, this.opt.tension)).setStroke(shadow).getStroke();
  112. } else {
  113. run.dyn.shadow = s.createPolyline(spoly).setStroke(shadow).getStroke();
  114. }
  115. }
  116. if(this.opt.markers){
  117. shadow = theme.marker.shadow;
  118. shadowMarkers = arr.map(spoly, function(c){
  119. return s.createPath("M" + c.x + " " + c.y + " " + theme.symbol).
  120. setStroke(shadow).setFill(shadow.color);
  121. }, this);
  122. }
  123. }
  124. if(this.opt.lines){
  125. if(outline){
  126. if(this.opt.tension){
  127. run.dyn.outline = s.createPath(lpath).setStroke(outline).getStroke();
  128. } else {
  129. run.dyn.outline = s.createPolyline(lpoly).setStroke(outline).getStroke();
  130. }
  131. }
  132. if(this.opt.tension){
  133. run.dyn.stroke = s.createPath(lpath).setStroke(theme.series.stroke).getStroke();
  134. } else {
  135. run.dyn.stroke = s.createPolyline(lpoly).setStroke(theme.series.stroke).getStroke();
  136. }
  137. }
  138. if(this.opt.markers){
  139. frontMarkers = new Array(lpoly.length);
  140. outlineMarkers = new Array(lpoly.length);
  141. outline = null;
  142. if(theme.marker.outline){
  143. outline = dc.makeStroke(theme.marker.outline);
  144. outline.width = 2 * outline.width + (theme.marker.stroke ? theme.marker.stroke.width : 0);
  145. }
  146. arr.forEach(lpoly, function(c, i){
  147. var path = "M" + c.x + " " + c.y + " " + theme.symbol;
  148. if(outline){
  149. outlineMarkers[i] = s.createPath(path).setStroke(outline);
  150. }
  151. frontMarkers[i] = s.createPath(path).setStroke(theme.marker.stroke).setFill(theme.marker.fill);
  152. }, this);
  153. if(events){
  154. var eventSeries = new Array(frontMarkers.length);
  155. arr.forEach(frontMarkers, function(s, i){
  156. var o = {
  157. element: "marker",
  158. index: i,
  159. run: run,
  160. shape: s,
  161. outline: outlineMarkers[i] || null,
  162. shadow: shadowMarkers && shadowMarkers[i] || null,
  163. cx: lpoly[i].x,
  164. cy: lpoly[i].y,
  165. x: i + 1,
  166. y: run.data[i]
  167. };
  168. this._connectEvents(o);
  169. eventSeries[i] = o;
  170. }, this);
  171. this._eventSeries[run.name] = eventSeries;
  172. }else{
  173. delete this._eventSeries[run.name];
  174. }
  175. }
  176. run.dirty = false;
  177. // update the accumulator
  178. for(var j = 0; j < run.data.length; ++j){
  179. var v = run.data[j];
  180. if(v !== null){
  181. if(isNaN(v)){ v = 0; }
  182. acc[j] -= v;
  183. }
  184. }
  185. }
  186. this.dirty = false;
  187. return this; // dojox.charting.plot2d.Stacked
  188. }
  189. });
  190. });