ClusteredBars.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. define("dojox/charting/plot2d/ClusteredBars", ["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "./Bars", "./common",
  2. "dojox/lang/functional", "dojox/lang/functional/reversed", "dojox/lang/utils"],
  3. function(lang, arr, declare, Bars, dc, df, dfr, du){
  4. /*=====
  5. var Bars = dojox.charting.plot2d.Bars;
  6. =====*/
  7. var purgeGroup = dfr.lambda("item.purgeGroup()");
  8. return declare("dojox.charting.plot2d.ClusteredBars", Bars, {
  9. // summary:
  10. // A plot representing grouped or clustered bars (horizontal bars)
  11. render: function(dim, offsets){
  12. // summary:
  13. // Run the calculations for any axes for this plot.
  14. // dim: Object
  15. // An object in the form of { width, height }
  16. // offsets: Object
  17. // An object of the form { l, r, t, b}.
  18. // returns: dojox.charting.plot2d.ClusteredBars
  19. // A reference to this plot for functional chaining.
  20. if(this.zoom && !this.isDataDirty()){
  21. return this.performZoom(dim, offsets);
  22. }
  23. this.resetEvents();
  24. this.dirty = this.isDirty();
  25. if(this.dirty){
  26. arr.forEach(this.series, purgeGroup);
  27. this._eventSeries = {};
  28. this.cleanGroup();
  29. var s = this.group;
  30. df.forEachRev(this.series, function(item){ item.cleanGroup(s); });
  31. }
  32. var t = this.chart.theme, f, gap, height, thickness,
  33. ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler),
  34. vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler),
  35. baseline = Math.max(0, this._hScaler.bounds.lower),
  36. baselineWidth = ht(baseline),
  37. events = this.events();
  38. f = dc.calculateBarSize(this._vScaler.bounds.scale, this.opt, this.series.length);
  39. gap = f.gap;
  40. height = thickness = f.size;
  41. for(var i = this.series.length - 1; i >= 0; --i){
  42. var run = this.series[i], shift = thickness * (this.series.length - i - 1);
  43. if(!this.dirty && !run.dirty){
  44. t.skip();
  45. this._reconnectEvents(run.name);
  46. continue;
  47. }
  48. run.cleanGroup();
  49. var theme = t.next("bar", [this.opt, run]), s = run.group,
  50. eventSeries = new Array(run.data.length);
  51. for(var j = 0; j < run.data.length; ++j){
  52. var value = run.data[j];
  53. if(value !== null){
  54. var v = typeof value == "number" ? value : value.y,
  55. hv = ht(v),
  56. width = hv - baselineWidth,
  57. w = Math.abs(width),
  58. finalTheme = typeof value != "number" ?
  59. t.addMixin(theme, "bar", value, true) :
  60. t.post(theme, "bar");
  61. if(w >= 0 && height >= 1){
  62. var rect = {
  63. x: offsets.l + (v < baseline ? hv : baselineWidth),
  64. y: dim.height - offsets.b - vt(j + 1.5) + gap + shift,
  65. width: w, height: height
  66. };
  67. var specialFill = this._plotFill(finalTheme.series.fill, dim, offsets);
  68. specialFill = this._shapeFill(specialFill, rect);
  69. var shape = s.createRect(rect).setFill(specialFill).setStroke(finalTheme.series.stroke);
  70. run.dyn.fill = shape.getFill();
  71. run.dyn.stroke = shape.getStroke();
  72. if(events){
  73. var o = {
  74. element: "bar",
  75. index: j,
  76. run: run,
  77. shape: shape,
  78. x: v,
  79. y: j + 1.5
  80. };
  81. this._connectEvents(o);
  82. eventSeries[j] = o;
  83. }
  84. if(this.animate){
  85. this._animateBar(shape, offsets.l + baselineWidth, -width);
  86. }
  87. }
  88. }
  89. }
  90. this._eventSeries[run.name] = eventSeries;
  91. run.dirty = false;
  92. }
  93. this.dirty = false;
  94. return this; // dojox.charting.plot2d.ClusteredBars
  95. }
  96. });
  97. });