## 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. Whenever the chart is now loaded or rendered, you can trap the events in JavaScript. 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. 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. This section assumes that you already know how FusionCharts works and the XML data structure it accepts. If you do not already know this, please read the relevant sections of documentation, as examples in this section are based on concepts explained in those sections.
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.