RadarAxisLabels.as 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package elements.axis {
  2. import flash.text.TextField;
  3. import flash.display.Sprite;
  4. import flash.text.TextFormat;
  5. import string.Utils;
  6. public class RadarAxisLabels extends Sprite{
  7. private var style:Object;
  8. public var labels:Array;
  9. public function RadarAxisLabels( json:Object ) {
  10. // default values
  11. this.style = {
  12. colour: '#784016',
  13. steps: 1
  14. };
  15. if( json != null )
  16. object_helper.merge_2( json, this.style );
  17. this.style.colour = Utils.get_colour( this.style.colour );
  18. // cache the text for tooltips
  19. this.labels = new Array();
  20. var values:Array;
  21. var ok:Boolean = false;
  22. if( ( this.style.labels is Array ) && ( this.style.labels.length > 0 ) )
  23. {
  24. for each( var s:Object in this.style.labels )
  25. this.add( s, this.style );
  26. }
  27. }
  28. public function add( label:Object, style:Object ) : void
  29. {
  30. var label_style:Object = {
  31. colour: style.colour,
  32. text: '',
  33. size: style.size,
  34. visible: true
  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. //
  47. // inheriting the 'visible' attribute
  48. // is complext due to the 'steps' value
  49. // only some labels will be visible
  50. //
  51. if( label_style.visible == null )
  52. {
  53. //
  54. // some labels will be invisible due to our parents step value
  55. //
  56. if ( ( (this.labels.length - 1) % style.steps ) == 0 )
  57. label_style.visible = true;
  58. else
  59. label_style.visible = false;
  60. }
  61. var l:TextField = this.make_label( label_style );
  62. this.addChild( l );
  63. }
  64. public function make_label( label_style:Object ):TextField {
  65. // we create the text in its own movie clip
  66. var tf:TextField = new TextField();
  67. tf.x = 0;
  68. tf.y = 0;
  69. tf.text = label_style.text;
  70. var fmt:TextFormat = new TextFormat();
  71. fmt.color = label_style.colour;
  72. fmt.font = "Verdana";
  73. fmt.size = label_style.size;
  74. fmt.align = "right";
  75. tf.setTextFormat(fmt);
  76. tf.autoSize = "left";
  77. tf.visible = label_style.visible;
  78. return tf;
  79. }
  80. // move y axis labels to the correct x pos
  81. public function resize( sc:ScreenCoordsRadar ):void {
  82. var i:Number;
  83. var tf:TextField;
  84. var center:Number = sc.get_center_x();
  85. for( i=0; i<this.numChildren; i++ ) {
  86. // right align
  87. tf = this.getChildAt(i) as TextField;
  88. tf.x = center - tf.width;
  89. }
  90. // now move it to the correct Y, vertical center align
  91. for ( i = 0; i < this.numChildren; i++ ) {
  92. tf = this.getChildAt(i) as TextField;
  93. tf.y = ( sc.get_y_from_val( i, false ) - (tf.height / 2) );
  94. }
  95. }
  96. public function die(): void {
  97. this.style = null;
  98. this.labels = null;
  99. this.graphics.clear();
  100. while ( this.numChildren > 0 )
  101. this.removeChildAt(0);
  102. }
  103. }
  104. }