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