Base.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.Base"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.charting.plot2d.Base"] = true;
  8. dojo.provide("dojox.charting.plot2d.Base");
  9. dojo.require("dojox.charting.scaler.primitive");
  10. dojo.require("dojox.charting.Element");
  11. dojo.require("dojox.charting.plot2d.common");
  12. dojo.require("dojox.charting.plot2d._PlotEvents");
  13. /*=====
  14. dojox.charting.plot2d.__PlotCtorArgs = function(){
  15. // summary:
  16. // The base keyword arguments object for plot constructors.
  17. // Note that the parameters for this may change based on the
  18. // specific plot type (see the corresponding plot type for
  19. // details).
  20. }
  21. =====*/
  22. dojo.declare("dojox.charting.plot2d.Base", [dojox.charting.Element, dojox.charting.plot2d._PlotEvents], {
  23. constructor: function(chart, kwArgs){
  24. // summary:
  25. // Create a base plot for charting.
  26. // chart: dojox.chart.Chart2D
  27. // The chart this plot belongs to.
  28. // kwArgs: dojox.charting.plot2d.__PlotCtorArgs?
  29. // An optional arguments object to help define the plot.
  30. this.zoom = null,
  31. this.zoomQueue = []; // zooming action task queue
  32. this.lastWindow = {vscale: 1, hscale: 1, xoffset: 0, yoffset: 0};
  33. },
  34. clear: function(){
  35. // summary:
  36. // Clear out all of the information tied to this plot.
  37. // returns: dojox.charting.plot2d.Base
  38. // A reference to this plot for functional chaining.
  39. this.series = [];
  40. this._hAxis = null;
  41. this._vAxis = null;
  42. this.dirty = true;
  43. return this; // dojox.charting.plot2d.Base
  44. },
  45. setAxis: function(axis){
  46. // summary:
  47. // Set an axis for this plot.
  48. // axis: dojox.charting.axis2d.Base
  49. // The axis to set.
  50. // returns: dojox.charting.plot2d.Base
  51. // A reference to this plot for functional chaining.
  52. if(axis){
  53. this[axis.vertical ? "_vAxis" : "_hAxis"] = axis;
  54. }
  55. return this; // dojox.charting.plot2d.Base
  56. },
  57. addSeries: function(run){
  58. // summary:
  59. // Add a data series to this plot.
  60. // run: dojox.charting.Series
  61. // The series to be added.
  62. // returns: dojox.charting.plot2d.Base
  63. // A reference to this plot for functional chaining.
  64. this.series.push(run);
  65. return this; // dojox.charting.plot2d.Base
  66. },
  67. getSeriesStats: function(){
  68. // summary:
  69. // Calculate the min/max on all attached series in both directions.
  70. // returns: Object
  71. // {hmin, hmax, vmin, vmax} min/max in both directions.
  72. return dojox.charting.plot2d.common.collectSimpleStats(this.series);
  73. },
  74. calculateAxes: function(dim){
  75. // summary:
  76. // Stub function for running the axis calculations (depricated).
  77. // dim: Object
  78. // An object of the form { width, height }
  79. // returns: dojox.charting.plot2d.Base
  80. // A reference to this plot for functional chaining.
  81. this.initializeScalers(dim, this.getSeriesStats());
  82. return this; // dojox.charting.plot2d.Base
  83. },
  84. isDirty: function(){
  85. // summary:
  86. // Returns whether or not this plot needs to be rendered.
  87. // returns: Boolean
  88. // The state of the plot.
  89. return this.dirty || this._hAxis && this._hAxis.dirty || this._vAxis && this._vAxis.dirty; // Boolean
  90. },
  91. isDataDirty: function(){
  92. // summary:
  93. // Returns whether or not any of this plot's data series need to be rendered.
  94. // returns: Boolean
  95. // Flag indicating if any of this plot's series are invalid and need rendering.
  96. return dojo.some(this.series, function(item){ return item.dirty; }); // Boolean
  97. },
  98. performZoom: function(dim, offsets){
  99. // summary:
  100. // Create/alter any zooming windows on this plot.
  101. // dim: Object
  102. // An object of the form { width, height }.
  103. // offsets: Object
  104. // An object of the form { l, r, t, b }.
  105. // returns: dojox.charting.plot2d.Base
  106. // A reference to this plot for functional chaining.
  107. // get current zooming various
  108. var vs = this._vAxis.scale || 1,
  109. hs = this._hAxis.scale || 1,
  110. vOffset = dim.height - offsets.b,
  111. hBounds = this._hScaler.bounds,
  112. xOffset = (hBounds.from - hBounds.lower) * hBounds.scale,
  113. vBounds = this._vScaler.bounds,
  114. yOffset = (vBounds.from - vBounds.lower) * vBounds.scale;
  115. // get incremental zooming various
  116. rVScale = vs / this.lastWindow.vscale,
  117. rHScale = hs / this.lastWindow.hscale,
  118. rXOffset = (this.lastWindow.xoffset - xOffset)/
  119. ((this.lastWindow.hscale == 1)? hs : this.lastWindow.hscale),
  120. rYOffset = (yOffset - this.lastWindow.yoffset)/
  121. ((this.lastWindow.vscale == 1)? vs : this.lastWindow.vscale),
  122. shape = this.group,
  123. anim = dojox.gfx.fx.animateTransform(dojo.delegate({
  124. shape: shape,
  125. duration: 1200,
  126. transform:[
  127. {name:"translate", start:[0, 0], end: [offsets.l * (1 - rHScale), vOffset * (1 - rVScale)]},
  128. {name:"scale", start:[1, 1], end: [rHScale, rVScale]},
  129. {name:"original"},
  130. {name:"translate", start: [0, 0], end: [rXOffset, rYOffset]}
  131. ]}, this.zoom));
  132. dojo.mixin(this.lastWindow, {vscale: vs, hscale: hs, xoffset: xOffset, yoffset: yOffset});
  133. //add anim to zooming action queue,
  134. //in order to avoid several zooming action happened at the same time
  135. this.zoomQueue.push(anim);
  136. //perform each anim one by one in zoomQueue
  137. dojo.connect(anim, "onEnd", this, function(){
  138. this.zoom = null;
  139. this.zoomQueue.shift();
  140. if(this.zoomQueue.length > 0){
  141. this.zoomQueue[0].play();
  142. }
  143. });
  144. if(this.zoomQueue.length == 1){
  145. this.zoomQueue[0].play();
  146. }
  147. return this; // dojox.charting.plot2d.Base
  148. },
  149. render: function(dim, offsets){
  150. // summary:
  151. // Render the plot on the chart.
  152. // dim: Object
  153. // An object of the form { width, height }.
  154. // offsets: Object
  155. // An object of the form { l, r, t, b }.
  156. // returns: dojox.charting.plot2d.Base
  157. // A reference to this plot for functional chaining.
  158. return this; // dojox.charting.plot2d.Base
  159. },
  160. getRequiredColors: function(){
  161. // summary:
  162. // Get how many data series we have, so we know how many colors to use.
  163. // returns: Number
  164. // The number of colors needed.
  165. return this.series.length; // Number
  166. },
  167. initializeScalers: function(dim, stats){
  168. // summary:
  169. // Initializes scalers using attached axes.
  170. // dim: Object:
  171. // Size of a plot area in pixels as {width, height}.
  172. // stats: Object:
  173. // Min/max of data in both directions as {hmin, hmax, vmin, vmax}.
  174. // returns: dojox.charting.plot2d.Base
  175. // A reference to this plot for functional chaining.
  176. if(this._hAxis){
  177. if(!this._hAxis.initialized()){
  178. this._hAxis.calculate(stats.hmin, stats.hmax, dim.width);
  179. }
  180. this._hScaler = this._hAxis.getScaler();
  181. }else{
  182. this._hScaler = dojox.charting.scaler.primitive.buildScaler(stats.hmin, stats.hmax, dim.width);
  183. }
  184. if(this._vAxis){
  185. if(!this._vAxis.initialized()){
  186. this._vAxis.calculate(stats.vmin, stats.vmax, dim.height);
  187. }
  188. this._vScaler = this._vAxis.getScaler();
  189. }else{
  190. this._vScaler = dojox.charting.scaler.primitive.buildScaler(stats.vmin, stats.vmax, dim.height);
  191. }
  192. return this; // dojox.charting.plot2d.Base
  193. }
  194. });
  195. }