FusionWidgets v3 offers tremendous integration capabilities with JavaScript. It seamlessly binds with JavaScript/AJAX to let you do the following:

  1. You can register your chart with JavaScript. Whenever the chart is now loaded or rendered, you can trap the events in JavaScript.
  2. 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 FusionWidgets 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.
  3. 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 FusionWidgets 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 FusionWidgets 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:

  1. Chart SWF Files - Present in Download Package > Charts
  2. 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 in Download Package > Code > JavaScript 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>FusionWidgets & JavaScript - Basic Example</TITLE>
   <SCRIPT LANGUAGE="Javascript" SRC="../Charts/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 Funnel Chart and I've finished loading and rendering.");
            return;
         }
      }
   </SCRIPT>
</HEAD>
<BODY>
<div id="chart1div">
   FusionWidgets
</div>
   <script language="JavaScript">
      var chart1 = new FusionCharts("../Charts/Funnel.swf", "chart1Id", "500", "400", "0", "1");
      chart1.setDataXML("<chart caption='Conversion Ratio' subcaption='May 2007' showPercentValues='1' decimals='1' baseFontSize='11' percentOfPrevious='1' showPlotBorder='1' funnelYScale='15' labelSepChar=': '><set label='Website Visits' value='385634' /><set label='Downloads' value='175631' /><set label='Interested to buy' value='84564' /><set label='Contract finalized' value='35654' /><set label='Purchased' value='12342' /></chart>");
      chart1.render("chart1div");
   </script>
</BODY>
</HTML>

In the above code, we are first creating a Funnel 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("../Charts/Funnel.swf", "chart1Id", "500", "400", "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 Funnel Chart and I've finished loading and rendering.");
         return;
      }
   }
</SCRIPT>

The above function FC_Rendered() gets invoked whenever a FusionWidgets 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 Funnel 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 Funnel chart. To do so, we match the DOM Id passed to this id with the DOM Id of our Funnel 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 FusionWidgets and JavaScript, we'll next see how to change the data of a chart at client side using setDataXML method.