source: code/Website/open-flash-chart/charts/ObjectCollection.as

Last change on this file was 7849, checked in by dennisw, 15 years ago
File size: 6.1 KB
Line 
1package charts {
2 import charts.series.Element;
3 import flash.geom.Point;
4 import elements.axis.XAxisLabels;
5
6 public class ObjectCollection
7 {
8 public var sets:Array;
9 public var groups:Number;
10
11 public function ObjectCollection() {
12 this.sets = new Array();
13 }
14
15 public function add( set:Base ): void {
16 this.sets.push( set );
17 }
18
19
20 public function get_max_x():Number {
21
22 var max:Number = Number.MIN_VALUE;
23
24 for each( var o:Base in this.sets )
25 max = Math.max( max, o.get_max_x() );
26
27 return max;
28 }
29
30 public function get_min_x():Number {
31
32 var min:Number = Number.MAX_VALUE;
33
34 for each( var o:Base in this.sets )
35 min = Math.min( min, o.get_min_x() );
36
37 return min;
38 }
39
40
41 // get x, y co-ords of vals
42 public function resize( sc:ScreenCoordsBase ):void {
43 for each ( var o:Base in this.sets )
44 o.resize( sc );
45 }
46
47 /**
48 * Tell each set to update the tooltip string and
49 * eplace all #x_label# with the label
50 *
51 * @param labels
52 */
53 public function tooltip_replace_labels( labels:XAxisLabels ):void {
54
55 for each ( var o:Base in this.sets )
56 o.tooltip_replace_labels( labels );
57 }
58
59 public function mouse_out():void {
60 for each( var s:Base in this.sets )
61 s.mouse_out();
62 }
63
64
65 private function closest( x:Number, y:Number ):Element {
66 var o:Object;
67 var s:Base;
68
69 // get closest points from each data set
70 var closest:Array = new Array();
71 for each( s in this.sets )
72 closest.push( s.closest( x, y ) );
73
74 // find closest point along X axis
75 var min:Number = Number.MAX_VALUE;
76 for each( o in closest )
77 min = Math.min( min, o.distance_x );
78
79 //
80 // now select all points that are the
81 // min (see above) distance along the X axis
82 //
83 var xx:Object = {element:null, distance_x:Number.MAX_VALUE, distance_y:Number.MAX_VALUE };
84 for each( o in closest ) {
85
86 if( o.distance_x == min )
87 {
88 // these share the same X position, so choose
89 // the closest to the mouse in the Y
90 if( o.distance_y < xx.distance_y )
91 xx = o;
92 }
93 }
94
95 // pie charts may not return an element
96 if( xx.element )
97 xx.element.set_tip( true );
98
99 return xx.element;
100 }
101
102 /*
103
104 hollow
105 line --> ------O---------------O-----
106
107 +-----+
108 | B |
109 +-----+ | +-----+
110 | A | | | C +- - -
111 | | | | | D
112 +-----+-----+---+-----+- - -
113 1 2
114
115 */
116 public function mouse_move( x:Number, y:Number ):Element {
117 //
118 // is the mouse over, above or below a
119 // bar or point? For grouped bar charts,
120 // two bars will share an X co-ordinate
121 // and be the same distance from the
122 // mouse. For example, if the mouse is
123 // in position 1 in diagram above. This
124 // filters out all items that are not
125 // above or below the mouse:
126 //
127 var e:Element = null;// this.inside__(x, y);
128
129 if ( !e )
130 {
131 //
132 // no Elements are above or below the mouse,
133 // so we select the BEST item to show (mouse
134 // is in position 2)
135 //
136 e = this.closest(x, y);
137 }
138
139 return e;
140 }
141
142
143 //
144 // Usually this will return an Array of one Element to
145 // the Tooltip, but some times 2 (or more) Elements will
146 // be on top of each other
147 //
148 public function closest_2( x:Number, y:Number ):Array {
149
150 var e:Element;
151 var s:Base;
152 var p:flash.geom.Point;
153
154 //
155 // get closest points from each data set
156 //
157 var closest:Array = new Array();
158 for each( s in this.sets ) {
159
160 var tmp:Array = s.closest_2( x, y );
161 for each( e in tmp )
162 closest.push( e );
163 }
164
165 //
166 // find closest point along X axis
167 // different sets may return Elements
168 // in different X locations
169 //
170 var min_x:Number = Number.MAX_VALUE;
171 for each( e in closest ) {
172
173 p = e.get_mid_point();
174 min_x = Math.min( min_x, Math.abs( x - p.x ) );
175 }
176
177 //
178 // filter out the Elements that
179 // are too far away along the X axis
180 //
181 var good_x:Array = new Array();
182 for each( e in closest ) {
183
184 p = e.get_mid_point();
185 if ( Math.abs( x - p.x ) == min_x )
186 good_x.push( e );
187 }
188
189 //
190 // now get min_y from filtered array
191 //
192 var min_y:Number = Number.MAX_VALUE;
193 for each( e in good_x ) {
194
195 p = e.get_mid_point();
196 min_y = Math.min( min_y, Math.abs( y - p.y ) );
197 }
198
199 //
200 // now filter out any that are not min_y
201 //
202 var good_x_and_y:Array = new Array();
203 for each( e in good_x ) {
204
205 p = e.get_mid_point();
206 if ( Math.abs( y - p.y ) == min_y )
207 good_x_and_y.push( e );
208 }
209
210 return good_x_and_y;
211 }
212
213 //
214 // find the closest point to the mouse
215 //
216 public function mouse_move_proximity( x:Number, y:Number ):Array {
217 var e:Element;
218 var s:Base;
219 var p:flash.geom.Point;
220
221 //
222 // get closest points from each data set
223 //
224 var closest:Array = new Array();
225 for each( s in this.sets ) {
226
227 var tmp:Array = s.mouse_proximity( x, y );
228 for each( e in tmp )
229 closest.push( e );
230 }
231
232 //
233 // find the min distance to these
234 //
235 var min_dist:Number = Number.MAX_VALUE;
236 var mouse:flash.geom.Point = new flash.geom.Point(x, y);
237 for each( e in closest ) {
238 min_dist = Math.min( flash.geom.Point.distance(e.get_mid_point(), mouse), min_dist );
239 }
240
241 // keep these closest Elements
242 var close:Array = [];
243 for each( e in closest ) {
244 if ( flash.geom.Point.distance(e.get_mid_point(), mouse) == min_dist )
245 close.push( e );
246 }
247
248 return close;
249 }
250
251 //
252 // are we resizing a PIE chart?
253 //
254 public function has_pie():Boolean {
255
256 if ( this.sets.length > 0 && ( this.sets[0] is Pie ) )
257 return true;
258 else
259 return false;
260 }
261
262 /**
263 * To stop memory leaks we explicitly kill all
264 * our children
265 */
266 public function die():void {
267
268 for each( var o:Base in this.sets )
269 o.die();
270 }
271 }
272}
Note: See TracBrowser for help on using the repository browser.