FusionCharts for Flex > Drill Down Charts > Flex Links

You can easily implement FusionCharts drilldown functionality directly from your Flex application. FusionCharts for Flex provides the following two processes through which you can imbibe drilldown functionality in charts. The two processes are:

  • Trapping FCClickEvent
  • Calling a function defined in Flex.
 
Using FCLinkClicked Event
In order to turn the data plot into a hotspot, you need to provide prefixing with S- . The click event will be trapped by event-listeners. Clicking on the data plot would trigger an event named FCClickEvent. You can put any value next to S-. This value would be passed to an event listener function as parameter. Developer can use this value to setup further actions for drilldown. For instance, you can invoke a message box containing certain value when a particular data plot is clicked, Similarly, another message box with a different value appears when another data plot is clicked.
 
XML Example:
 
<chart caption='ABC Bank Branches' subCaption='(In Asian Countries)' yaxislabel='Branches' xaxislabel='Country'>
    <set label='Hong Kong' value='235' link="S-Week 4"/>
 
Here, we pass a value "Week 4" to the event listener function.
 
Array Example:
 
[Bindable]
private var data:ArrayCollection=new ArrayCollection([
 
        {label:'Week 4',value:'48',link:'S-Week 4'}
    ]);
 
XMLList Example:
 
[Bindable]
private var chartData:XML=
     <main>
          <data>
               <data label='Jan' value='17400' link='S-Week 4' />
          </data>
     </main>
 
Model Example:
 
<mx:Model id="chartData">
        <chart>
           <data>
               <label>Jan</label>
                <value>17400</value>
               <link>S-Week 4</link>
           </data>
        </chart>
</mx:Model>
 
Please refer to Handling Events page in Flex Developers Guide > FusionCharts control section for a detailed description on implementation of FCClickEvent.
 
Using Flex Functions as Link:
You can easily invoke Flex functions through drilldowns by specifying the name of the function prefixed with E-, as the value of the link attribute. You can also pass parameters to Flex function by specifying the parameters along with the function.
 
<set ... value='235' link='E-my_func,Hong Kong,235'/>

In the above code, my_func is a function defined in a Flex project. The function is invoked when a data plot (column, pie, bar etc.) is clicked, and the values of the label and value attribute pertaining to that data plot will be passed as parameters. You can even pass identifier numbers or strings to each data as parameters to the function.

Let's make an example chart to demonstrate Flex function call thtough a link. We'll create a simple 2D Column chart to display number of branches that ABC bank has in various Asian countries. When a column is clicked a message box is displayed, showing values of label and value attributes pertaining to the clicked data plot.

Here's the code:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx"  xmlns:components="com.fusioncharts.components.*">
  <components:FusionCharts id="myChart" FCChartType="Column2D" width="500" height="300" FCDataURL="Data.xml" />
  <fx:Script>
    <![CDATA[
         import mx.controls.Alert;
         // This function will be invoked upon click in a particular data element.
         // The function name and parameters are specified on the chart data or in
         // the XML file as link attribute and starts with 'E-'
         public function my_func(linkParam:String):void
         {
           Alert.show(linkParam);
         }
    ]]>
   </fx:Script>
</s:Application>
 
Following is the XML code for the chart:
 
<chart caption='ABC Bank Branches' subCaption='(In Asian Countries)' yaxislabel='Branches' xaxislabel='Country'>
<set label='Hong Kong' value='235' link='E-my_func,Hong Kong,235'/>
<set label='Japan' value='123' link='E-my_func,Japan,123'/>
<set label='Singapore' value='129' link='E-my_func,Singapore,129'/>
<set label='Malaysia' value='121' link='E-my_func,Malayasia,121'/>
<set label='Taiwan' value='110' link='E-my_func,Taiwan, 110'/>
<set label='China' value='90' link='E-my_func,China, 90'/>
<set label='S. Korea' value='86' link='E-my_func,S. Korea, 86'/>
</chart>
 
In the code (above), we've defined a function my_func that will respond to the click event, which occurs when the data plot is clicked.

By default, FusionCharts for Flex calls the Flex functions from the Global scope. However, if you wish to define the functions inside an object, you need to specify the reference to that object through the FCFlexFunctionsObject property. Let's modify the above example to demonstrate this:
<?xml version="1.0" encoding="utf-8"?>
   <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx"
     xmlns:components="com.fusioncharts.components.*" initialize="init()">

        <components:FusionCharts id="myChart" FCChartType="Column2D" width="500" height="300" FCDataURL="Data.xml" />

   <mx:Script>
      <![CDATA[

            // Declare a myFunctions object            
            public var myFunctionsObj:myFunctions;

            public function init():void
            {
                  // instantiate myFunctionsObj                 
                  myFunctionsObj = new myFunctions();

                  // assign myFunctionsObj to chart                  
                  myChart.FCFlexFunctionsObject = myFunctionsObj;
            }
     
      ]]>
   </mx:Script>
</mx:Application>

The code of the myFunction class is:

package
{
    // This class contains the functions which will be called from 
    // the Flex function links defined in the chart data

    internal class myFunctions
    {

        import mx.controls.Alert; 

        // This function will be invoked upon click in a particular data element.
        // The function name and parameters are specified on the chart data or in 
        // the XML file as link attribute and starts with 'E-' 

        public function my_func(linkParam:String):void
        {
            Alert.show(linkParam);
        }

        public function myFunctions(){ }

    }
}
In the above code, we put my_func in a class named myFunctions and instantiated the class as an object named myFunctionsObj. We assign this object as the Flex function handler object through FCFlexFunctionsObject = myFunctionsObj. This makes the chart call the Flex function from the myFunctionsObj object when a link with E- prefix is invoked.
Let us also see how Flex Functions as Link can be defined in Array, XMLList and Model objets.
Array Example:
 
[Bindable]
private var chartData:ArrayCollection=new ArrayCollection([
                 { label:'Jan', value:'17400', link:'E-my_func,Hong Kong,235'}
             ]);
 
XMLList Example:
 
[Bindable]
private var chartData:XML=
     <main>
         <data>
            <data label='Jan' value='17400' link:'E-my_func,Hong Kong,235'/>
         </data>
    </main>;
 
Model Example:
 
<mx:Model id="chartData">
        <chart>
           <data>
               <label>Jan</label>
                <value>17400</value>
               <link>E-my_func,Hong Kong,235</link>
           </data>
        </chart>

</mx:Model>