PageRenderTime 79ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/open-flash-chart/main.as

http://github.com/saberma/open-flash-chart-2
ActionScript | 907 lines | 499 code | 190 blank | 218 comment | 69 complexity | 959164d899343f7ffd25b0a4bed63edc MD5 | raw file
  1. package {
  2. import charts.series.Element;
  3. import charts.Factory;
  4. import charts.ObjectCollection;
  5. import elements.menu.Menu;
  6. import charts.series.has_tooltip;
  7. import flash.events.Event;
  8. import flash.events.MouseEvent;
  9. // for image upload:
  10. import flash.events.ProgressEvent;
  11. import flash.net.URLVariables;
  12. import flash.display.Sprite;
  13. import flash.net.URLLoader;
  14. import flash.net.URLRequest;
  15. import flash.display.StageAlign;
  16. import flash.display.StageScaleMode;
  17. import string.Utils;
  18. import global.Global;
  19. import com.serialization.json.JSON;
  20. import flash.external.ExternalInterface;
  21. import flash.ui.ContextMenu;
  22. import flash.ui.ContextMenuItem;
  23. import flash.events.IOErrorEvent;
  24. import flash.events.ContextMenuEvent;
  25. import flash.system.System;
  26. import flash.display.LoaderInfo;
  27. // export the chart as an image
  28. import com.adobe.images.PNGEncoder;
  29. import com.adobe.images.JPGEncoder;
  30. import mx.utils.Base64Encoder;
  31. // import com.dynamicflash.util.Base64;
  32. import flash.display.BitmapData;
  33. import flash.utils.ByteArray;
  34. import flash.net.URLRequestHeader;
  35. import flash.net.URLRequestMethod;
  36. import flash.net.URLLoaderDataFormat;
  37. import elements.axis.XAxis;
  38. import elements.axis.XAxisLabels;
  39. import elements.axis.YAxisBase;
  40. import elements.axis.YAxisLeft;
  41. import elements.axis.YAxisRight;
  42. import elements.axis.RadarAxis;
  43. import elements.Background;
  44. import elements.labels.XLegend;
  45. import elements.labels.Title;
  46. import elements.labels.Keys;
  47. import elements.labels.YLegendBase;
  48. import elements.labels.YLegendLeft;
  49. import elements.labels.YLegendRight;
  50. public class main extends Sprite {
  51. public var VERSION:String = "2 Kvasir";
  52. private var title:Title = null;
  53. //private var x_labels:XAxisLabels;
  54. private var x_axis:XAxis;
  55. private var radar_axis:RadarAxis;
  56. private var x_legend:XLegend;
  57. private var y_axis:YAxisBase;
  58. private var y_axis_right:YAxisBase;
  59. private var y_legend:YLegendBase;
  60. private var y_legend_2:YLegendBase;
  61. private var keys:Keys;
  62. private var obs:ObjectCollection;
  63. public var tool_tip_wrapper:String;
  64. private var sc:ScreenCoords;
  65. private var tooltip:Tooltip;
  66. private var background:Background;
  67. private var menu:Menu;
  68. private var ok:Boolean;
  69. private var URL:String; // ugh, vile. The IOError doesn't report the URL
  70. private var id:String; // chart id passed inf from user
  71. private var chart_parameters:Object;
  72. private var json:String;
  73. public function main() {
  74. this.chart_parameters = LoaderInfo(this.loaderInfo).parameters;
  75. if( this.chart_parameters['loading'] == null )
  76. this.chart_parameters['loading'] = 'Loading data...';
  77. var l:Loading = new Loading(this.chart_parameters['loading']);
  78. this.addChild( l );
  79. this.build_right_click_menu();
  80. this.ok = false;
  81. if( !this.find_data() )
  82. {
  83. // no data found -- debug mode?
  84. try {
  85. var file:String = "../../data-files/candle.txt";
  86. this.load_external_file( file );
  87. /*
  88. // test AJAX calls like this:
  89. var file:String = "../data-files/bar-2.txt";
  90. this.load_external_file( file );
  91. file = "../data-files/radar-area.txt";
  92. this.load_external_file( file );
  93. */
  94. }
  95. catch (e:Error) {
  96. this.show_error( 'Loading test data\n'+file+'\n'+e.message );
  97. }
  98. }
  99. // inform javascript that it can call our reload method
  100. this.addCallback("reload", reload); // mf 18nov08, line 110 of original 'main.as'
  101. // inform javascript that it can call our load method
  102. this.addCallback("load", load);
  103. // inform javascript that it can call our post_image method
  104. this.addCallback("post_image", post_image);
  105. //
  106. this.addCallback("get_img_binary", getImgBinary);
  107. // more interface
  108. this.addCallback("get_version", getVersion);
  109. // TODO: chanf all external to use this:
  110. //
  111. // tell our external interface manager to pass out the chart ID
  112. // with every external call.
  113. //
  114. if ( this.chart_parameters['id'] )
  115. {
  116. var ex:ExternalInterfaceManager = ExternalInterfaceManager.getInstance();
  117. ex.setUp(this.chart_parameters['id']);
  118. }
  119. // tell the web page that we are ready
  120. if( this.chart_parameters['id'] )
  121. this.callExternalCallback("ofc_ready", this.chart_parameters['id']);
  122. else
  123. this.callExternalCallback("ofc_ready");
  124. this.set_the_stage();
  125. }
  126. private function addCallback(functionName:String, closure:Function): void {
  127. // the debug player does not have an external interface
  128. // because it is NOT embedded in a browser
  129. if (ExternalInterface.available)
  130. ExternalInterface.addCallback(functionName, closure);
  131. }
  132. private function callExternalCallback(functionName:String, ... optionalArgs ): * {
  133. // the debug player does not have an external interface
  134. // because it is NOT embedded in a browser
  135. if (ExternalInterface.available)
  136. return ExternalInterface.call(functionName, optionalArgs);
  137. }
  138. public function getVersion():String {return VERSION;}
  139. // public function getImgBinary():String { return Base64.encodeByteArray(image_binary()); }
  140. public function getImgBinary():String {
  141. tr.ace('Saving image :: image_binary()');
  142. var bmp:BitmapData = new BitmapData(this.stage.stageWidth, this.stage.stageHeight);
  143. bmp.draw(this);
  144. var b64:Base64Encoder = new Base64Encoder();
  145. var b:ByteArray = PNGEncoder.encode(bmp);
  146. // var encoder:JPGEncoder = new JPGEncoder(80);
  147. // var q:ByteArray = encoder.encode(bmp);
  148. // b64.encodeBytes(q);
  149. //
  150. //
  151. //
  152. b64.encodeBytes(b);
  153. return b64.toString();
  154. //
  155. // commented out by J vander? why?
  156. // return b64.flush();
  157. //
  158. //
  159. /*
  160. var b64:Base64Encoder = new Base64Encoder();
  161. b64.encodeBytes(image_binary());
  162. tr.ace( b64 as String );
  163. return b64 as String;
  164. */
  165. }
  166. /**
  167. * Called from the context menu:
  168. */
  169. public function saveImage(e:ContextMenuEvent):void {
  170. // ExternalInterface.call("save_image", this.chart_parameters['id']);// , getImgBinary());
  171. // ExternalInterface.call("save_image", getImgBinary());
  172. // this just calls the javascript function which will grab an image from use
  173. // an do something with it.
  174. this.callExternalCallback("save_image", this.chart_parameters['id']);
  175. }
  176. private function image_binary() : ByteArray {
  177. tr.ace('Saving image :: image_binary()');
  178. var pngSource:BitmapData = new BitmapData(this.width, this.height);
  179. pngSource.draw(this);
  180. return PNGEncoder.encode(pngSource);
  181. }
  182. //
  183. // External interface called by Javascript to
  184. // save the flash as an image, then POST it to a URL
  185. //
  186. //public function post_image(url:String, post_params:Object, callback:String, debug:Boolean):void {
  187. public function post_image(url:String, callback:String, debug:Boolean):void {
  188. var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
  189. //Make sure to use the correct path to jpg_encoder_download.php
  190. var request:URLRequest = new URLRequest(url);
  191. request.requestHeaders.push(header);
  192. request.method = URLRequestMethod.POST;
  193. //
  194. request.data = image_binary();
  195. var loader:URLLoader = new URLLoader();
  196. loader.dataFormat = URLLoaderDataFormat.VARIABLES;
  197. /*
  198. * i can't figure out how to make these work
  199. *
  200. var urlVars:URLVariables = new URLVariables();
  201. for (var key:String in post_params) {
  202. urlVars[key] = post_params[key];
  203. }
  204. */
  205. // base64:
  206. // urlVars.b64_image_data = getImgBinary();
  207. // RAW:
  208. // urlVars.b64_image_data = image_binary();
  209. // request.data = urlVars;
  210. var id:String = '';
  211. if ( this.chart_parameters['id'] )
  212. id = this.chart_parameters['id'];
  213. if( debug )
  214. {
  215. // debug the PHP:
  216. flash.net.navigateToURL(request, "_blank");
  217. }
  218. else
  219. {
  220. //we have to use the PROGRESS event instead of the COMPLETE event due to a bug in flash
  221. loader.addEventListener(ProgressEvent.PROGRESS, function (e:ProgressEvent):void {
  222. tr.ace("progress:" + e.bytesLoaded + ", total: " + e.bytesTotal);
  223. if ((e.bytesLoaded == e.bytesTotal) && (callback != null)) {
  224. tr.aces('Calling: ', callback + '(' + id + ')');
  225. this.call(callback, id);
  226. }
  227. });
  228. try {
  229. loader.load( request );
  230. } catch (error:Error) {
  231. tr.ace("unable to load:" + error);
  232. }
  233. /*
  234. var loader:URLLoader = new URLLoader();
  235. loader.dataFormat = URLLoaderDataFormat.BINARY;
  236. loader.addEventListener(Event.COMPLETE, function(e:Event):void {
  237. tr.ace('Saved image to:');
  238. tr.ace( url );
  239. //
  240. // when the upload has finished call the user
  241. // defined javascript function/method
  242. //
  243. ExternalInterface.call(callback);
  244. });
  245. loader.load( jpgURLRequest );
  246. */
  247. }
  248. }
  249. private function onContextMenuHandler(event:ContextMenuEvent):void
  250. {
  251. }
  252. //
  253. // try to find some data to load,
  254. // check the URL for a file name,
  255. //
  256. //
  257. public function find_data(): Boolean {
  258. // var all:String = ExternalInterface.call("window.location.href.toString");
  259. var vars:String = this.callExternalCallback("window.location.search.substring", 1);
  260. tr.aces( 1, vars );
  261. if( vars != null )
  262. {
  263. var p:Array = vars.split( '&' );
  264. for each ( var v:String in p )
  265. {
  266. if( v.indexOf( 'ofc=' ) > -1 )
  267. {
  268. var tmp:Array = v.split('=');
  269. tr.ace( 'Found external file:' + tmp[1] );
  270. this.load_external_file( tmp[1] );
  271. //
  272. // LOOK:
  273. //
  274. return true;
  275. }
  276. }
  277. }
  278. if( this.chart_parameters['data-file'] )
  279. {
  280. tr.ace( 'Found parameter:' + this.chart_parameters['data-file'] );
  281. this.load_external_file( this.chart_parameters['data-file'] );
  282. //
  283. // LOOK:
  284. //
  285. return true;
  286. }
  287. var get_data:String = 'open_flash_chart_data';
  288. if( this.chart_parameters['get-data'] )
  289. get_data = this.chart_parameters['get-data'];
  290. var json_string:*;
  291. if( this.chart_parameters['id'] )
  292. json_string = this.callExternalCallback( get_data , this.chart_parameters['id']);
  293. else
  294. json_string = this.callExternalCallback( get_data );
  295. if( json_string != null )
  296. {
  297. if( json_string is String )
  298. {
  299. this.parse_json( json_string );
  300. //
  301. // We have loaded the data, so this.ok = true
  302. //
  303. this.ok = true;
  304. //
  305. // LOOK:
  306. //
  307. return true;
  308. }
  309. }
  310. return false;
  311. }
  312. //
  313. // an external interface, used by javascript to
  314. // reload JSON from a URL :: mf 18nov08
  315. //
  316. public function reload( url:String ):void {
  317. var l:Loading = new Loading(this.chart_parameters['loading']);
  318. this.addChild( l );
  319. this.load_external_file( url );
  320. }
  321. private function load_external_file( file:String ):void {
  322. this.URL = file;
  323. //
  324. // LOAD THE DATA
  325. //
  326. var loader:URLLoader = new URLLoader();
  327. loader.addEventListener( IOErrorEvent.IO_ERROR, this.ioError );
  328. loader.addEventListener( Event.COMPLETE, xmlLoaded );
  329. var request:URLRequest = new URLRequest(file);
  330. loader.load(request);
  331. }
  332. private function ioError( e:IOErrorEvent ):void {
  333. // remove the 'loading data...' msg:
  334. this.removeChildAt(0);
  335. var msg:ErrorMsg = new ErrorMsg( 'Open Flash Chart\nIO ERROR\nLoading test data\n' + e.text );
  336. msg.add_html( 'This is the URL that I tried to open:<br><a href="'+this.URL+'">'+this.URL+'</a>' );
  337. this.addChild( msg );
  338. }
  339. private function show_error( msg:String ):void {
  340. // remove the 'loading data...' msg:
  341. this.removeChildAt(0);
  342. var m:ErrorMsg = new ErrorMsg( msg );
  343. //m.add_html( 'Click here to open your JSON file: <a href="http://a.com">asd</a>' );
  344. this.addChild(m);
  345. }
  346. public function get_x_legend() : XLegend {
  347. return this.x_legend;
  348. }
  349. private function set_the_stage():void {
  350. // tell flash to align top left, and not to scale
  351. // anything (we do that in the code)
  352. this.stage.align = StageAlign.TOP_LEFT;
  353. //
  354. // ----- RESIZE ----
  355. //
  356. // noScale: now we can pick up resize events
  357. this.stage.scaleMode = StageScaleMode.NO_SCALE;
  358. this.stage.addEventListener(Event.ACTIVATE, this.activateHandler);
  359. this.stage.addEventListener(Event.RESIZE, this.resizeHandler);
  360. this.stage.addEventListener(Event.MOUSE_LEAVE, this.mouseOut);
  361. this.addEventListener( MouseEvent.MOUSE_OVER, this.mouseMove );
  362. }
  363. private function mouseMove( event:Event ):void {
  364. // tr.ace( 'over ' + event.target );
  365. // tr.ace('move ' + Math.random().toString());
  366. // tr.ace( this.tooltip.get_tip_style() );
  367. if ( !this.tooltip )
  368. return; // <- an error and the JSON was not loaded
  369. switch( this.tooltip.get_tip_style() ) {
  370. case Tooltip.CLOSEST:
  371. this.mouse_move_closest( event );
  372. break;
  373. case Tooltip.PROXIMITY:
  374. this.mouse_move_proximity( event as MouseEvent );
  375. break;
  376. case Tooltip.NORMAL:
  377. this.mouse_move_follow( event as MouseEvent );
  378. break;
  379. }
  380. }
  381. private function mouse_move_follow( event:MouseEvent ):void {
  382. // tr.ace( event.currentTarget );
  383. // tr.ace( event.target );
  384. if ( event.target is has_tooltip )
  385. this.tooltip.draw( event.target as has_tooltip );
  386. else
  387. this.tooltip.hide();
  388. }
  389. private function mouse_move_proximity( event:MouseEvent ):void {
  390. //tr.ace( event.currentTarget );
  391. //tr.ace( event.target );
  392. var elements:Array = this.obs.mouse_move_proximity( this.mouseX, this.mouseY );
  393. this.tooltip.closest( elements );
  394. }
  395. private function mouse_move_closest( event:Event ):void {
  396. var elements:Array = this.obs.closest_2( this.mouseX, this.mouseY );
  397. this.tooltip.closest( elements );
  398. }
  399. private function activateHandler(event:Event):void {
  400. tr.aces("activateHandler:", event);
  401. tr.aces("stage", this.stage);
  402. }
  403. private function resizeHandler(event:Event):void {
  404. // tr.ace("resizeHandler: " + event);
  405. this.resize();
  406. }
  407. //
  408. // pie charts are simpler to resize, they don't
  409. // have all the extras (X,Y axis, legends etc..)
  410. //
  411. private function resize_pie(): ScreenCoordsBase {
  412. // should this be here?
  413. this.addEventListener(MouseEvent.MOUSE_MOVE, this.mouseMove);
  414. this.background.resize();
  415. this.title.resize();
  416. // this object is used in the mouseMove method
  417. this.sc = new ScreenCoords(
  418. this.title.get_height(), 0, this.stage.stageWidth, this.stage.stageHeight,
  419. null, null, null, 0, 0, false );
  420. this.obs.resize( sc );
  421. return sc;
  422. }
  423. //
  424. //
  425. private function resize_radar(): ScreenCoordsBase {
  426. this.addEventListener(MouseEvent.MOUSE_MOVE, this.mouseMove);
  427. this.background.resize();
  428. this.title.resize();
  429. this.keys.resize( 0, this.title.get_height() );
  430. var top:Number = this.title.get_height() + this.keys.get_height();
  431. // this object is used in the mouseMove method
  432. var sc:ScreenCoordsRadar = new ScreenCoordsRadar(top, 0, this.stage.stageWidth, this.stage.stageHeight);
  433. sc.set_range( this.radar_axis.get_range() );
  434. // 0-4 = 5 spokes
  435. sc.set_angles( this.obs.get_max_x()-this.obs.get_min_x()+1 );
  436. // resize the axis first because they may
  437. // change the radius (to fit the labels on screen)
  438. this.radar_axis.resize( sc );
  439. this.obs.resize( sc );
  440. return sc;
  441. }
  442. private function resize():void {
  443. //
  444. // the chart is async, so we may get this
  445. // event before the chart has loaded, or has
  446. // partly loaded
  447. //
  448. if ( !this.ok )
  449. return; // <-- something is wrong
  450. var sc:ScreenCoordsBase;
  451. if ( this.radar_axis != null )
  452. sc = this.resize_radar();
  453. else if ( this.obs.has_pie() )
  454. sc = this.resize_pie();
  455. else
  456. sc = this.resize_chart();
  457. if( this.menu )
  458. this.menu.resize();
  459. // tell the web page that we have resized our content
  460. if( this.chart_parameters['id'] )
  461. this.callExternalCallback("ofc_resize", sc.left, sc.width, sc.top, sc.height, this.chart_parameters['id']);
  462. else
  463. this.callExternalCallback("ofc_resize", sc.left, sc.width, sc.top, sc.height);
  464. sc = null;
  465. }
  466. private function resize_chart(): ScreenCoordsBase {
  467. //
  468. // we want to show the tooltip closest to
  469. // items near the mouse, so hook into the
  470. // mouse move event:
  471. //
  472. this.addEventListener(MouseEvent.MOUSE_MOVE, this.mouseMove);
  473. // FlashConnect.trace("stageWidth: " + stage.stageWidth + " stageHeight: " + stage.stageHeight);
  474. this.background.resize();
  475. this.title.resize();
  476. var left:Number = this.y_legend.get_width() /*+ this.y_labels.get_width()*/ + this.y_axis.get_width();
  477. this.keys.resize( left, this.title.get_height() );
  478. var top:Number = this.title.get_height() + this.keys.get_height();
  479. var bottom:Number = this.stage.stageHeight;
  480. bottom -= (this.x_legend.get_height() + this.x_axis.get_height());
  481. var right:Number = this.stage.stageWidth;
  482. right -= this.y_legend_2.get_width();
  483. //right -= this.y_labels_right.get_width();
  484. right -= this.y_axis_right.get_width();
  485. // this object is used in the mouseMove method
  486. this.sc = new ScreenCoords(
  487. top, left, right, bottom,
  488. this.y_axis.get_range(),
  489. this.y_axis_right.get_range(),
  490. this.x_axis.get_range(),
  491. this.x_axis.first_label_width(),
  492. this.x_axis.last_label_width(),
  493. false );
  494. this.sc.set_bar_groups(this.obs.groups);
  495. this.x_axis.resize( sc,
  496. // can we remove this:
  497. this.stage.stageHeight-(this.x_legend.get_height()+this.x_axis.labels.get_height()) // <-- up from the bottom
  498. );
  499. this.y_axis.resize( this.y_legend.get_width(), sc );
  500. this.y_axis_right.resize( 0, sc );
  501. this.x_legend.resize( sc );
  502. this.y_legend.resize();
  503. this.y_legend_2.resize();
  504. this.obs.resize( sc );
  505. // Test code:
  506. this.dispatchEvent(new Event("on-show"));
  507. return sc;
  508. }
  509. private function mouseOut(event:Event):void {
  510. if( this.tooltip != null )
  511. this.tooltip.hide();
  512. if( this.obs != null )
  513. this.obs.mouse_out();
  514. }
  515. //
  516. // an external interface, used by javascript to
  517. // pass in a JSON string
  518. //
  519. public function load( s:String ):void {
  520. this.parse_json( s );
  521. }
  522. //
  523. // JSON is loaded from an external URL
  524. //
  525. private function xmlLoaded(event:Event):void {
  526. var loader:URLLoader = URLLoader(event.target);
  527. this.parse_json( loader.data );
  528. }
  529. //
  530. // we have data! parse it and make the chart
  531. //
  532. private function parse_json( json_string:String ):void {
  533. // tr.ace(json_string);
  534. var ok:Boolean = false;
  535. try {
  536. var json:Object = JSON.deserialize( json_string );
  537. ok = true;
  538. }
  539. catch (e:Error) {
  540. // remove the 'loading data...' msg:
  541. this.removeChildAt(0);
  542. this.addChild( new JsonErrorMsg( json_string as String, e ) );
  543. }
  544. //
  545. // don't catch these errors:
  546. //
  547. if( ok )
  548. {
  549. // remove 'loading data...' msg:
  550. this.removeChildAt(0);
  551. this.build_chart( json );
  552. // force this to be garbage collected
  553. json = null;
  554. }
  555. json_string = '';
  556. }
  557. private function build_chart( json:Object ):void {
  558. tr.ace('----');
  559. tr.ace(JSON.serialize(json));
  560. tr.ace('----');
  561. if ( this.obs != null )
  562. this.die();
  563. // init singletons:
  564. NumberFormat.getInstance( json );
  565. NumberFormat.getInstanceY2( json );
  566. this.tooltip = new Tooltip( json.tooltip )
  567. var g:Global = Global.getInstance();
  568. g.set_tooltip_string( this.tooltip.tip_text );
  569. //
  570. // these are common to both X Y charts and PIE charts:
  571. this.background = new Background( json );
  572. this.title = new Title( json.title );
  573. //
  574. this.addChild( this.background );
  575. //
  576. if ( JsonInspector.is_radar( json ) ) {
  577. this.obs = Factory.MakeChart( json );
  578. this.radar_axis = new RadarAxis( json.radar_axis );
  579. this.keys = new Keys( this.obs );
  580. this.addChild( this.radar_axis );
  581. this.addChild( this.keys );
  582. }
  583. else if ( !JsonInspector.has_pie_chart( json ) )
  584. {
  585. this.build_chart_background( json );
  586. }
  587. else
  588. {
  589. // this is a PIE chart
  590. this.obs = Factory.MakeChart( json );
  591. // PIE charts default to FOLLOW tooltips
  592. this.tooltip.set_tip_style( Tooltip.NORMAL );
  593. }
  594. // these are added in the Flash Z Axis order
  595. this.addChild( this.title );
  596. for each( var set:Sprite in this.obs.sets )
  597. this.addChild( set );
  598. this.addChild( this.tooltip );
  599. if (json['menu'] != null) {
  600. this.menu = new Menu('99', json['menu']);
  601. this.addChild(this.menu);
  602. }
  603. this.ok = true;
  604. this.resize();
  605. }
  606. //
  607. // PIE charts don't have this.
  608. // build grid, axis, legends and key
  609. //
  610. private function build_chart_background( json:Object ):void {
  611. //
  612. // This reads all the 'elements' of the chart
  613. // e.g. bars and lines, then creates them as sprites
  614. //
  615. this.obs = Factory.MakeChart( json );
  616. //
  617. this.x_legend = new XLegend( json.x_legend );
  618. this.y_legend = new YLegendLeft( json );
  619. this.y_legend_2 = new YLegendRight( json );
  620. this.x_axis = new XAxis( json, this.obs.get_min_x(), this.obs.get_max_x() );
  621. this.y_axis = new YAxisLeft();
  622. this.y_axis_right = new YAxisRight();
  623. // access all our globals through this:
  624. var g:Global = Global.getInstance();
  625. // this is needed by all the elements tooltip
  626. g.x_labels = this.x_axis.labels;
  627. g.x_legend = this.x_legend;
  628. //
  629. // pick up X Axis labels for the tooltips
  630. //
  631. this.obs.tooltip_replace_labels( this.x_axis.labels );
  632. //
  633. //
  634. //
  635. this.keys = new Keys( this.obs );
  636. this.addChild( this.x_legend );
  637. this.addChild( this.y_legend );
  638. this.addChild( this.y_legend_2 );
  639. this.addChild( this.y_axis );
  640. this.addChild( this.y_axis_right );
  641. this.addChild( this.x_axis );
  642. this.addChild( this.keys );
  643. // now these children have access to the stage,
  644. // tell them to init
  645. this.y_axis.init(json);
  646. this.y_axis_right.init(json);
  647. }
  648. /**
  649. * Remove all our referenced objects
  650. */
  651. private function die():void {
  652. this.obs.die();
  653. this.obs = null;
  654. if ( this.tooltip != null ) this.tooltip.die();
  655. if ( this.x_legend != null ) this.x_legend.die();
  656. if ( this.y_legend != null ) this.y_legend.die();
  657. if ( this.y_legend_2 != null ) this.y_legend_2.die();
  658. if ( this.y_axis != null ) this.y_axis.die();
  659. if ( this.y_axis_right != null ) this.y_axis_right.die();
  660. if ( this.x_axis != null ) this.x_axis.die();
  661. if ( this.keys != null ) this.keys.die();
  662. if ( this.title != null ) this.title.die();
  663. if ( this.radar_axis != null ) this.radar_axis.die();
  664. if ( this.background != null ) this.background.die();
  665. this.tooltip = null;
  666. this.x_legend = null;
  667. this.y_legend = null;
  668. this.y_legend_2 = null;
  669. this.y_axis = null;
  670. this.y_axis_right = null;
  671. this.x_axis = null;
  672. this.keys = null;
  673. this.title = null;
  674. this.radar_axis = null;
  675. this.background = null;
  676. while ( this.numChildren > 0 )
  677. this.removeChildAt(0);
  678. if ( this.hasEventListener(MouseEvent.MOUSE_MOVE))
  679. this.removeEventListener(MouseEvent.MOUSE_MOVE, this.mouseMove);
  680. // do not force a garbage collection, it is not supported:
  681. // http://stackoverflow.com/questions/192373/force-garbage-collection-in-as3
  682. }
  683. private function build_right_click_menu(): void {
  684. var cm:ContextMenu = new ContextMenu();
  685. cm.addEventListener(ContextMenuEvent.MENU_SELECT, onContextMenuHandler);
  686. cm.hideBuiltInItems();
  687. // OFC CREDITS
  688. var fs:ContextMenuItem = new ContextMenuItem("Charts by Open Flash Chart [Version "+VERSION+"]" );
  689. fs.addEventListener(
  690. ContextMenuEvent.MENU_ITEM_SELECT,
  691. function doSomething(e:ContextMenuEvent):void {
  692. var url:String = "http://teethgrinder.co.uk/open-flash-chart-2/";
  693. var request:URLRequest = new URLRequest(url);
  694. flash.net.navigateToURL(request, '_blank');
  695. });
  696. cm.customItems.push( fs );
  697. var save_image_message:String = ( this.chart_parameters['save_image_message'] ) ? this.chart_parameters['save_image_message'] : 'Save Image Locally';
  698. var dl:ContextMenuItem = new ContextMenuItem(save_image_message);
  699. dl.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, this.saveImage);
  700. cm.customItems.push( dl );
  701. this.contextMenu = cm;
  702. }
  703. public function format_y_axis_label( val:Number ): String {
  704. // if( this._y_format != undefined )
  705. // {
  706. // var tmp:String = _root._y_format.replace('#val#',_root.format(val));
  707. // tmp = tmp.replace('#val:time#',_root.formatTime(val));
  708. // tmp = tmp.replace('#val:none#',String(val));
  709. // tmp = tmp.replace('#val:number#', NumberUtils.formatNumber (Number(val)));
  710. // return tmp;
  711. // }
  712. // else
  713. return NumberUtils.format(val,2,true,true,false);
  714. }
  715. }
  716. }