## FusionCharts and JavaScript > How it Works?
FusionCharts v3 offers tremendous integration capabilities with JavaScript. It seamlessly binds with JavaScript/AJAX to let you do the following: You can register your chart with JavaScript and then listen to various events like - chart loaded, data loaded, chart rendered, error in chart etc. For a full list of events exposed by chart, please click here. You can update the chart at client side, without incurring any page refreshes, by providing it ready-made XML data from JavaScript functions. Using this method, you can integrate FusionCharts with your AJAX applications. Your AJAX wrappers can get the data from server, process it, convert it into XML and then update the charts already present on the page. Update the chart at client side by providing it direct URL to the XML data provider page. The chart would itself download the new data, parse it and then render - all without involving any page refreshes. Get the chart's XML data Get any attribute from chart XML Get the chart's data as CSV Invoke the exporting routine for chart (as image/PDF) Invoke further JavaScript methods like signature() and hasRendered() to get information about the chart Using a combination of FusionCharts and JavaScript, you can offer a seamless experience to your end users. Here, we'll discuss how to integrate both these technologies to yield the best results. Before we move to the examples, let's quickly discuss the security configuration you need to undertake to run our examples code or your own examples, when running from local file system.
Security settings pertinent to FusionCharts and JavaScript
Adobe Flash Player, by default, restricts SWF files on local filesystem to make any JavaScript class, or vice-versa. This has been done to reduce harm to your system by "evil" Flash SWFs. However, when the same SWF is routed through a server (localhost or remote), these rules do not apply as the browser then treats those SWF under a different security context, where they cannot do any harm to your local filesystem. As sush, the same rules also apply to FusionCharts when run from your file system. So, if you directly run any of our example codes (present in Download Pacakage > Code > JavaScript) from your local filesystem, you'll see that they fail silently. To make them work, you can follow either of the steps below: Move the examples folder (along with ../FusionCharts folder) to your server (either localhost or remote) and execute from there. Or, you can configure Flash Player settings to allow SWFs present in specific folders to raise JavaScript class. This can be done by going to for a specific folder, you'll need to visit http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html and add the folder containing our examples to the list of allowed folder in that interface. With this, let's move ahead and create some examples.
The first step
Before we start with any of our examples, you first need to make sure that you've the following things ready with you: Chart SWF Files - Present in Download Package > Charts FusionCharts JavaScript Class File (FusionCharts.js) - Present in Download Package > JSClass Let's now create a basic example, where we see how to register the chart with JavaScript and track the rendered event of chart. Code examples discussed in this section are present inDownload Package > Code > JavaScript > Basics folder.
Registering with JavaScript and tracking the rendered event
For this example, create a simple HTML page BasicExample.html with the following code:
<HTML> <HEAD> <TITLE>FusionCharts & JavaScript - Basic Example</TITLE> <SCRIPT LANGUAGE="Javascript" SRC="../../FusionCharts/FusionCharts.js"></SCRIPT> <SCRIPT LANGUAGE="JavaScript"> //FC_Rendered method is called whenever a FusionCharts chart on the page //has finished initial rendering. To this function, the chart passes its //own DOM Id. function FC_Rendered(DOMId){ //If it's our required chart if (DOMId=="chart1Id"){ //Simply alert window.alert("Look Ma! I am Column3D and I've finished loading and rendering."); return; } } </SCRIPT> </HEAD> <BODY> <div id="chart1div"> FusionCharts </div> <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>"); chart1.render("chart1div"); </script> </BODY> </HTML>
In the above code, we are first creating a Column3D chart using FusionCharts JavaScript class. We set the DOM Id of the chart as chart1Id. It is this ID using which the chart will now be recognized in the HTML page. You need to make sure that each chart on the page gets a different Id, else the browser would start behaving weirdly. To register the chart with JavaScript, we set the registerWithJS parameter to 1. It's the last parameter in FusionCharts constructor function. var chart1 = new FusionCharts("../../FusionCharts/Column3D.swf", "chart1Id", "400", "300", "0", "1"); Now, we've provided a basic XML data document to chart using its setDataXML method. Finally, we render the chart. If you now switch to <SCRIPTS> section of HTML <HEAD>, you'll find the following function:
<SCRIPT LANGUAGE="JavaScript"> //FC_Rendered method is called whenever a FusionCharts chart on the page //has finished initial rendering. To this function, the chart passes its //own DOM Id. function FC_Rendered(DOMId){ //If it's our required chart if (DOMId=="chart1Id"){ //Simply alert window.alert("Look Ma! I am Column3D and I've finished loading and rendering."); return; } } </SCRIPT>
The above function FC_Rendered() gets invoked whenever a FusionCharts chart (on the page) has finished rendering for the first time. To this function, the chart passes its own DOM Id, so that we can cross refer to it. In our example, when the Column 3D chart (with DOM Id chart1ID) has finished rendering, it calls this function too. In this function, we check if it was invoked by our required Column 3D chart. To do so, we match the DOM Id passed to this id with the DOM Id of our Column chart. This is useful when you've multiple charts on page and you want to track loading of each of those charts. Finally, in this function, we output a message to the user that the chart has been loaded. In your applications, you can put in application logic here (or AJAX code) to get data or build data and then update the chart, which we'll soon see. Now that you're famililar with the basic concepts of FusionCharts and JavaScript, we'll next see how to change the data of a chart at client side using setDataXML method.