/src/com/globalworks/lbs/tools/charts/Base.as

https://github.com/fgontier/LBS_earnings_potential_calculator · ActionScript · 411 lines · 204 code · 96 blank · 111 comment · 48 complexity · eb21a8f1404f96c31ddf3113972785d6 MD5 · raw file

  1. package charts {
  2. import flash.display.Sprite;
  3. import charts.series.Element;
  4. import flash.geom.Point;
  5. import elements.axis.XAxisLabels;
  6. public class Base extends Sprite {
  7. // accessed by the Keys object to display the key
  8. protected var key:String;
  9. protected var font_size:Number;
  10. public var colour:Number;
  11. public var line_width:Number;
  12. public var circle_size:Number;
  13. //
  14. // hold the Element values, for lines this is an
  15. // array of string Y values, for Candle it is an
  16. // array of string 'high,open,low,close' values,
  17. // for scatter it is 'x,y' etc...
  18. //
  19. public var values:Array;
  20. protected var axis:Number;
  21. public function Base()
  22. {}
  23. public function get_colour(): Number {
  24. return this.colour;
  25. }
  26. //
  27. // return an array of key info objects:
  28. //
  29. public function get_keys(): Object {
  30. var tmp:Array = [];
  31. // some lines may not have a key
  32. if( (this.font_size > 0) && (this.key != '' ) )
  33. tmp.push( { 'text':this.key, 'font-size':this.font_size, 'colour':this.get_colour() } );
  34. return tmp;
  35. }
  36. //
  37. // whatever sets of data that *may* be attached to the right
  38. // Y Axis call this to see if they are attached to it or not.
  39. // All lines, area and bar charts call this.
  40. //
  41. protected function which_axis_am_i_attached_to( data:Array, i:Number ): Number {
  42. //
  43. // some data sets are attached to the right
  44. // Y axis (and min max), in the future we
  45. // may support many axis
  46. //
  47. if( data['show_y2'] != undefined )
  48. if( data['show_y2'] != 'false' )
  49. if( data['y2_lines'] != undefined )
  50. {
  51. var tmp:Array = data.y2_lines.split(",");
  52. var pos:Number = tmp.indexOf( i.toString() );
  53. if ( pos == -1 )
  54. return 1;
  55. else
  56. return 2; // <-- this line found in y2_lines, so it is attached to axis 2 (right axis)
  57. }
  58. return 1;
  59. }
  60. /**
  61. * may be called by main.as to make the X Axis labels
  62. * @return
  63. */
  64. public function get_max_x():Number {
  65. var max:Number = Number.MIN_VALUE;
  66. //
  67. // count the non-mask items:
  68. //
  69. for ( var i:Number = 0; i < this.numChildren; i++ ) {
  70. if ( this.getChildAt(i) is Element ) {
  71. var e:Element = this.getChildAt(i) as Element
  72. max = Math.max( max, e.get_x() );
  73. }
  74. }
  75. return max;
  76. }
  77. public function get_min_x():Number {
  78. var min:Number = Number.MAX_VALUE;
  79. //
  80. // count the non-mask items:
  81. //
  82. for ( var i:Number = 0; i < this.numChildren; i++ ) {
  83. if ( this.getChildAt(i) is Element ) {
  84. var e:Element = this.getChildAt(i) as Element
  85. min = Math.min( min, e.get_x() );
  86. }
  87. }
  88. return min;
  89. }
  90. //
  91. // this should be overriden
  92. //
  93. public function resize( sc:ScreenCoordsBase ):void{}
  94. //public function draw( val:String, mc:Object ):void {}
  95. //
  96. // TODO: old remove when tooltips tested
  97. //
  98. public function closest( x:Number, y:Number ): Object {
  99. var shortest:Number = Number.MAX_VALUE;
  100. var closest:Element = null;
  101. var dx:Number;
  102. for ( var i:Number = 0; i < this.numChildren; i++ ) {
  103. //
  104. // some of the children will will mask
  105. // Sprites, so filter those out:
  106. //
  107. if( this.getChildAt(i) is Element ) {
  108. var e:Element = this.getChildAt(i) as Element;
  109. e.set_tip( false );
  110. dx = Math.abs( x -e.x );
  111. if( dx < shortest ) {
  112. shortest = dx;
  113. closest = e;
  114. }
  115. }
  116. }
  117. var dy:Number = 0;
  118. if( closest )
  119. dy = Math.abs( y - closest.y );
  120. return { element:closest, distance_x:shortest, distance_y:dy };
  121. }
  122. //
  123. // Line and bar charts will normally only have one
  124. // Element at any X position, but when using Radar axis
  125. // you may get many at any give X location.
  126. //
  127. // Scatter charts can have many items at the same X position
  128. //
  129. public function closest_2( x:Number, y:Number ): Array {
  130. // get the closest Elements X value
  131. var x:Number = closest_x(x);
  132. var tmp:Array = this.get_all_at_this_x_pos(x);
  133. // tr.aces('tmp.length', tmp.length);
  134. var closest:Array = this.get_closest_y(tmp, y);
  135. var dy:Number = Math.abs( y - closest.y );
  136. // tr.aces('closest.length', closest.length);
  137. return closest;
  138. }
  139. //
  140. // get the X value of the closest points to the mouse
  141. //
  142. private function closest_x( x:Number ):Number {
  143. var closest:Number = Number.MAX_VALUE;
  144. var p:flash.geom.Point;
  145. var x_pos:Number;
  146. var dx:Number;
  147. for ( var i:Number = 0; i < this.numChildren; i++ ) {
  148. //
  149. // some of the children will will mask
  150. // Sprites, so filter those out:
  151. //
  152. if( this.getChildAt(i) is Element ) {
  153. var e:Element = this.getChildAt(i) as Element;
  154. p = e.get_mid_point();
  155. dx = Math.abs( x - p.x );
  156. if( dx < closest ) {
  157. closest = dx;
  158. x_pos = p.x;
  159. }
  160. }
  161. }
  162. return x_pos;
  163. }
  164. //
  165. // get all the Elements at this X position
  166. // BarStack overrides this
  167. //
  168. protected function get_all_at_this_x_pos( x:Number ):Array {
  169. var tmp:Array = new Array();
  170. var p:flash.geom.Point;
  171. var e:Element;
  172. for ( var i:Number = 0; i < this.numChildren; i++ ) {
  173. // some of the children will will mask
  174. // Sprites, so filter those out:
  175. //
  176. if( this.getChildAt(i) is Element ) {
  177. e = this.getChildAt(i) as Element;
  178. //
  179. // Point elements are invisible by default.
  180. //
  181. // Prevent invisible points from showing tooltips
  182. // For scatter line area
  183. //if (e.visible)
  184. //{
  185. p = e.get_mid_point();
  186. if ( p.x == x )
  187. tmp.push( e );
  188. //}
  189. }
  190. }
  191. return tmp;
  192. }
  193. //
  194. // scatter charts may have many Elements in the same
  195. // x, y location
  196. //
  197. private function get_closest_y( elements:Array, y:Number):Array {
  198. var y_min:Number = Number.MAX_VALUE;
  199. var dy:Number;
  200. var closest:Array = new Array();
  201. var p:flash.geom.Point;
  202. var e:Element;
  203. // get min Y distance
  204. for each( e in elements ) {
  205. p = e.get_mid_point();
  206. dy = Math.abs( y - p.y );
  207. y_min = Math.min( dy, y_min );
  208. }
  209. // select all Elements at this Y pos
  210. for each( e in elements ) {
  211. p = e.get_mid_point();
  212. dy = Math.abs( y - p.y );
  213. if( dy == y_min )
  214. closest.push(e);
  215. }
  216. return closest;
  217. }
  218. //
  219. // scatter charts may have many Elements in the same
  220. // x, y location
  221. //
  222. public function mouse_proximity( x:Number, y:Number ): Array {
  223. var closest:Number = Number.MAX_VALUE;
  224. var p:flash.geom.Point;
  225. var i:Number;
  226. var e:Element;
  227. var mouse:flash.geom.Point = new flash.geom.Point(x, y);
  228. //
  229. // find the closest Elements
  230. //
  231. for ( i=0; i < this.numChildren; i++ ) {
  232. // filter mask Sprites
  233. if( this.getChildAt(i) is Element ) {
  234. e = this.getChildAt(i) as Element;
  235. closest = Math.min( flash.geom.Point.distance(e.get_mid_point(), mouse), closest );
  236. }
  237. }
  238. //
  239. // grab all Elements at this distance
  240. //
  241. var close:Array = [];
  242. for ( i=0; i < this.numChildren; i++ ) {
  243. // filter mask Sprites
  244. if( this.getChildAt(i) is Element ) {
  245. e = this.getChildAt(i) as Element;
  246. if ( flash.geom.Point.distance(e.get_mid_point(), mouse) == closest )
  247. close.push(e);
  248. }
  249. }
  250. return close;
  251. }
  252. //
  253. // this is a backup function so if the mouse leaves the
  254. // movie for some reason without raising the mouse
  255. // out event (this happens if the user is wizzing the mouse about)
  256. //
  257. public function mouse_out():void {
  258. for ( var i:Number = 0; i < this.numChildren; i++ ) {
  259. // filter out the mask elements in line charts
  260. if( this.getChildAt(i) is Element ) {
  261. var e:Element = this.getChildAt(i) as Element;
  262. e.set_tip(false);
  263. }
  264. }
  265. }
  266. //
  267. // index of item (bar, point, pie slice, horizontal bar) may be used
  268. // to look up its X value (bar,point) or Y value (H Bar) or used as
  269. // the sequence number (Pie)
  270. //
  271. protected function get_element( index:Number, value:Object ): Element {
  272. return null;
  273. }
  274. public function add_values():void {
  275. // keep track of the X position (column)
  276. var index:Number = 0;
  277. for each ( var val:Object in this.values )
  278. {
  279. var tmp:Element;
  280. // filter out the 'null' values
  281. if( val != null )
  282. {
  283. tmp = this.get_element( index, val );
  284. if( tmp.line_mask != null )
  285. this.addChild( tmp.line_mask );
  286. this.addChild( tmp );
  287. }
  288. index++;
  289. }
  290. }
  291. /**
  292. * See ObjectCollection tooltip_replace_labels
  293. *
  294. * @param labels
  295. */
  296. public function tooltip_replace_labels( labels:XAxisLabels ):void {
  297. for ( var i:Number = 0; i < this.numChildren; i++ ) {
  298. // filter out the mask elements in line charts
  299. if( this.getChildAt(i) is Element ) {
  300. var e:Element = this.getChildAt(i) as Element;
  301. e.tooltip_replace_labels( labels );
  302. }
  303. }
  304. }
  305. public function die():void {
  306. for ( var i:Number = 0; i < this.numChildren; i++ )
  307. if ( this.getChildAt(i) is Element ) {
  308. var e:Element = this.getChildAt(i) as Element;
  309. e.die();
  310. }
  311. while ( this.numChildren > 0 )
  312. this.removeChildAt(0);
  313. }
  314. }
  315. }