RadarSpokeLabels.as 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package elements.axis {
  2. import flash.text.TextField;
  3. import flash.display.Sprite;
  4. import flash.text.TextFormat;
  5. import string.Utils;
  6. import flash.geom.Point;
  7. public class RadarSpokeLabels extends Sprite{
  8. private var style:Object;
  9. public var labels:Array;
  10. public function RadarSpokeLabels( json:Object ) {
  11. // default values
  12. this.style = {
  13. colour: '#784016'
  14. };
  15. if( json != null )
  16. object_helper.merge_2( json, this.style );
  17. // tr.ace_json(this.style);
  18. this.style.colour = Utils.get_colour( this.style.colour );
  19. // cache the text for tooltips
  20. this.labels = new Array();
  21. var values:Array;
  22. var ok:Boolean = false;
  23. if( ( this.style.labels is Array ) && ( this.style.labels.length > 0 ) )
  24. {
  25. for each( var s:Object in this.style.labels )
  26. this.add( s, this.style );
  27. }
  28. }
  29. public function add( label:Object, style:Object ) : void
  30. {
  31. var label_style:Object = {
  32. colour: style.colour,
  33. text: '',
  34. size: 11
  35. };
  36. if( label is String )
  37. label_style.text = label as String;
  38. else {
  39. object_helper.merge_2( label, label_style );
  40. }
  41. // our parent colour is a number, but
  42. // we may have our own colour:
  43. if( label_style.colour is String )
  44. label_style.colour = Utils.get_colour( label_style.colour );
  45. this.labels.push( label_style.text );
  46. var l:TextField = this.make_label( label_style );
  47. this.addChild( l );
  48. }
  49. public function make_label( label_style:Object ):TextField {
  50. // we create the text in its own movie clip
  51. var tf:TextField = new TextField();
  52. tf.x = 0;
  53. tf.y = 0;
  54. var tmp:Array = label_style.text.split( '<br>' );
  55. var text:String = tmp.join('\n');
  56. tf.text = text;
  57. var fmt:TextFormat = new TextFormat();
  58. fmt.color = label_style.colour;
  59. fmt.font = "Verdana";
  60. fmt.size = label_style.size;
  61. fmt.align = "right";
  62. tf.setTextFormat(fmt);
  63. tf.autoSize = "left";
  64. tf.visible = true;
  65. return tf;
  66. }
  67. // move y axis labels to the correct x pos
  68. public function resize( sc:ScreenCoordsRadar ):void {
  69. var tf:TextField;
  70. //
  71. // loop over the lables and make sure they are on the screen,
  72. // reduce the radius until they fit
  73. //
  74. var i:Number = 0;
  75. var outside:Boolean;
  76. do
  77. {
  78. outside = false;
  79. this.resize_2( sc );
  80. for ( i = 0; i < this.numChildren; i++ )
  81. {
  82. tf = this.getChildAt(i) as TextField;
  83. if( (tf.x < sc.left) ||
  84. (tf.y < sc.top) ||
  85. (tf.y + tf.height > sc.bottom ) ||
  86. (tf.x + tf.width > sc.right)
  87. )
  88. outside = true;
  89. }
  90. sc.reduce_radius();
  91. }
  92. while ( outside && sc.get_radius() > 10 );
  93. //
  94. //
  95. //
  96. }
  97. private function resize_2( sc:ScreenCoordsRadar ):void {
  98. var i:Number;
  99. var tf:TextField;
  100. var mid_x:Number = sc.get_center_x();
  101. // now move it to the correct Y, vertical center align
  102. for ( i = 0; i < this.numChildren; i++ ) {
  103. tf = this.getChildAt(i) as TextField;
  104. var p:flash.geom.Point = sc.get_get_x_from_pos_and_y_from_val( i, sc.get_max() );
  105. if ( p.x > mid_x )
  106. tf.x = p.x; // <-- right align the text
  107. else
  108. tf.x = p.x - tf.width; // <-- left align the text
  109. if ( i == 0 ) {
  110. //
  111. // this is the top label and will overwrite
  112. // the radius label -- so we right align it
  113. // because the radius labels are left aligned
  114. //
  115. tf.y = p.y - tf.height;
  116. tf.x = p.x;
  117. }
  118. else
  119. tf.y = p.y;
  120. }
  121. }
  122. public function die(): void {
  123. this.style = null;
  124. this.labels = null;
  125. this.graphics.clear();
  126. while ( this.numChildren > 0 )
  127. this.removeChildAt(0);
  128. }
  129. }
  130. }