PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/ofc/elements/axis/YAxisBase.as

http://ofcgwt.googlecode.com/
ActionScript | 314 lines | 180 code | 55 blank | 79 comment | 29 complexity | 7c7ec9c79d56cb4699efe74537981cb5 MD5 | raw file
  1. package elements.axis {
  2. import flash.display.Sprite;
  3. import string.Utils;
  4. public class YAxisBase extends Sprite {
  5. protected var stroke:Number;
  6. protected var tick_length:Number;
  7. protected var colour:Number;
  8. protected var steps:Number;
  9. protected var grid_colour:Number;
  10. public var style:Object;
  11. protected var labels:YAxisLabelsBase;
  12. private var user_labels:Array;
  13. private var user_ticks:Boolean;
  14. private var logarithmicScale:Boolean = false;
  15. function YAxisBase(){}
  16. public function init(json:Object): void {}
  17. // called once the sprite has been added to the stage
  18. // so now it has access to the stage
  19. protected function _init(json:Object, name:String, style:Object): void {
  20. this.style = style;
  21. if( json[name] )
  22. object_helper.merge_2( json[name], this.style );
  23. this.colour = Utils.get_colour( style.colour );
  24. this.grid_colour = Utils.get_colour( style['grid-colour'] );
  25. this.stroke = style.stroke;
  26. this.tick_length = style['tick-length'];
  27. this.logarithmicScale = (this.style['log-scale'] == true);
  28. // make sure we don't have 1,000,000 steps
  29. var min:Number = Math.min(this.style.min, this.style.max);
  30. var max:Number = Math.max(this.style.min, this.style.max);
  31. // tr.aces(min, max, this.stage.stageHeight, this.style.steps);
  32. this.style.steps = this.get_steps(min, max, this.stage.stageHeight);
  33. // tr.aces(min, max, this.stage.stageHeight, this.style.steps);
  34. //
  35. // colour the grid lines
  36. //
  37. // TODO: remove this and
  38. // this.user_ticks
  39. // this.user_labels
  40. //
  41. if ((this.style.labels != null) &&
  42. (this.style.labels.labels != null) &&
  43. (this.style.labels.labels is Array) &&
  44. (this.style.labels.labels.length > 0))
  45. {
  46. this.user_labels = new Array();
  47. for each( var lbl:Object in this.style.labels.labels )
  48. {
  49. if (!(lbl is String)) {
  50. if (lbl.y != null)
  51. {
  52. var tmpObj:Object = { y: lbl.y };
  53. if (lbl["grid-colour"])
  54. {
  55. tmpObj["grid-colour"] = Utils.get_colour(lbl["grid-colour"]);
  56. }
  57. else
  58. {
  59. tmpObj["grid-colour"] = this.grid_colour;
  60. }
  61. this.user_ticks = true;
  62. this.user_labels.push(tmpObj);
  63. }
  64. }
  65. }
  66. }
  67. }
  68. public function auto_range(max:Number): Number {
  69. var maxValue:Number = Math.max(max) * 1.07;
  70. var l:Number = Math.round(Math.log(maxValue)/Math.log(10));
  71. var p:Number = Math.pow(10, l) / 2;
  72. maxValue = Math.round(maxValue * 1.1 / p) * p;
  73. return maxValue;
  74. /*
  75. var maxValue:Number = Math.max($bar_1->data) * 1.07;
  76. $l = round(log($maxValue)/log(10));
  77. $p = pow(10, $l) / 2;
  78. $maxValue = round($maxValue * 1.1 / $p) * $p;
  79. */
  80. /*
  81. * http://forums.openflashchart.com/viewtopic.php?f=5&t=617&start=0
  82. * cdcarson
  83. // y axis data...
  84. $counts = array_values($data);
  85. $ymax = max($counts);
  86. // add a bit of padding to the top, not strictly necessary...
  87. $ymax += ceil(.1 * $ymax);
  88. //$max_steps could be anything,depending on the height of the chart, font-size, etc..
  89. $max_steps = 10;
  90. /**
  91. * The step sizes to test are created using an
  92. * array of multipliers and a power of 10, starting at 0.
  93. * $step_size = $multiplier * pow(10, $exponent);
  94. * Assuming $multipliers = array(1, 2, 5) this would give us...
  95. * 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000,...
  96. * /
  97. $n = 0;
  98. $multipliers = array(1, 2, 5);
  99. $num_multipliers = count($multipliers);
  100. $exponent = floor($n / $num_multipliers);
  101. $multiplier = $multipliers[$n % $num_multipliers];
  102. $step_size = $multiplier * pow(10, $exponent);
  103. $num_steps = ceil($ymax/$step_size);
  104. //keep testing until we have the right step_size...
  105. while ($num_steps >= $max_steps){
  106. $n ++;
  107. $exponent = floor($n / $num_multipliers);
  108. $multiplier = $multipliers[$n % $num_multipliers];
  109. $step_size = $multiplier * pow(10, $exponent);
  110. $num_steps = ceil($ymax/$step_size);
  111. }
  112. $yaxis = new y_axis();
  113. $yaxis->set_range(0, $ymax, $step_size);
  114. */
  115. }
  116. public function get_style():Object { return null; }
  117. //
  118. // may be called by the labels
  119. //
  120. public function set_y_max( m:Number ):void {
  121. this.style.max = m;
  122. }
  123. public function get_range():Range {
  124. return new Range( this.style.min, this.style.max, this.style.steps, this.style.offset );
  125. }
  126. public function get_width():Number {
  127. return this.stroke + this.tick_length + this.labels.width;
  128. }
  129. public function get_log_scale():Boolean {
  130. return this.logarithmicScale;
  131. }
  132. public function die(): void {
  133. //this.offset = null;
  134. this.style = null;
  135. if (this.labels != null) this.labels.die();
  136. this.labels = null;
  137. this.graphics.clear();
  138. while ( this.numChildren > 0 )
  139. this.removeChildAt(0);
  140. }
  141. private function get_steps(min:Number, max:Number, height:Number):Number {
  142. // try to avoid infinite loops...
  143. if ( this.style.steps == 0 )
  144. this.style.steps = 1;
  145. if ( this.style.steps < 0 )
  146. this.style.steps *= -1;
  147. // how many steps (grid lines) do we have?
  148. var s:Number = (max - min) / this.style.steps;
  149. if ( s > (height/2) ) {
  150. // either no steps are set, or they are wrong and
  151. // we have more grid lines than pixels to show them.
  152. // E.g:
  153. // max = 1,001,000
  154. // min = 1,000
  155. // s = 200,000
  156. s = (max - min) / 5;
  157. }
  158. else
  159. {
  160. s = this.style.steps;
  161. }
  162. return s;
  163. }
  164. public function resize(label_pos:Number, sc:ScreenCoords):void { }
  165. protected function resize_helper(label_pos:Number, sc:ScreenCoords, right:Boolean):void {
  166. // Set opacity for the first line to 0 (otherwise it overlaps the x-axel line)
  167. //
  168. // Bug? Does this work on graphs with minus values?
  169. //
  170. var i2:Number = 0;
  171. var i:Number;
  172. var y:Number;
  173. var lbl:Object;
  174. var min:Number = Math.min(this.style.min, this.style.max);
  175. var max:Number = Math.max(this.style.min, this.style.max);
  176. if( !right )
  177. this.labels.resize( label_pos, sc );
  178. else
  179. this.labels.resize( sc.right + this.stroke + this.tick_length, sc );
  180. if ( !this.style.visible )
  181. return;
  182. this.graphics.clear();
  183. this.graphics.lineStyle( 0, 0, 0 );
  184. if ( this.style['grid-visible'] )
  185. this.draw_grid_lines(this.style.steps, min, max, right, sc);
  186. var pos:Number;
  187. if (!right)
  188. pos = sc.left - this.stroke;
  189. else
  190. pos = sc.right;
  191. // Axis line:
  192. this.graphics.beginFill( this.colour, 1 );
  193. this.graphics.drawRect( pos, sc.top, this.stroke, sc.height );
  194. this.graphics.endFill();
  195. // ticks..
  196. var width:Number;
  197. if (this.user_ticks)
  198. {
  199. for each( lbl in this.user_labels )
  200. {
  201. y = sc.get_y_from_val(lbl.y, right);
  202. if ( !right )
  203. tick_pos = sc.left - this.stroke - this.tick_length;
  204. else
  205. tick_pos = sc.right + this.stroke;
  206. this.graphics.beginFill( this.colour, 1 );
  207. this.graphics.drawRect( tick_pos, y - (this.stroke / 2), this.tick_length, this.stroke );
  208. this.graphics.endFill();
  209. }
  210. }
  211. else
  212. {
  213. for(i=min; i<=max; i+=this.style.steps) {
  214. // start at the bottom and work up:
  215. y = sc.get_y_from_val(i, right);
  216. var tick_pos:Number;
  217. if ( !right )
  218. tick_pos = sc.left - this.stroke - this.tick_length;
  219. else
  220. tick_pos = sc.right + this.stroke;
  221. this.graphics.beginFill( this.colour, 1 );
  222. this.graphics.drawRect( tick_pos, y - (this.stroke / 2), this.tick_length, this.stroke );
  223. this.graphics.endFill();
  224. }
  225. }
  226. }
  227. private function draw_grid_lines(steps:Number, min:Number, max:Number, right:Boolean, sc:ScreenCoords): void {
  228. var y:Number;
  229. var lbl:Object;
  230. //
  231. // draw GRID lines
  232. //
  233. if (this.user_ticks)
  234. {
  235. for each(lbl in this.user_labels )
  236. {
  237. y = sc.get_y_from_val(lbl.y, right);
  238. this.graphics.beginFill(lbl["grid-colour"], 1);
  239. this.graphics.drawRect( sc.left, y, sc.width, 1 );
  240. this.graphics.endFill();
  241. }
  242. }
  243. else
  244. {
  245. //
  246. // hack: http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_13989&sliceId=1
  247. //
  248. max += 0.000004;
  249. for( var i:Number = min; i<=max; i+=steps ) {
  250. y = sc.get_y_from_val(i, right);
  251. this.graphics.beginFill( this.grid_colour, 1 );
  252. this.graphics.drawRect( sc.left, y, sc.width, 1 );
  253. this.graphics.endFill();
  254. }
  255. }
  256. }
  257. }
  258. }