1 | package charts {
|
---|
2 |
|
---|
3 | import flash.display.Sprite;
|
---|
4 | import charts.series.Element;
|
---|
5 | import flash.geom.Point;
|
---|
6 | import elements.axis.XAxisLabels;
|
---|
7 |
|
---|
8 | public class Base extends Sprite {
|
---|
9 |
|
---|
10 | // accessed by the Keys object to display the key
|
---|
11 | protected var key:String;
|
---|
12 | protected var font_size:Number;
|
---|
13 |
|
---|
14 |
|
---|
15 | public var colour:Number;
|
---|
16 | public var line_width:Number;
|
---|
17 | public var circle_size:Number;
|
---|
18 |
|
---|
19 | //
|
---|
20 | // hold the Element values, for lines this is an
|
---|
21 | // array of string Y values, for Candle it is an
|
---|
22 | // array of string 'high,open,low,close' values,
|
---|
23 | // for scatter it is 'x,y' etc...
|
---|
24 | //
|
---|
25 | public var values:Array;
|
---|
26 |
|
---|
27 | protected var axis:Number;
|
---|
28 |
|
---|
29 | public function Base()
|
---|
30 | {}
|
---|
31 |
|
---|
32 | public function get_colour(): Number {
|
---|
33 | return this.colour;
|
---|
34 | }
|
---|
35 |
|
---|
36 | //
|
---|
37 | // return an array of key info objects:
|
---|
38 | //
|
---|
39 | public function get_keys(): Object {
|
---|
40 |
|
---|
41 | var tmp:Array = [];
|
---|
42 |
|
---|
43 | // some lines may not have a key
|
---|
44 | if( (this.font_size > 0) && (this.key != '' ) )
|
---|
45 | tmp.push( { 'text':this.key, 'font-size':this.font_size, 'colour':this.get_colour() } );
|
---|
46 |
|
---|
47 | return tmp;
|
---|
48 | }
|
---|
49 |
|
---|
50 | //
|
---|
51 | // whatever sets of data that *may* be attached to the right
|
---|
52 | // Y Axis call this to see if they are attached to it or not.
|
---|
53 | // All lines, area and bar charts call this.
|
---|
54 | //
|
---|
55 | protected function which_axis_am_i_attached_to( data:Array, i:Number ): Number {
|
---|
56 | //
|
---|
57 | // some data sets are attached to the right
|
---|
58 | // Y axis (and min max), in the future we
|
---|
59 | // may support many axis
|
---|
60 | //
|
---|
61 | if( data['show_y2'] != undefined )
|
---|
62 | if( data['show_y2'] != 'false' )
|
---|
63 | if( data['y2_lines'] != undefined )
|
---|
64 | {
|
---|
65 | var tmp:Array = data.y2_lines.split(",");
|
---|
66 | var pos:Number = tmp.indexOf( i.toString() );
|
---|
67 |
|
---|
68 | if ( pos == -1 )
|
---|
69 | return 1;
|
---|
70 | else
|
---|
71 | return 2; // <-- this line found in y2_lines, so it is attached to axis 2 (right axis)
|
---|
72 | }
|
---|
73 |
|
---|
74 | return 1;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * may be called by main.as to make the X Axis labels
|
---|
80 | * @return
|
---|
81 | */
|
---|
82 | public function get_max_x():Number {
|
---|
83 |
|
---|
84 | var max:Number = Number.MIN_VALUE;
|
---|
85 | //
|
---|
86 | // count the non-mask items:
|
---|
87 | //
|
---|
88 | for ( var i:Number = 0; i < this.numChildren; i++ ) {
|
---|
89 | if ( this.getChildAt(i) is Element ) {
|
---|
90 |
|
---|
91 | var e:Element = this.getChildAt(i) as Element
|
---|
92 | max = Math.max( max, e.get_x() );
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | return max;
|
---|
97 | }
|
---|
98 |
|
---|
99 | public function get_min_x():Number {
|
---|
100 |
|
---|
101 | var min:Number = Number.MAX_VALUE;
|
---|
102 | //
|
---|
103 | // count the non-mask items:
|
---|
104 | //
|
---|
105 | for ( var i:Number = 0; i < this.numChildren; i++ ) {
|
---|
106 | if ( this.getChildAt(i) is Element ) {
|
---|
107 |
|
---|
108 | var e:Element = this.getChildAt(i) as Element
|
---|
109 | min = Math.min( min, e.get_x() );
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | return min;
|
---|
114 | }
|
---|
115 |
|
---|
116 | //
|
---|
117 | // this should be overriden
|
---|
118 | //
|
---|
119 | public function resize( sc:ScreenCoordsBase ):void{}
|
---|
120 |
|
---|
121 | //public function draw( val:String, mc:Object ):void {}
|
---|
122 |
|
---|
123 |
|
---|
124 |
|
---|
125 |
|
---|
126 | //
|
---|
127 | // TODO: old remove when tooltips tested
|
---|
128 | //
|
---|
129 | public function closest( x:Number, y:Number ): Object {
|
---|
130 | var shortest:Number = Number.MAX_VALUE;
|
---|
131 | var closest:Element = null;
|
---|
132 | var dx:Number;
|
---|
133 |
|
---|
134 | for ( var i:Number = 0; i < this.numChildren; i++ ) {
|
---|
135 |
|
---|
136 | //
|
---|
137 | // some of the children will will mask
|
---|
138 | // Sprites, so filter those out:
|
---|
139 | //
|
---|
140 | if( this.getChildAt(i) is Element ) {
|
---|
141 |
|
---|
142 | var e:Element = this.getChildAt(i) as Element;
|
---|
143 | e.set_tip( false );
|
---|
144 |
|
---|
145 | dx = Math.abs( x -e.x );
|
---|
146 |
|
---|
147 | if( dx < shortest ) {
|
---|
148 | shortest = dx;
|
---|
149 | closest = e;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | var dy:Number = 0;
|
---|
155 | if( closest )
|
---|
156 | dy = Math.abs( y - closest.y );
|
---|
157 |
|
---|
158 | return { element:closest, distance_x:shortest, distance_y:dy };
|
---|
159 | }
|
---|
160 |
|
---|
161 | //
|
---|
162 | // Line and bar charts will normally only have one
|
---|
163 | // Element at any X position, but when using Radar axis
|
---|
164 | // you may get many at any give X location.
|
---|
165 | //
|
---|
166 | // Scatter charts can have many items at the same X position
|
---|
167 | //
|
---|
168 | public function closest_2( x:Number, y:Number ): Array {
|
---|
169 |
|
---|
170 | // get the closest Elements X value
|
---|
171 | var x:Number = closest_x(x);
|
---|
172 | var tmp:Array = this.get_all_at_this_x_pos(x);
|
---|
173 |
|
---|
174 | // tr.aces('tmp.length', tmp.length);
|
---|
175 |
|
---|
176 | var closest:Array = this.get_closest_y(tmp, y);
|
---|
177 | var dy:Number = Math.abs( y - closest.y );
|
---|
178 | // tr.aces('closest.length', closest.length);
|
---|
179 |
|
---|
180 | return closest;
|
---|
181 | }
|
---|
182 |
|
---|
183 | //
|
---|
184 | // get the X value of the closest points to the mouse
|
---|
185 | //
|
---|
186 | private function closest_x( x:Number ):Number {
|
---|
187 |
|
---|
188 | var closest:Number = Number.MAX_VALUE;
|
---|
189 | var p:flash.geom.Point;
|
---|
190 | var x_pos:Number;
|
---|
191 | var dx:Number;
|
---|
192 |
|
---|
193 | for ( var i:Number = 0; i < this.numChildren; i++ ) {
|
---|
194 |
|
---|
195 | //
|
---|
196 | // some of the children will will mask
|
---|
197 | // Sprites, so filter those out:
|
---|
198 | //
|
---|
199 | if( this.getChildAt(i) is Element ) {
|
---|
200 |
|
---|
201 | var e:Element = this.getChildAt(i) as Element;
|
---|
202 |
|
---|
203 | p = e.get_mid_point();
|
---|
204 | dx = Math.abs( x - p.x );
|
---|
205 |
|
---|
206 | if( dx < closest ) {
|
---|
207 | closest = dx;
|
---|
208 | x_pos = p.x;
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | return x_pos;
|
---|
214 | }
|
---|
215 |
|
---|
216 | //
|
---|
217 | // get all the Elements at this X position
|
---|
218 | // BarStack overrides this
|
---|
219 | //
|
---|
220 | protected function get_all_at_this_x_pos( x:Number ):Array {
|
---|
221 |
|
---|
222 | var tmp:Array = new Array();
|
---|
223 | var p:flash.geom.Point;
|
---|
224 | var e:Element;
|
---|
225 |
|
---|
226 | for ( var i:Number = 0; i < this.numChildren; i++ ) {
|
---|
227 |
|
---|
228 | // some of the children will will mask
|
---|
229 | // Sprites, so filter those out:
|
---|
230 | //
|
---|
231 | if( this.getChildAt(i) is Element ) {
|
---|
232 |
|
---|
233 | e = this.getChildAt(i) as Element;
|
---|
234 |
|
---|
235 | //
|
---|
236 | // Point elements are invisible by default.
|
---|
237 | //
|
---|
238 | // Prevent invisible points from showing tooltips
|
---|
239 | // For scatter line area
|
---|
240 | //if (e.visible)
|
---|
241 | //{
|
---|
242 | p = e.get_mid_point();
|
---|
243 | if ( p.x == x )
|
---|
244 | tmp.push( e );
|
---|
245 | //}
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | return tmp;
|
---|
250 | }
|
---|
251 |
|
---|
252 | //
|
---|
253 | // scatter charts may have many Elements in the same
|
---|
254 | // x, y location
|
---|
255 | //
|
---|
256 | private function get_closest_y( elements:Array, y:Number):Array {
|
---|
257 |
|
---|
258 | var y_min:Number = Number.MAX_VALUE;
|
---|
259 | var dy:Number;
|
---|
260 | var closest:Array = new Array();
|
---|
261 | var p:flash.geom.Point;
|
---|
262 | var e:Element;
|
---|
263 |
|
---|
264 | // get min Y distance
|
---|
265 | for each( e in elements ) {
|
---|
266 |
|
---|
267 | p = e.get_mid_point();
|
---|
268 | dy = Math.abs( y - p.y );
|
---|
269 |
|
---|
270 | y_min = Math.min( dy, y_min );
|
---|
271 | }
|
---|
272 |
|
---|
273 | // select all Elements at this Y pos
|
---|
274 | for each( e in elements ) {
|
---|
275 |
|
---|
276 | p = e.get_mid_point();
|
---|
277 | dy = Math.abs( y - p.y );
|
---|
278 | if( dy == y_min )
|
---|
279 | closest.push(e);
|
---|
280 | }
|
---|
281 |
|
---|
282 | return closest;
|
---|
283 | }
|
---|
284 |
|
---|
285 | //
|
---|
286 | // scatter charts may have many Elements in the same
|
---|
287 | // x, y location
|
---|
288 | //
|
---|
289 | public function mouse_proximity( x:Number, y:Number ): Array {
|
---|
290 |
|
---|
291 | var closest:Number = Number.MAX_VALUE;
|
---|
292 | var p:flash.geom.Point;
|
---|
293 | var i:Number;
|
---|
294 | var e:Element;
|
---|
295 | var mouse:flash.geom.Point = new flash.geom.Point(x, y);
|
---|
296 |
|
---|
297 | //
|
---|
298 | // find the closest Elements
|
---|
299 | //
|
---|
300 | for ( i=0; i < this.numChildren; i++ ) {
|
---|
301 |
|
---|
302 | // filter mask Sprites
|
---|
303 | if( this.getChildAt(i) is Element ) {
|
---|
304 |
|
---|
305 | e = this.getChildAt(i) as Element;
|
---|
306 | closest = Math.min( flash.geom.Point.distance(e.get_mid_point(), mouse), closest );
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | //
|
---|
311 | // grab all Elements at this distance
|
---|
312 | //
|
---|
313 | var close:Array = [];
|
---|
314 | for ( i=0; i < this.numChildren; i++ ) {
|
---|
315 |
|
---|
316 | // filter mask Sprites
|
---|
317 | if( this.getChildAt(i) is Element ) {
|
---|
318 |
|
---|
319 | e = this.getChildAt(i) as Element;
|
---|
320 | if ( flash.geom.Point.distance(e.get_mid_point(), mouse) == closest )
|
---|
321 | close.push(e);
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | return close;
|
---|
326 | }
|
---|
327 |
|
---|
328 |
|
---|
329 |
|
---|
330 | //
|
---|
331 | // this is a backup function so if the mouse leaves the
|
---|
332 | // movie for some reason without raising the mouse
|
---|
333 | // out event (this happens if the user is wizzing the mouse about)
|
---|
334 | //
|
---|
335 | public function mouse_out():void {
|
---|
336 | for ( var i:Number = 0; i < this.numChildren; i++ ) {
|
---|
337 |
|
---|
338 | // filter out the mask elements in line charts
|
---|
339 | if( this.getChildAt(i) is Element ) {
|
---|
340 |
|
---|
341 | var e:Element = this.getChildAt(i) as Element;
|
---|
342 | e.set_tip(false);
|
---|
343 | }
|
---|
344 | }
|
---|
345 | }
|
---|
346 |
|
---|
347 |
|
---|
348 | //
|
---|
349 | // index of item (bar, point, pie slice, horizontal bar) may be used
|
---|
350 | // to look up its X value (bar,point) or Y value (H Bar) or used as
|
---|
351 | // the sequence number (Pie)
|
---|
352 | //
|
---|
353 | protected function get_element( index:Number, value:Object ): Element {
|
---|
354 | return null;
|
---|
355 | }
|
---|
356 |
|
---|
357 | public function add_values():void {
|
---|
358 |
|
---|
359 | // keep track of the X position (column)
|
---|
360 | var index:Number = 0;
|
---|
361 |
|
---|
362 | for each ( var val:Object in this.values )
|
---|
363 | {
|
---|
364 | var tmp:Element;
|
---|
365 |
|
---|
366 | //
|
---|
367 | // TODO: fix or document what is happening in link-null-bug.txt
|
---|
368 | //
|
---|
369 |
|
---|
370 | // filter out the 'null' values
|
---|
371 | if( val != null )
|
---|
372 | {
|
---|
373 | tmp = this.get_element( index, val );
|
---|
374 |
|
---|
375 | if( tmp.line_mask != null )
|
---|
376 | this.addChild( tmp.line_mask );
|
---|
377 |
|
---|
378 | this.addChild( tmp );
|
---|
379 | }
|
---|
380 |
|
---|
381 | index++;
|
---|
382 | }
|
---|
383 | }
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * See ObjectCollection tooltip_replace_labels
|
---|
387 | *
|
---|
388 | * @param labels
|
---|
389 | */
|
---|
390 | public function tooltip_replace_labels( labels:XAxisLabels ):void {
|
---|
391 | for ( var i:Number = 0; i < this.numChildren; i++ ) {
|
---|
392 |
|
---|
393 | // filter out the mask elements in line charts
|
---|
394 | if( this.getChildAt(i) is Element ) {
|
---|
395 |
|
---|
396 | var e:Element = this.getChildAt(i) as Element;
|
---|
397 | e.tooltip_replace_labels( labels );
|
---|
398 | }
|
---|
399 | }
|
---|
400 | }
|
---|
401 |
|
---|
402 | public function die():void {
|
---|
403 |
|
---|
404 | for ( var i:Number = 0; i < this.numChildren; i++ )
|
---|
405 | if ( this.getChildAt(i) is Element ) {
|
---|
406 |
|
---|
407 | var e:Element = this.getChildAt(i) as Element;
|
---|
408 | e.die();
|
---|
409 | }
|
---|
410 |
|
---|
411 | while ( this.numChildren > 0 )
|
---|
412 | this.removeChildAt(0);
|
---|
413 | }
|
---|
414 | }
|
---|
415 | }
|
---|