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