Drill Down Charts > Using JavaScript Links
 

Instead of using simple links, you can also set JavaScript links using FusionCharts i.e., you can invoke a JavaScript function present in the same page (in which the chart is present) when the end viewer clicks a data element on the chart. To attain this, all you need to do is place the name of JavaScript function instead of the link URL, as under:

<set name='USA' value='235' color='F23456' link="JavaScript:myJS('USA, 235');"/>

In the above code, myJS refers to a custom JavaScript function present in the HTML page that embeds this chart. You can also specify any number of parameters for this function. When you now click the data item (column, pie, bar etc.) for this particular data, myJs function would be invoked and 'USA, 235' would be passed to the function as the function parameter.

Let's quickly see a basic example of this. We'll create a folder DrillDown > Basic_JS for this example.

 
A simple JavaScript drill-down example

We'll create a simple 3D column chart indicating "Bank Branches Per Million Inhabitants" for South Asian countries. Each column when clicked, would pass its name and value to our custom JavaScript function myJS, which (for the sake of demonstration) would just write it out in an alert box. In realistic situations, you can use this data (passed from FusionCharts to JavaScript) to manipulate more data, visual interface or anything else.

We first create the XML data document for this example - Data.xml

<graph bgcolor='fdfdfd' caption='Bank Branches Per Million Inhabitants' subCaption='Source: Swiss Re' yaxisminvalue='0' yaxisname='Branches' xaxisname='Country' numdivlines='0' decimalPrecision='0' showCanvasBg='0' showLimits='0'>
     <set name='Hong Kong' value='235' color='F23456' link="JavaScript:myJS('Hong Kong, 235');"/>
     <set name='Japan' value='123' color='33FF66' link="JavaScript:myJS('Japan, 123');"/>
     <set name='Singapore' value='129' color='FF6600' link="JavaScript:myJS('Singapore, 129');"/>
     <set name='Malaysia' value='121' color='3399FF' link="JavaScript:myJS('Malaysia, 121');"/>
     <set name='Taiwan' value='110' color='009966' link="JavaScript:myJS('Taiwan, 110');"/>
     <set name='China' value='90' color='CC3399' link="JavaScript:myJS('China, 90');"/>
     <set name='S. Korea' value='86' color='FFCC33' link="JavaScript:myJS('S. Korea, 86');"/>
</graph>

As you can see above, for each data item, we've defined a JavaScript link, that points to the custom function myJS. To this function, we're passing the name and value of the data item.

The HTML page (Chart.html) looks as under:

<HTML>
<HEAD>
<meta http-equiv=Content-Type content="text/html; charset=ISO-8859-1">
<TITLE>FusionCharts Drill-down JavaScript example</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function myJS(myVar){
   //Here, we're simply outputting the value received from FusionCharts.
   //In real world scenarious, you can use it for more useful purposes like changing data, Visual Interface etc or redirecting to a new page
   window.alert(myVar);
}
//-->
</SCRIPT>
</HEAD>
<BODY bgcolor="#FFFFFF">
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/
flash/swflash.cab#version=6,0,0,0"
WIDTH="500" HEIGHT="350" id="FusionCharts" ALIGN="">
<PARAM NAME=movie VALUE="../../Charts/FC_2_3_Column3D.swf?chartWidth=500&chartHeight=350"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#FFFFFF> <EMBED src="../../Charts/FC_2_3_Column3D.swf?chartWidth=500&chartHeight=350" quality=high bgcolor=#FFFFFF WIDTH="500" HEIGHT="350" NAME="FusionCharts" ALIGN=""
TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>
</OBJECT>
</BODY>
</HTML>

When you now view this chart in the browser and click on a particular column, you'll get the following output: