1 | package charts.series.bars {
|
---|
2 |
|
---|
3 | import flash.events.Event;
|
---|
4 | import flash.events.MouseEvent;
|
---|
5 | import flash.display.Sprite;
|
---|
6 | import caurina.transitions.Tweener;
|
---|
7 | import caurina.transitions.Equations;
|
---|
8 | import flash.geom.Point;
|
---|
9 | import global.Global;
|
---|
10 | import charts.series.Element;
|
---|
11 | import string.Utils;
|
---|
12 |
|
---|
13 | public class Base extends Element
|
---|
14 | {
|
---|
15 | protected var tip_pos:flash.geom.Point;
|
---|
16 | protected var colour:Number;
|
---|
17 | protected var group:Number;
|
---|
18 | protected var top:Number;
|
---|
19 | protected var bottom:Number;
|
---|
20 | protected var mouse_out_alpha:Number;
|
---|
21 | private var on_show_animate:Boolean;
|
---|
22 | protected var on_show:Properties;
|
---|
23 |
|
---|
24 |
|
---|
25 | public function Base( index:Number, props:Properties, group:Number )
|
---|
26 | {
|
---|
27 | super();
|
---|
28 | this.index = index;
|
---|
29 | this.parse_value(props);
|
---|
30 | this.colour = props.get_colour('colour');
|
---|
31 |
|
---|
32 | this.tooltip = this.replace_magic_values( props.get('tip') );
|
---|
33 |
|
---|
34 | this.group = group;
|
---|
35 | this.visible = true;
|
---|
36 | this.on_show_animate = true;
|
---|
37 | this.on_show = props.get('on-show');
|
---|
38 |
|
---|
39 | // remember what our original alpha is:
|
---|
40 | this.mouse_out_alpha = props.get('alpha');
|
---|
41 | // set the sprit alpha:
|
---|
42 | this.alpha = this.mouse_out_alpha;
|
---|
43 |
|
---|
44 | this.addEventListener(MouseEvent.MOUSE_OVER, this.mouseOver);
|
---|
45 | this.addEventListener(MouseEvent.MOUSE_OUT, this.mouseOut);
|
---|
46 |
|
---|
47 | //
|
---|
48 | // This is UGLY!!! We need to decide if we are passing in a SINGLE style object,
|
---|
49 | // or many parameters....
|
---|
50 | //
|
---|
51 | if ( props.has('on-click') ) // <-- may be null/not set
|
---|
52 | if( props.get('on-click') != false ) // <-- may be FALSE
|
---|
53 | this.set_on_click( props.get('on-click') );
|
---|
54 |
|
---|
55 | if( props.has('axis') )
|
---|
56 | if( props.get('axis') == 'right' )
|
---|
57 | this.right_axis = true;
|
---|
58 | }
|
---|
59 |
|
---|
60 | //
|
---|
61 | // most line and bar charts have a single value which is the
|
---|
62 | // Y position, some like candle and scatter have many values
|
---|
63 | // and will override this method to parse their value
|
---|
64 | //
|
---|
65 | protected function parse_value( props:Properties ):void {
|
---|
66 |
|
---|
67 | if ( !props.has('bottom') ) {
|
---|
68 | // align to Y min OR zero
|
---|
69 | props.set('bottom', Number.MIN_VALUE );
|
---|
70 | }
|
---|
71 |
|
---|
72 | this.top = props.get('top');
|
---|
73 | this.bottom = props.get('bottom');
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected function replace_magic_values( t:String ): String {
|
---|
77 |
|
---|
78 | t = t.replace('#top#', NumberUtils.formatNumber( this.top ));
|
---|
79 | t = t.replace('#bottom#', NumberUtils.formatNumber( this.bottom ));
|
---|
80 | t = t.replace('#val#', NumberUtils.formatNumber( this.top - this.bottom ));
|
---|
81 |
|
---|
82 | return t;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | //
|
---|
87 | // for tooltip closest - return the middle point
|
---|
88 | //
|
---|
89 | public override function get_mid_point():flash.geom.Point {
|
---|
90 |
|
---|
91 | //
|
---|
92 | // bars mid point
|
---|
93 | //
|
---|
94 | return new flash.geom.Point( this.x + (this.width/2), this.y );
|
---|
95 | }
|
---|
96 |
|
---|
97 | public override function mouseOver(event:Event):void {
|
---|
98 | this.is_tip = true;
|
---|
99 | Tweener.addTween(this, { alpha:1, time:0.6, transition:Equations.easeOutCirc } );
|
---|
100 | }
|
---|
101 |
|
---|
102 | public override function mouseOut(event:Event):void {
|
---|
103 | this.is_tip = false;
|
---|
104 | Tweener.addTween(this, { alpha:this.mouse_out_alpha, time:0.8, transition:Equations.easeOutElastic } );
|
---|
105 | }
|
---|
106 |
|
---|
107 | // override this:
|
---|
108 | public override function resize( sc:ScreenCoordsBase ):void {}
|
---|
109 |
|
---|
110 | //
|
---|
111 | // tooltip.left for bars center over the bar
|
---|
112 | //
|
---|
113 | public override function get_tip_pos(): Object {
|
---|
114 | return {x:this.tip_pos.x, y:this.tip_pos.y };
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | //
|
---|
119 | // Called by most of the bar charts.
|
---|
120 | // Moves the Sprite into the correct position, then
|
---|
121 | // returns the bounds so the bar can draw its self.
|
---|
122 | //
|
---|
123 | protected function resize_helper( sc:ScreenCoords ):Object {
|
---|
124 | var tmp:Object = sc.get_bar_coords(this.index, this.group);
|
---|
125 |
|
---|
126 | var bar_top:Number = sc.get_y_from_val(this.top, this.right_axis);
|
---|
127 | var bar_bottom:Number;
|
---|
128 |
|
---|
129 | if( this.bottom == Number.MIN_VALUE )
|
---|
130 | bar_bottom = sc.get_y_bottom(this.right_axis);
|
---|
131 | else
|
---|
132 | bar_bottom = sc.get_y_from_val(this.bottom, this.right_axis);
|
---|
133 |
|
---|
134 | var top:Number;
|
---|
135 | var height:Number;
|
---|
136 | var upside_down:Boolean = false;
|
---|
137 |
|
---|
138 | if( bar_bottom < bar_top ) {
|
---|
139 | top = bar_bottom;
|
---|
140 | upside_down = true;
|
---|
141 | }
|
---|
142 | else
|
---|
143 | {
|
---|
144 | top = bar_top;
|
---|
145 | }
|
---|
146 |
|
---|
147 | height = Math.abs( bar_bottom - bar_top );
|
---|
148 |
|
---|
149 |
|
---|
150 | //
|
---|
151 | // tell the tooltip where to show its self
|
---|
152 | //
|
---|
153 | this.tip_pos = new flash.geom.Point( tmp.x + (tmp.width / 2), top );
|
---|
154 |
|
---|
155 | if ( this.on_show_animate )
|
---|
156 | this.first_show(tmp.x, top, tmp.width, height);
|
---|
157 | else {
|
---|
158 | //
|
---|
159 | // move the Sprite to the correct screen location:
|
---|
160 | //
|
---|
161 | this.y = top;
|
---|
162 | this.x = tmp.x;
|
---|
163 | }
|
---|
164 |
|
---|
165 | //
|
---|
166 | // return the bounds to draw the item:
|
---|
167 | //
|
---|
168 | return { width:tmp.width, top:top, height:height, upside_down:upside_down };
|
---|
169 | }
|
---|
170 |
|
---|
171 | protected function first_show(x:Number, y:Number, width:Number, height:Number): void {
|
---|
172 |
|
---|
173 | this.on_show_animate = false;
|
---|
174 | Tweener.removeTweens(this);
|
---|
175 |
|
---|
176 | // tr.aces('base.as', this.on_show.get('type') );
|
---|
177 | var d:Number = x / this.stage.stageWidth;
|
---|
178 | d *= this.on_show.get('cascade');
|
---|
179 | d += this.on_show.get('delay');
|
---|
180 |
|
---|
181 | switch( this.on_show.get('type') ) {
|
---|
182 |
|
---|
183 | case 'pop-up':
|
---|
184 | this.x = x;
|
---|
185 | this.y = this.stage.stageHeight + this.height + 3;
|
---|
186 | Tweener.addTween(this, { y:y, time:1, delay:d, transition:Equations.easeOutBounce } );
|
---|
187 | break;
|
---|
188 |
|
---|
189 | case 'drop':
|
---|
190 | this.x = x;
|
---|
191 | this.y = -height - 10;
|
---|
192 | Tweener.addTween(this, { y:y, time:1, delay:d, transition:Equations.easeOutBounce } );
|
---|
193 | break;
|
---|
194 |
|
---|
195 | case 'fade-in':
|
---|
196 | this.x = x;
|
---|
197 | this.y = y;
|
---|
198 | this.alpha = 0;
|
---|
199 | Tweener.addTween(this, { alpha:this.mouse_out_alpha, time:1.2, delay:d, transition:Equations.easeOutQuad } );
|
---|
200 | break;
|
---|
201 |
|
---|
202 | case 'grow-down':
|
---|
203 | this.x = x;
|
---|
204 | this.y = y;
|
---|
205 | this.scaleY = 0.01;
|
---|
206 | Tweener.addTween(this, { scaleY:1, time:1.2, delay:d, transition:Equations.easeOutQuad } );
|
---|
207 | break;
|
---|
208 |
|
---|
209 | case 'grow-up':
|
---|
210 | this.x = x;
|
---|
211 | this.y = y+height;
|
---|
212 | this.scaleY = 0.01;
|
---|
213 | Tweener.addTween(this, { scaleY:1, time:1.2, delay:d, transition:Equations.easeOutQuad } );
|
---|
214 | Tweener.addTween(this, { y:y, time:1.2, delay:d, transition:Equations.easeOutQuad } );
|
---|
215 | break;
|
---|
216 |
|
---|
217 | case 'pop':
|
---|
218 | this.y = top;
|
---|
219 | this.alpha = 0.2;
|
---|
220 | Tweener.addTween(this, { alpha:this.mouse_out_alpha, time:0.7, delay:d, transition:Equations.easeOutQuad } );
|
---|
221 |
|
---|
222 | // shrink the bar to 3x3 px
|
---|
223 | this.x = x + (width/2);
|
---|
224 | this.y = y + (height/2);
|
---|
225 | this.width = 3;
|
---|
226 | this.height = 3;
|
---|
227 |
|
---|
228 | Tweener.addTween(this, { x:x, y:y, width:width, height:height, time:1.2, delay:d, transition:Equations.easeOutElastic } );
|
---|
229 | break;
|
---|
230 |
|
---|
231 | default:
|
---|
232 | this.y = y;
|
---|
233 | this.x = x;
|
---|
234 |
|
---|
235 | }
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|