ScreenCoords.as 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. package {
  2. import flash.geom.Point;
  3. import charts.series.dots.Point;
  4. public class ScreenCoords extends ScreenCoordsBase
  5. {
  6. private var x_range:Range;
  7. private var y_range:Range;
  8. private var y_right_range:Range;
  9. // position of the zero line
  10. //public var zero:Number=0;
  11. //public var steps:Number=0;
  12. // tick_offset is set by 3D axis
  13. public var tick_offset:Number;
  14. private var x_offset:Boolean;
  15. private var y_offset:Boolean;
  16. private var bar_groups:Number;
  17. public function ScreenCoords( top:Number, left:Number, right:Number, bottom:Number,
  18. y_axis_range:Range,
  19. y_axis_right_range:Range,
  20. x_axis_range:Range,
  21. x_left_label_width:Number, x_right_label_width:Number,
  22. three_d:Boolean )
  23. {
  24. super( top, left, right, bottom );
  25. var tmp_left:Number = left;
  26. this.x_range = x_axis_range;
  27. this.y_range = y_axis_range;
  28. this.y_right_range = y_axis_right_range;
  29. // tr.ace( '-----' );
  30. // tr.ace( this.x_range.count() );
  31. // tr.ace( this.y_range.count() );
  32. if( x_range ) {
  33. right = this.jiggle( left, right, x_right_label_width, x_axis_range.count() );
  34. tmp_left = this.shrink_left( left, right, x_left_label_width, x_axis_range.count() );
  35. }
  36. this.top = top;
  37. this.left = Math.max(left, tmp_left);
  38. // round this down to the nearest int:
  39. this.right = Math.floor( right );
  40. this.bottom = bottom;
  41. this.width = this.right-this.left;
  42. this.height = bottom-top;
  43. if( three_d )
  44. {
  45. // tell the box object that the
  46. // X axis labels need to be offset
  47. this.tick_offset = 12;
  48. }
  49. else
  50. this.tick_offset = 0;
  51. //
  52. // x_offset:
  53. //
  54. // False True
  55. //
  56. // | |
  57. // | |
  58. // | |
  59. // +--+--+--+ |-+--+--+--+-+
  60. // 0 1 2 3 0 1 2 3
  61. //
  62. // PIE charts don't have these:
  63. if( x_axis_range ) {
  64. this.x_offset = x_axis_range.offset;
  65. }
  66. if( y_axis_range ) {
  67. // tr.aces( 'YYYY', y_axis_range.offset );
  68. this.y_offset = y_axis_range.offset;
  69. }
  70. this.bar_groups = 1;
  71. }
  72. //
  73. // if the last X label is wider than the chart area, the last few letters will
  74. // be outside the drawing area. So we make the chart width smaller so the label
  75. // will fit into the screen.
  76. //
  77. //DZ: this implementation chops off the last label on scatter charts because it
  78. // assumes the label is centered on the last "item" (like a bar) instead of
  79. // at the max edge of the plot.
  80. public function jiggle_original( left:Number, right:Number, x_label_width:Number, count:Number ): Number {
  81. var r:Number = 0;
  82. if( x_label_width != 0 )
  83. {
  84. var item_width:Number = (right-left) / count;
  85. r = right - (item_width / 2);
  86. var new_right:Number = right;
  87. // while the right most X label is off the edge of the
  88. // Stage, move the box.right - 1
  89. while( r+(x_label_width/2) > right )
  90. {
  91. new_right -= 1;
  92. // changing the right also changes the item_width:
  93. item_width = (new_right-left) / count;
  94. r = new_right-(item_width/2);
  95. }
  96. right = new_right;
  97. }
  98. return right;
  99. }
  100. //DZ: this implementation probably add white space on the right side of a
  101. // non-scatter type plot because it assumes that the label is centered at
  102. // the max edge of the plot instead of centered on the last "item"
  103. // (like a bar)
  104. public function jiggle( left:Number, right:Number, x_label_width:Number, count:Number ): Number {
  105. return right - (x_label_width / 2);
  106. }
  107. //
  108. // if the left label is truncated, shrink the box until
  109. // it fits onto the screen
  110. //
  111. public function shrink_left( left:Number, right:Number, x_label_width:Number, count:Number ): Number {
  112. var pos:Number = 0;
  113. if( x_label_width != 0 )
  114. {
  115. var item_width:Number = (right-left) / count;
  116. pos = left+(item_width/2);
  117. var new_left:Number = left;
  118. // while the left most label is hanging off the Stage
  119. // move the box.left in one pixel:
  120. while( pos-(x_label_width/2) < 0 )
  121. {
  122. new_left += 1;
  123. // changing the left also changes the item_width:
  124. item_width = (right-new_left) / count;
  125. pos = new_left+(item_width/2);
  126. }
  127. left = new_left;
  128. }
  129. return left;
  130. }
  131. //
  132. // the bottom point of a bar:
  133. // min=-100 and max=100, use b.zero
  134. // min = 10 and max = 20, use b.bottom
  135. //
  136. public override function get_y_bottom( right_axis:Boolean = false ):Number
  137. {
  138. //
  139. // may have min=10, max=20, or
  140. // min = 20, max = -20 (upside down chart)
  141. //
  142. var r:Range = right_axis ? this.y_right_range : this.y_range;
  143. var min:Number = r.min;
  144. var max:Number = r.max;
  145. min = Math.min( min, max );
  146. return this.get_y_from_val( Math.max(0,min), right_axis );
  147. }
  148. // takes a value and returns the screen Y location
  149. public function getY_old( i:Number, right_axis:Boolean ):Number
  150. {
  151. var r:Range = right_axis ? this.y_right_range : this.y_range;
  152. var steps:Number = this.height / (r.count());// ( right_axis ));
  153. // find Y pos for value=zero
  154. var y:Number = this.bottom-(steps*(r.min*-1));
  155. // move up (-Y) to our point (don't forget that y_min will shift it down)
  156. y -= i*steps;
  157. return y;
  158. }
  159. //
  160. // takes a value and returns the screen Y location
  161. // what is the Y range?
  162. //
  163. // Horizontal bar charts are offset. Note:
  164. // step = 1
  165. // and step/2 is offset at the bottom and top
  166. // so we add 1*step so we can calculate:
  167. //
  168. // offset = true
  169. //
  170. // |
  171. // X -|==========
  172. // |
  173. // Y -|===
  174. // |
  175. // Z -|========
  176. // +--+--+--+--+--+--
  177. //
  178. // offset = false
  179. //
  180. // 2 -|
  181. // |
  182. // 1 -| 0--0--0--0--0
  183. // |
  184. // 0 -+--+--+--+--+--+--
  185. //
  186. public override function get_y_from_val( i:Number, right_axis:Boolean = false ):Number {
  187. var r:Range = right_axis ? this.y_right_range : this.y_range;
  188. var steps:Number = this.height / r.count();
  189. // tr.ace( 'off' );
  190. // tr.ace( this.y_offset.offset );
  191. // tr.ace( count );
  192. var tmp:Number = 0;
  193. if( this.y_offset )
  194. tmp = (steps / 2);
  195. // move up (-Y) to our point (don't forget that y_min will shift it down)
  196. return this.bottom-tmp-(r.min-i)*steps*-1;
  197. }
  198. public override function get_get_x_from_pos_and_y_from_val( index:Number, y:Number, right_axis:Boolean = false ):flash.geom.Point {
  199. return new flash.geom.Point(
  200. this.get_x_from_pos( index ),
  201. this.get_y_from_val( y, right_axis ) );
  202. }
  203. public function width_():Number
  204. {
  205. return this.right-this.left_();
  206. }
  207. private function left_():Number
  208. {
  209. var padding_left:Number = this.tick_offset;
  210. return this.left+padding_left;
  211. }
  212. //
  213. // Scatter and Horizontal Bar charts use this:
  214. //
  215. // get the x position by value
  216. // (e.g. what is the x position for -5 ?)
  217. //
  218. public override function get_x_from_val( i:Number ):Number {
  219. // Patch from DZ:
  220. var rev:Boolean = this.x_range.min > this.x_range.max;
  221. var count:Number = this.x_range.count();
  222. count += (rev && this.x_range.offset) ? -2 : 0;
  223. var item_width:Number = this.width_() / count;
  224. // end DZ
  225. var pos:Number = i-this.x_range.min;
  226. var tmp:Number = 0;
  227. if( this.x_offset )
  228. tmp = Math.abs(item_width/2);
  229. return this.left_()+tmp+(pos*item_width);
  230. }
  231. //
  232. // get the x location of the n'th item
  233. //
  234. public override function get_x_from_pos( i:Number ):Number {
  235. // DZ:
  236. // var item_width:Number = Math.abs(this.width_() / this.x_range.count());
  237. var rev:Boolean = this.x_range.min > this.x_range.max;
  238. var count:Number = this.x_range.count();
  239. count += (rev && this.x_range.offset) ? -2 : 0;
  240. var item_width:Number = Math.abs(this.width_() / count);
  241. var tmp:Number = 0;
  242. if( this.x_offset )
  243. tmp = (item_width/2);
  244. return this.left_()+tmp+(i*item_width);
  245. }
  246. //
  247. // get the position of the n'th X axis tick
  248. //
  249. public function get_x_tick_pos( i:Number ):Number
  250. {
  251. return this.get_x_from_pos(i) - this.tick_offset;
  252. }
  253. //
  254. // make a point object, using the absolute values (e.g. -5,-5 )
  255. /*
  256. public function make_point_2( x:Number, y:Number, right_axis:Boolean ):charts.Elements.Point
  257. {
  258. return new charts.Elements.Point(
  259. this.get_x_from_val( x ),
  260. this.get_y_from_val( y, right_axis )
  261. // whats this for?
  262. //,y
  263. );
  264. }*/
  265. public function set_bar_groups( n:Number ): void {
  266. this.bar_groups = n;
  267. }
  268. //
  269. // index: the n'th bar from the left
  270. //
  271. public function get_bar_coords( index:Number, group:Number ):Object {
  272. var item_width:Number = this.width_() / this.x_range.count();
  273. // the bar(s) have gaps between them:
  274. var bar_set_width:Number = item_width*0.8;
  275. // get the margin between sets of bars:
  276. var tmp:Number = 0;
  277. if( this.x_offset )
  278. tmp = item_width;
  279. // 1 bar == 100% wide, 2 bars = 50% wide each
  280. var bar_width:Number = bar_set_width / this.bar_groups;
  281. //bar_width -= 0.001; // <-- hack so bars don't quite touch
  282. var bar_left:Number = this.left_()+((tmp-bar_set_width)/2);
  283. var left:Number = bar_left+(index*item_width);
  284. left += bar_width * group;
  285. return { x:left, width:bar_width };
  286. }
  287. public function get_horiz_bar_coords( index:Number, group:Number ):Object {
  288. // split the height into equal heights for each bar
  289. var bar_width:Number = this.height / this.y_range.count();
  290. // the bar(s) have gaps between them:
  291. var bar_set_width:Number = bar_width*0.8;
  292. // 1 bar == 100% wide, 2 bars = 50% wide each
  293. var group_width:Number = bar_set_width / this.bar_groups;
  294. var bar_top:Number = this.top+((bar_width-bar_set_width)/2);
  295. var top:Number = bar_top+(index*bar_width);
  296. top += group_width * group;
  297. return { y:top, width:group_width };
  298. }
  299. public function makePointHLC( x:Number, high:Number, close:Number, low:Number, right_axis:Boolean, group:Number, group_count:Number )
  300. :PointHLC {
  301. var item_width:Number = this.width_() / this.x_range.count();
  302. // the bar(s) have gaps between them:
  303. var bar_set_width:Number = item_width*1;
  304. // get the margin between sets of bars:
  305. var bar_left:Number = this.left_()+((item_width-bar_set_width)/2);
  306. // 1 bar == 100% wide, 2 bars = 50% wide each
  307. var bar_width:Number = bar_set_width/group_count;
  308. var left:Number = bar_left+(x*item_width);
  309. left += bar_width*group;
  310. return new PointHLC(
  311. left,
  312. this.get_y_from_val( high, right_axis ),
  313. this.get_y_from_val( close, right_axis ),
  314. this.get_y_from_val( low, right_axis ),
  315. high,
  316. bar_width
  317. // ,close
  318. );
  319. }
  320. }
  321. }