Scatter.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. define("dojox/charting/plot2d/Scatter", ["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "./Base", "./common",
  2. "dojox/lang/functional", "dojox/lang/functional/reversed", "dojox/lang/utils", "dojox/gfx/fx", "dojox/gfx/gradutils"],
  3. function(lang, arr, declare, Base, dc, df, dfr, du, fx, gradutils){
  4. /*=====
  5. var Base = dojox.charting.plot2d.Base;
  6. =====*/
  7. var purgeGroup = dfr.lambda("item.purgeGroup()");
  8. return declare("dojox.charting.plot2d.Scatter", Base, {
  9. // summary:
  10. // A plot object representing a typical scatter chart.
  11. defaultParams: {
  12. hAxis: "x", // use a horizontal axis named "x"
  13. vAxis: "y", // use a vertical axis named "y"
  14. shadows: null, // draw shadows
  15. animate: null // animate chart to place
  16. },
  17. optionalParams: {
  18. // theme component
  19. markerStroke: {},
  20. markerOutline: {},
  21. markerShadow: {},
  22. markerFill: {},
  23. markerFont: "",
  24. markerFontColor: ""
  25. },
  26. constructor: function(chart, kwArgs){
  27. // summary:
  28. // Create the scatter plot.
  29. // chart: dojox.charting.Chart
  30. // The chart this plot belongs to.
  31. // kwArgs: dojox.charting.plot2d.__DefaultCtorArgs?
  32. // An optional keyword arguments object to help define this plot's parameters.
  33. this.opt = lang.clone(this.defaultParams);
  34. du.updateWithObject(this.opt, kwArgs);
  35. du.updateWithPattern(this.opt, kwArgs, this.optionalParams);
  36. this.series = [];
  37. this.hAxis = this.opt.hAxis;
  38. this.vAxis = this.opt.vAxis;
  39. this.animate = this.opt.animate;
  40. },
  41. render: function(dim, offsets){
  42. // summary:
  43. // Run the calculations for any axes for this plot.
  44. // dim: Object
  45. // An object in the form of { width, height }
  46. // offsets: Object
  47. // An object of the form { l, r, t, b}.
  48. // returns: dojox.charting.plot2d.Scatter
  49. // A reference to this plot for functional chaining.
  50. if(this.zoom && !this.isDataDirty()){
  51. return this.performZoom(dim, offsets);
  52. }
  53. this.resetEvents();
  54. this.dirty = this.isDirty();
  55. if(this.dirty){
  56. arr.forEach(this.series, purgeGroup);
  57. this._eventSeries = {};
  58. this.cleanGroup();
  59. var s = this.group;
  60. df.forEachRev(this.series, function(item){ item.cleanGroup(s); });
  61. }
  62. var t = this.chart.theme, events = this.events();
  63. for(var i = this.series.length - 1; i >= 0; --i){
  64. var run = this.series[i];
  65. if(!this.dirty && !run.dirty){
  66. t.skip();
  67. this._reconnectEvents(run.name);
  68. continue;
  69. }
  70. run.cleanGroup();
  71. if(!run.data.length){
  72. run.dirty = false;
  73. t.skip();
  74. continue;
  75. }
  76. var theme = t.next("marker", [this.opt, run]), s = run.group, lpoly,
  77. ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler),
  78. vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler);
  79. if(typeof run.data[0] == "number"){
  80. lpoly = arr.map(run.data, function(v, i){
  81. return {
  82. x: ht(i + 1) + offsets.l,
  83. y: dim.height - offsets.b - vt(v)
  84. };
  85. }, this);
  86. }else{
  87. lpoly = arr.map(run.data, function(v, i){
  88. return {
  89. x: ht(v.x) + offsets.l,
  90. y: dim.height - offsets.b - vt(v.y)
  91. };
  92. }, this);
  93. }
  94. var shadowMarkers = new Array(lpoly.length),
  95. frontMarkers = new Array(lpoly.length),
  96. outlineMarkers = new Array(lpoly.length);
  97. arr.forEach(lpoly, function(c, i){
  98. var finalTheme = typeof run.data[i] == "number" ?
  99. t.post(theme, "marker") :
  100. t.addMixin(theme, "marker", run.data[i], true),
  101. path = "M" + c.x + " " + c.y + " " + finalTheme.symbol;
  102. if(finalTheme.marker.shadow){
  103. shadowMarkers[i] = s.createPath("M" + (c.x + finalTheme.marker.shadow.dx) + " " +
  104. (c.y + finalTheme.marker.shadow.dy) + " " + finalTheme.symbol).
  105. setStroke(finalTheme.marker.shadow).setFill(finalTheme.marker.shadow.color);
  106. if(this.animate){
  107. this._animateScatter(shadowMarkers[i], dim.height - offsets.b);
  108. }
  109. }
  110. if(finalTheme.marker.outline){
  111. var outline = dc.makeStroke(finalTheme.marker.outline);
  112. outline.width = 2 * outline.width + finalTheme.marker.stroke.width;
  113. outlineMarkers[i] = s.createPath(path).setStroke(outline);
  114. if(this.animate){
  115. this._animateScatter(outlineMarkers[i], dim.height - offsets.b);
  116. }
  117. }
  118. var stroke = dc.makeStroke(finalTheme.marker.stroke),
  119. fill = this._plotFill(finalTheme.marker.fill, dim, offsets);
  120. if(fill && (fill.type === "linear" || fill.type == "radial")){
  121. var color = gradutils.getColor(fill, {x: c.x, y: c.y});
  122. if(stroke){
  123. stroke.color = color;
  124. }
  125. frontMarkers[i] = s.createPath(path).setStroke(stroke).setFill(color);
  126. }else{
  127. frontMarkers[i] = s.createPath(path).setStroke(stroke).setFill(fill);
  128. }
  129. if(this.animate){
  130. this._animateScatter(frontMarkers[i], dim.height - offsets.b);
  131. }
  132. }, this);
  133. if(frontMarkers.length){
  134. run.dyn.stroke = frontMarkers[frontMarkers.length - 1].getStroke();
  135. run.dyn.fill = frontMarkers[frontMarkers.length - 1].getFill();
  136. }
  137. if(events){
  138. var eventSeries = new Array(frontMarkers.length);
  139. arr.forEach(frontMarkers, function(s, i){
  140. var o = {
  141. element: "marker",
  142. index: i,
  143. run: run,
  144. shape: s,
  145. outline: outlineMarkers && outlineMarkers[i] || null,
  146. shadow: shadowMarkers && shadowMarkers[i] || null,
  147. cx: lpoly[i].x,
  148. cy: lpoly[i].y
  149. };
  150. if(typeof run.data[0] == "number"){
  151. o.x = i + 1;
  152. o.y = run.data[i];
  153. }else{
  154. o.x = run.data[i].x;
  155. o.y = run.data[i].y;
  156. }
  157. this._connectEvents(o);
  158. eventSeries[i] = o;
  159. }, this);
  160. this._eventSeries[run.name] = eventSeries;
  161. }else{
  162. delete this._eventSeries[run.name];
  163. }
  164. run.dirty = false;
  165. }
  166. this.dirty = false;
  167. return this; // dojox.charting.plot2d.Scatter
  168. },
  169. _animateScatter: function(shape, offset){
  170. fx.animateTransform(lang.delegate({
  171. shape: shape,
  172. duration: 1200,
  173. transform: [
  174. {name: "translate", start: [0, offset], end: [0, 0]},
  175. {name: "scale", start: [0, 0], end: [1, 1]},
  176. {name: "original"}
  177. ]
  178. }, this.animate)).play();
  179. }
  180. });
  181. });