Bubble.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. define("dojox/charting/plot2d/Bubble", ["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array",
  2. "./Base", "./common", "dojox/lang/functional", "dojox/lang/functional/reversed",
  3. "dojox/lang/utils", "dojox/gfx/fx"],
  4. function(lang, declare, arr, Base, dc, df, dfr, du, fx){
  5. /*=====
  6. var Base = dojox.charting.plot2d.Base;
  7. =====*/
  8. var purgeGroup = dfr.lambda("item.purgeGroup()");
  9. return declare("dojox.charting.plot2d.Bubble", Base, {
  10. // summary:
  11. // A plot representing bubbles. Note that data for Bubbles requires 3 parameters,
  12. // in the form of: { x, y, size }, where size determines the size of the bubble.
  13. defaultParams: {
  14. hAxis: "x", // use a horizontal axis named "x"
  15. vAxis: "y", // use a vertical axis named "y"
  16. animate: null // animate bars into place
  17. },
  18. optionalParams: {
  19. // theme component
  20. stroke: {},
  21. outline: {},
  22. shadow: {},
  23. fill: {},
  24. font: "",
  25. fontColor: ""
  26. },
  27. constructor: function(chart, kwArgs){
  28. // summary:
  29. // Create a plot of bubbles.
  30. // chart: dojox.charting.Chart
  31. // The chart this plot belongs to.
  32. // kwArgs: dojox.charting.plot2d.__DefaultCtorArgs?
  33. // Optional keyword arguments object to help define plot parameters.
  34. this.opt = lang.clone(this.defaultParams);
  35. du.updateWithObject(this.opt, kwArgs);
  36. du.updateWithPattern(this.opt, kwArgs, this.optionalParams);
  37. this.series = [];
  38. this.hAxis = this.opt.hAxis;
  39. this.vAxis = this.opt.vAxis;
  40. this.animate = this.opt.animate;
  41. },
  42. // override the render so that we are plotting only circles.
  43. render: function(dim, offsets){
  44. // summary:
  45. // Run the calculations for any axes for this plot.
  46. // dim: Object
  47. // An object in the form of { width, height }
  48. // offsets: Object
  49. // An object of the form { l, r, t, b}.
  50. // returns: dojox.charting.plot2d.Bubble
  51. // A reference to this plot for functional chaining.
  52. if(this.zoom && !this.isDataDirty()){
  53. return this.performZoom(dim, offsets);
  54. }
  55. this.resetEvents();
  56. this.dirty = this.isDirty();
  57. if(this.dirty){
  58. arr.forEach(this.series, purgeGroup);
  59. this._eventSeries = {};
  60. this.cleanGroup();
  61. var s = this.group;
  62. df.forEachRev(this.series, function(item){ item.cleanGroup(s); });
  63. }
  64. var t = this.chart.theme,
  65. ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler),
  66. vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler),
  67. events = this.events();
  68. for(var i = this.series.length - 1; i >= 0; --i){
  69. var run = this.series[i];
  70. if(!this.dirty && !run.dirty){
  71. t.skip();
  72. this._reconnectEvents(run.name);
  73. continue;
  74. }
  75. run.cleanGroup();
  76. if(!run.data.length){
  77. run.dirty = false;
  78. t.skip();
  79. continue;
  80. }
  81. if(typeof run.data[0] == "number"){
  82. console.warn("dojox.charting.plot2d.Bubble: the data in the following series cannot be rendered as a bubble chart; ", run);
  83. continue;
  84. }
  85. var theme = t.next("circle", [this.opt, run]), s = run.group,
  86. points = arr.map(run.data, function(v, i){
  87. return v ? {
  88. x: ht(v.x) + offsets.l,
  89. y: dim.height - offsets.b - vt(v.y),
  90. radius: this._vScaler.bounds.scale * (v.size / 2)
  91. } : null;
  92. }, this);
  93. var frontCircles = null, outlineCircles = null, shadowCircles = null;
  94. // make shadows if needed
  95. if(theme.series.shadow){
  96. shadowCircles = arr.map(points, function(item){
  97. if(item !== null){
  98. var finalTheme = t.addMixin(theme, "circle", item, true),
  99. shadow = finalTheme.series.shadow;
  100. var shape = s.createCircle({
  101. cx: item.x + shadow.dx, cy: item.y + shadow.dy, r: item.radius
  102. }).setStroke(shadow).setFill(shadow.color);
  103. if(this.animate){
  104. this._animateBubble(shape, dim.height - offsets.b, item.radius);
  105. }
  106. return shape;
  107. }
  108. return null;
  109. }, this);
  110. if(shadowCircles.length){
  111. run.dyn.shadow = shadowCircles[shadowCircles.length - 1].getStroke();
  112. }
  113. }
  114. // make outlines if needed
  115. if(theme.series.outline){
  116. outlineCircles = arr.map(points, function(item){
  117. if(item !== null){
  118. var finalTheme = t.addMixin(theme, "circle", item, true),
  119. outline = dc.makeStroke(finalTheme.series.outline);
  120. outline.width = 2 * outline.width + theme.series.stroke.width;
  121. var shape = s.createCircle({
  122. cx: item.x, cy: item.y, r: item.radius
  123. }).setStroke(outline);
  124. if(this.animate){
  125. this._animateBubble(shape, dim.height - offsets.b, item.radius);
  126. }
  127. return shape;
  128. }
  129. return null;
  130. }, this);
  131. if(outlineCircles.length){
  132. run.dyn.outline = outlineCircles[outlineCircles.length - 1].getStroke();
  133. }
  134. }
  135. // run through the data and add the circles.
  136. frontCircles = arr.map(points, function(item){
  137. if(item !== null){
  138. var finalTheme = t.addMixin(theme, "circle", item, true),
  139. rect = {
  140. x: item.x - item.radius,
  141. y: item.y - item.radius,
  142. width: 2 * item.radius,
  143. height: 2 * item.radius
  144. };
  145. var specialFill = this._plotFill(finalTheme.series.fill, dim, offsets);
  146. specialFill = this._shapeFill(specialFill, rect);
  147. var shape = s.createCircle({
  148. cx: item.x, cy: item.y, r: item.radius
  149. }).setFill(specialFill).setStroke(finalTheme.series.stroke);
  150. if(this.animate){
  151. this._animateBubble(shape, dim.height - offsets.b, item.radius);
  152. }
  153. return shape;
  154. }
  155. return null;
  156. }, this);
  157. if(frontCircles.length){
  158. run.dyn.fill = frontCircles[frontCircles.length - 1].getFill();
  159. run.dyn.stroke = frontCircles[frontCircles.length - 1].getStroke();
  160. }
  161. if(events){
  162. var eventSeries = new Array(frontCircles.length);
  163. arr.forEach(frontCircles, function(s, i){
  164. if(s !== null){
  165. var o = {
  166. element: "circle",
  167. index: i,
  168. run: run,
  169. shape: s,
  170. outline: outlineCircles && outlineCircles[i] || null,
  171. shadow: shadowCircles && shadowCircles[i] || null,
  172. x: run.data[i].x,
  173. y: run.data[i].y,
  174. r: run.data[i].size / 2,
  175. cx: points[i].x,
  176. cy: points[i].y,
  177. cr: points[i].radius
  178. };
  179. this._connectEvents(o);
  180. eventSeries[i] = o;
  181. }
  182. }, this);
  183. this._eventSeries[run.name] = eventSeries;
  184. }else{
  185. delete this._eventSeries[run.name];
  186. }
  187. run.dirty = false;
  188. }
  189. this.dirty = false;
  190. return this; // dojox.charting.plot2d.Bubble
  191. },
  192. _animateBubble: function(shape, offset, size){
  193. fx.animateTransform(lang.delegate({
  194. shape: shape,
  195. duration: 1200,
  196. transform: [
  197. {name: "translate", start: [0, offset], end: [0, 0]},
  198. {name: "scale", start: [0, 1/size], end: [1, 1]},
  199. {name: "original"}
  200. ]
  201. }, this.animate)).play();
  202. }
  203. });
  204. });