source: code/Website/open-flash-chart/charts/series/Element.as@ 7937

Last change on this file since 7937 was 7849, checked in by dennisw, 15 years ago
File size: 4.5 KB
Line 
1package charts.series {
2
3 import charts.series.has_tooltip;
4 import flash.display.Sprite;
5 import string.Utils;
6 import global.Global;
7 import flash.events.Event;
8 import flash.events.MouseEvent;
9 import caurina.transitions.Tweener;
10 import caurina.transitions.Equations;
11 import flash.geom.Point;
12 import flash.net.URLRequest;
13 import flash.net.navigateToURL;
14 import flash.external.ExternalInterface;
15 import elements.axis.XAxisLabels;
16
17 public class Element extends Sprite implements has_tooltip {
18 //
19 // for line data
20 //
21 public var _x:Number;
22 public var _y:Number;
23
24 public var index:Number;
25 protected var tooltip:String;
26 private var link:String;
27 public var is_tip:Boolean;
28
29 public var line_mask:Sprite;
30 protected var right_axis:Boolean;
31
32
33 public function Element()
34 {
35 // elements don't change shape much, so lets
36 // cache it
37 this.cacheAsBitmap = true;
38 this.right_axis = false;
39 }
40
41 public function resize( sc:ScreenCoordsBase ):void {
42
43 var p:flash.geom.Point = sc.get_get_x_from_pos_and_y_from_val( this._x, this._y, this.right_axis );
44 this.x = p.x;
45 this.y = p.y;
46 }
47
48 //
49 // for tooltip closest - return the middle point
50 //
51 public function get_mid_point():flash.geom.Point {
52
53 //
54 // dots have x, y in the center of the dot
55 //
56 return new flash.geom.Point( this.x, this.y );
57 }
58
59 public function get_x(): Number {
60 return this._x;
61 }
62
63 /**
64 * When true, this element is displaying a tooltip
65 * and should fade-in, pulse, or become active
66 *
67 * override this to show hovered states.
68 *
69 * @param b
70 */
71 public function set_tip( b:Boolean ):void {}
72
73
74 //
75 // if this is put in the Element constructor, it is
76 // called multiple times for some reason :-(
77 //
78 protected function attach_events():void {
79
80 // weak references so the garbage collector will kill them:
81 this.addEventListener(MouseEvent.MOUSE_OVER, this.mouseOver, false, 0, true);
82 this.addEventListener(MouseEvent.MOUSE_OUT, this.mouseOut, false, 0, true);
83 }
84
85 public function mouseOver(event:Event):void {
86 this.pulse();
87 }
88
89 public function pulse():void {
90 // pulse:
91 Tweener.addTween(this, {alpha:.5, time:0.4, transition:"linear"} );
92 Tweener.addTween(this, {alpha:1, time:0.4, delay:0.4, onComplete:this.pulse, transition:"linear"});
93 }
94
95 public function mouseOut(event:Event):void {
96 // stop the pulse, then fade in
97 Tweener.removeTweens(this);
98 Tweener.addTween(this, { alpha:1, time:0.4, transition:Equations.easeOutElastic } );
99 }
100
101 public function set_on_click( s:String ):void {
102 this.link = s;
103 this.buttonMode = true;
104 this.useHandCursor = true;
105 // weak references so the garbage collector will kill it:
106 this.addEventListener(MouseEvent.MOUSE_UP, this.mouseUp, false, 0, true);
107 }
108
109 private function mouseUp(event:Event):void {
110
111 if ( this.link.substring(0, 6) == 'trace:' ) {
112 // for the test JSON files:
113 tr.ace( this.link );
114 }
115 else if ( this.link.substring(0, 5) == 'http:' )
116 this.browse_url( this.link );
117 else if ( this.link.substring(0, 6) == 'https:' )
118 this.browse_url( this.link );
119 else {
120 //
121 // TODO: fix the on click to pass out the chart id:
122 //
123 // var ex:ExternalInterfaceManager = ExternalInterfaceManager.getInstance();
124 // ex.callJavascript(this.link, this.index);
125 ExternalInterface.call( this.link, this.index );
126 }
127 }
128
129 private function browse_url( url:String ):void {
130 var req:URLRequest = new URLRequest(this.link);
131 try
132 {
133 navigateToURL(req);
134 }
135 catch (e:Error)
136 {
137 trace("Error opening link: " + this.link);
138 }
139 }
140
141 public function get_tip_pos():Object {
142 return {x:this.x, y:this.y};
143 }
144
145
146 //
147 // this may be overriden by Collection objects
148 //
149 public function get_tooltip():String {
150 return this.tooltip;
151 }
152
153 /**
154 * Replace #x_label# with the label. This is called
155 * after the X Label object has been built (see main.as)
156 *
157 * @param labels
158 */
159 public function tooltip_replace_labels( labels:XAxisLabels ):void {
160
161 tr.aces('x label', this._x, labels.get( this._x ));
162 this.tooltip = this.tooltip.replace('#x_label#', labels.get( this._x ) );
163 }
164
165 /**
166 * Mem leaks
167 */
168 public function die():void {
169
170 if ( this.line_mask != null ) {
171
172 this.line_mask.graphics.clear();
173 this.line_mask = null;
174 }
175 }
176 }
177}
Note: See TracBrowser for help on using the repository browser.