YAxisLabelsBase.as 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package elements.axis {
  2. import flash.display.Sprite;
  3. import elements.axis.YTextField;
  4. import flash.text.TextFormat;
  5. import org.flashdevelop.utils.FlashConnect;
  6. import br.com.stimuli.string.printf;
  7. import string.Utils;
  8. public class YAxisLabelsBase extends Sprite {
  9. private var steps:Number;
  10. private var right:Boolean;
  11. protected var style:Object;
  12. public var i_need_labels:Boolean;
  13. protected var lblText:String;
  14. public var y_max:Number;
  15. public function YAxisLabelsBase(json:Object, axis_name:String) {
  16. var i:Number;
  17. var s:String;
  18. var values:Array;
  19. var steps:Number;
  20. // TODO: calculate Y max from the data
  21. this.y_max = 10;
  22. if( json[axis_name] )
  23. {
  24. //
  25. // Old crufty JSON, refactor out at some point,
  26. //
  27. //
  28. if( json[axis_name].labels is Array )
  29. {
  30. values = [];
  31. // use passed in min if provided else zero
  32. i = (json[axis_name] && json[axis_name].min) ? json[axis_name].min : 0;
  33. for each( s in json[axis_name].labels )
  34. {
  35. values.push( { val:s, pos:i } );
  36. i++;
  37. }
  38. //
  39. // alter the MinMax object:
  40. //
  41. // use passed in max if provided else the number of values less 1
  42. this.y_max = (json[axis_name] && json[axis_name].max) ? json[axis_name].max : values.length - 1;
  43. this.i_need_labels = false;
  44. }
  45. }
  46. //
  47. // an object, that contains an array of objects:
  48. //
  49. if( json[axis_name] )
  50. {
  51. if ( json[axis_name].labels is Object )
  52. {
  53. if ( json[axis_name].labels.text is String )
  54. this.lblText = json[axis_name].labels.text;
  55. var visibleSteps:Number = 1;
  56. if( json[axis_name].steps is Number )
  57. visibleSteps = json[axis_name].steps;
  58. if( json[axis_name].labels.steps is Number )
  59. visibleSteps = json[axis_name].labels.steps;
  60. if ( json[axis_name].labels.labels is Array )
  61. {
  62. values = [];
  63. // use passed in min if provided else zero
  64. var label_pos:Number = (json[axis_name] && json[axis_name].min) ? json[axis_name].min : 0;
  65. for each( var obj:Object in json[axis_name].labels.labels )
  66. {
  67. if(obj is Number)
  68. {
  69. values.push( { val:lblText, pos:obj } );
  70. //i = (obj > i) ? obj as Number : i;
  71. }
  72. else if(obj is String)
  73. {
  74. values.push( {
  75. val: obj,
  76. pos: label_pos,
  77. visible: ((label_pos % visibleSteps) == 0)
  78. } );
  79. //i = (obj > i) ? obj as Number : i;
  80. }
  81. else if (obj.y is Number)
  82. {
  83. s = (obj.text is String) ? obj.text : lblText;
  84. var style:Object = { val:s, pos:obj.y }
  85. if (obj.colour != null)
  86. style.colour = obj.colour;
  87. if (obj.size != null)
  88. style.size = obj.size;
  89. if (obj.rotate != null)
  90. style.rotate = obj.rotate;
  91. values.push( style );
  92. //i = (obj.y > i) ? obj.y : i;
  93. }
  94. label_pos++;
  95. }
  96. this.i_need_labels = false;
  97. }
  98. }
  99. }
  100. this.steps = steps;
  101. var lblStyle:YLabelStyle = new YLabelStyle(json, name);
  102. this.style = lblStyle.style;
  103. //
  104. // TODO: hack, if the user has not defined either left or right
  105. // by default set left axis to show and right to hide.
  106. //
  107. if ( !json[axis_name] && axis_name!='y_axis' )
  108. this.style.show_labels = false;
  109. //
  110. //
  111. // Default to using "rotate" from the y_axis level
  112. if ( json[axis_name] && json[axis_name].rotate ) {
  113. this.style.rotate = json[axis_name].rotate;
  114. }
  115. // Next override with any values at the y_axis.labels level
  116. if (( json[axis_name] != null ) &&
  117. ( json[axis_name].labels != null ) ) {
  118. object_helper.merge_2( json[axis_name].labels, this.style );
  119. }
  120. this.add_labels(values);
  121. }
  122. private function add_labels(values:Array): void {
  123. // are the Y Labels visible?
  124. if( !this.style.show_labels )
  125. return;
  126. // labels
  127. var pos:Number = 0;
  128. for each ( var v:Object in values )
  129. {
  130. var lblStyle:Object = { };
  131. object_helper.merge_2( this.style, lblStyle );
  132. object_helper.merge_2( v, lblStyle );
  133. if ( lblStyle.visible )
  134. {
  135. var tmp:YTextField = this.make_label( lblStyle );
  136. tmp.y_val = v.pos;
  137. this.addChild(tmp);
  138. pos++;
  139. }
  140. }
  141. }
  142. /**
  143. * This is called from the init function, because it is only after the Sprite
  144. * is added to the stagethat we know the size of the flash window and know
  145. * how many ticks/labelswe auto generate
  146. */
  147. public function make_labels(min:Number, max:Number, steps:Number): void {
  148. tr.aces('make_labels', this.i_need_labels, min, max, false, steps, this.lblText);
  149. tr.aces(this.style.show_labels);
  150. if ( !this.i_need_labels )
  151. return;
  152. this.i_need_labels = false;
  153. this.make_labels_(min, max, false, steps, this.lblText);
  154. }
  155. //
  156. // use Y Min, Y Max and Y Steps to create an array of
  157. // Y labels:
  158. //
  159. protected function make_labels_(min:Number, max:Number, right:Boolean, steps:Number, lblText:String):void {
  160. var values:Array = [];
  161. var min_:Number = Math.min( min, max );
  162. var max_:Number = Math.max( min, max );
  163. // hack: hack: http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_13989&sliceId=1
  164. max_ += 0.000004;
  165. var eek:Number = 0;
  166. for( var i:Number = min_; i <= max_; i+=steps ) {
  167. values.push( { val:lblText, pos:i } );
  168. // make sure we don't generate too many labels:
  169. if( eek++ > 250 ) break;
  170. }
  171. this.add_labels(values);
  172. }
  173. private function make_label( lblStyle:Object ):YTextField
  174. {
  175. lblStyle.colour = string.Utils.get_colour(lblStyle.colour);
  176. var tf:YTextField = new YTextField();
  177. //tf.border = true;
  178. tf.text = this.replace_magic_values(lblStyle.val, lblStyle.pos);
  179. var fmt:TextFormat = new TextFormat();
  180. fmt.color = lblStyle.colour;
  181. fmt.font = lblStyle.rotate == "vertical" ? "spArial" : "Verdana";
  182. fmt.size = lblStyle.size;
  183. fmt.align = "right";
  184. tf.setTextFormat(fmt);
  185. tf.autoSize = "right";
  186. if (lblStyle.rotate == "vertical")
  187. {
  188. tf.rotation = 270;
  189. tf.embedFonts = true;
  190. tf.antiAliasType = flash.text.AntiAliasType.ADVANCED;
  191. }
  192. return tf;
  193. }
  194. // move y axis labels to the correct x pos
  195. public function resize( left:Number, sc:ScreenCoords ):void
  196. {
  197. }
  198. public function get_width():Number{
  199. var max:Number = 0;
  200. for( var i:Number=0; i<this.numChildren; i++ )
  201. {
  202. var tf:YTextField = this.getChildAt(i) as YTextField;
  203. max = Math.max( max, tf.width );
  204. }
  205. return max;
  206. }
  207. public function die(): void {
  208. while ( this.numChildren > 0 )
  209. this.removeChildAt(0);
  210. }
  211. private function replace_magic_values(labelText:String, yVal:Number):String {
  212. labelText = labelText.replace('#val#', NumberUtils.formatNumber(yVal));
  213. return labelText;
  214. }
  215. }
  216. }