Bubble.js 7.1 KB

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