Search Flex Components Free

Custom Search

December 27, 2007

load a swf from outside and call its function

Lets suppose you have a swf(Column3D.swf) and you don't have code(FLA) for it but you need to call its function(setDataXML) from Flex and need to send parameters.

How it can be done through JAVASCRIPT

<TITLE>FusionCharts & JavaScript - Updating chart using setDataXML() Method</TITLE>
<SCRIPT LANGUAGE=”Javascript” src=”../../FusionCharts/FusionCharts.js” mce_src=”../../FusionCharts/FusionCharts.js”></SCRIPT>
//updateChart method is called whenever the user clicks the button to
//update the chart. Here, we get a reference to the chart and update it's
//data using setDataXML method.
function updateChart(DOMId){
//Get reference to chart object using Dom ID
var chartObj = getChartFromId(”chart1Id”);
//Update it's XML
chartObj. setDataXML (”<chart><set label='B' value='12′ /><set label='C' value='10′ /><set label='D' value='18′ /><set label='E' value='21′ /></chart>”);
//Disable the button
this.document.frmUpdate.btnUpdate.disabled = true;
<div id=”chart1div”>
<script language=”JavaScript”>
var chart1 = new FusionCharts(”../../FusionCharts/Column3D.swf”, “chart1Id”, “400″, “300″, “0″, “1″);
chart1.setDataXML(”<chart><set label='A' value='10′ /><set label='B' value='11′ /></chart>”);
<form name='frmUpdate'>
<input type='button' value='Change Data' onClick='javaScript:updateChart();' name='btnUpdate'>

create a new flash file (CallFlash.swf) and a movieclip on stage and give an instance name(mc) to it.Write actions on the 1st frame of an application:

function interFunc(){
clearInterval(_root.interid); setDataXML (_root.param2);

var receiving_lc:LocalConnection = new LocalConnection();
receiving_lc.methodToExecute = function(param1:String,param2:String) {
//please use getBytesLoaded, getTotalBytes instead of using setInterval function
_root.param2 = param2;
_root.interid = setInterval(interFunc,1000);

In the Flex application:

Add Column3D.swf and CallFlash.swf

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=” ” layout=”absolute”>
public var myXML:String=”<chart><set label='A' value='10′ /><set label='B' value='11′ /></chart>”;
private function initApp():void{
var sending_lc:LocalConnection = new LocalConnection();
sending_lc.send(”lc_name”, “methodToExecute”,”Column3D.swf”,myXML);
<mx:SWFLoader source=”CallFlash.swf” creationComplete=”initApp()” />

Run the Flex application, it will show the Column3D.swf data according to the parameter you are sending.

Related Flex Tutorials