Chart out of SharePoint data without Stacked

Step 1: Create a sharepoint list with the following data in it.

Step 2: Below code creates the bar chart

<?xml version=”1.0″ encoding=”utf-8″?><mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”
layout=”absolute” creationComplete=”onCreate(event)”><mx:Script><![CDATA[

import mx.charts.series.*;
import mx.controls.Alert;
import mx.collections.ArrayCollection;
import mx.binding.utils.BindingUtils;
import mx.charts.*;
import mx.core.UIComponent;

private var MainXMLList:XMLList;
private var Project:Array =new Array();
private var Status:Array =new Array();
private var BU:Array =new Array();
private var MainXML:URLLoader;

[Bindable]
public var HoldStatus:ArrayCollection=new ArrayCollection();

private function onCreate(e:Event):void
{
MainXML=new URLLoader(new URLRequest(“http://teams4.sharepoint.hp.com/teams/ProjSpace/_vti_bin/owssvr.dll?Cmd=Display&List={56FD41DB-4D68-43EF-AF9F-3A76CBC0A8CC}&XMLDATA=TRUE”))
MainXML.addEventListener(Event.COMPLETE,MainXMLL)
}

private function MainXMLL(e:Event):void
{
var myXML:XML = XML(e.target.data);
var XMLL:XMLList=new XMLList(e.target.data)
var rsNS:Namespace = XMLL.namespace(“rs”);
var zNS:Namespace = XMLL.namespace(“z”);
var sNS:Namespace = XMLL.namespace(“s”);
MainXMLList=new XMLList(XMLL.rsNS::data.zNS::row)
HoldStatus.source=[]
for(var i:int=0;i<MainXMLList.length();i++)
{
BU.push(MainXMLList[i].attribute(“ows_Project_x0020_BU”))
Status.push(MainXMLList[i].attribute(“ows_Status”))
}
BU=removeDuplicates(BU)
Status=removeDuplicates(Status)
for(var b:int=0;b<BU.length;b++)
{
HoldStatus.source.push({Dummy:”April”})  // Just pushing dummy value to create an empty index
HoldStatus[b][“Title”]=BU[b]  // Creating a Title Idenfier in Arraycollection which stores BU1 and BU2
for(var s:int=0;s<Status.length;s++)
HoldStatus[b][Status[s]]=MainXMLList.(attribute(“ows_Project_x0020_BU”)== BU[b] && attribute(“ows_Status”) == Status[s] ).length()  //If Project BU =”BU1″ and Status =”Pipleline” Takes the count.
}

var myChart:BarChart = new BarChart(); // Create barchart dynamically
myChart.dataProvider = HoldStatus; //Assigning the HoldStatus (Arraycollection as a dataprovider for BarChart

// Setting up the Vertical Axis
var vAxis:CategoryAxis = new CategoryAxis();
vAxis.categoryField = “Title”;
myChart.verticalAxis = vAxis;
var hh:LinearAxis=new LinearAxis()
hh.interval=1
myChart.horizontalAxis=hh

 //Creating barseries (Pipeline, Closed, Complete) where Status array

holds “Pipeline, Closed and Complete”
for(var c:int=0;c<Status.length;c++)
{
var BS:BarSeries = new BarSeries();
BS.xField = Status[c];
BS.displayName = Status[c];
myChart.series.push(BS) //Pushes all bar series into Chart
}

myChart.percentWidth=95;
myChart.percentHeight=80;
var l:Legend = new Legend();
l.dataProvider = myChart;
l.direction=”horizontal”
l.percentWidth=95;
l.percentHeight=20;

myPanel.addChild(myChart);
myPanel.addChild(l);
}
private function removeDuplicates(arr:Array):Array  // This function retrieves the unique values from the Array
{
var currentValue:String = “”;
var tempArray:Array = new Array();
arr.sort(Array.CASEINSENSITIVE);
arr.forEach(
function(item:*, index:uint, array:Array):void {
if (currentValue != item) {
tempArray.push(item);
currentValue= item;
}
}
);

return tempArray;

}

]]>

</mx:Script>

<mx:Panel id=”myPanel” width=”300″ height=”300″>

</mx:Panel>

</mx:Application>

 

 

This will collect all BU Values and Status
for(var i:int=0;i<MainXMLList.length();i++)
{
BU.push(MainXMLList[i].attribute(“ows_Project_x0020_BU”))
Status.push(MainXMLList[i].attribute(“ows_Status”))
}Below code retrieves unique values and stores back into the same
array variable
BU=removeDuplicates(BU)Status=removeDuplicates(Status)Below code construct a loop for BUs and inner loop for status to
get the count of projects

for(var b:int=0;b<BU.length;b++)
{
HoldStatus.source.push({Dummy:”April”})
HoldStatus[b][“Title”]=BU[b]
for(var s:int=0;s<Status.length;s++)
HoldStatus[b][Status[s]]=MainXMLList.(attribute(“ows_Project_x0020_BU”)== BU[b] && attribute(“ows_Status”) == Status[s] ).length()
}

This will create an array collection like below 

{Title :BU1 , Pipeline: 2, Closed: 2}
{Title :BU2 , Pipeline: 1, Closed: 2}

Output

Chart out of SharePoint data

Step 1: Create a sharepoint list with the following data in it.

Step 2: Below code creates the bar chart

<?xml version=”1.0″ encoding=”utf-8″?><mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”
layout=”absolute” creationComplete=”onCreate(event)”>

<mx:Script>

<![CDATA[

import mx.charts.series.*;
import mx.controls.Alert;
import mx.collections.ArrayCollection;
import mx.binding.utils.BindingUtils;
import mx.charts.*;
import mx.core.UIComponent;

private var MainXMLList:XMLList;
private var Project:Array =new Array();
private var Status:Array =new Array();
private var BU:Array =new Array();
private var MainXML:URLLoader;

[Bindable]
public var HoldStatus:ArrayCollection=new ArrayCollection();

private function onCreate(e:Event):void
{
MainXML=new URLLoader(new URLRequest(“http://teams4.sharepoint.hp.com/teams/ProjSpace/_vti_bin/owssvr.dll?Cmd=Display&List={56FD41DB-4D68-43EF-AF9F-3A76CBC0A8CC}&XMLDATA=TRUE”))
MainXML.addEventListener(Event.COMPLETE,MainXMLL)
}

private function MainXMLL(e:Event):void
{
var myXML:XML = XML(e.target.data);
var XMLL:XMLList=new XMLList(e.target.data)
var rsNS:Namespace = XMLL.namespace(“rs”);
var zNS:Namespace = XMLL.namespace(“z”);
var sNS:Namespace = XMLL.namespace(“s”);
MainXMLList=new XMLList(XMLL.rsNS::data.zNS::row)
HoldStatus.source=[]
for(var i:int=0;i<MainXMLList.length();i++)
{
BU.push(MainXMLList[i].attribute(“ows_Project_x0020_BU”))
Status.push(MainXMLList[i].attribute(“ows_Status”))
}
BU=removeDuplicates(BU)
Status=removeDuplicates(Status)
for(var b:int=0;b<BU.length;b++)
{
HoldStatus.source.push({Dummy:”April”})  // Just pushing dummy value to create an empty index
HoldStatus[b][“Title”]=BU[b]  // Creating a Title Idenfier in Arraycollection which stores BU1 and BU2
for(var s:int=0;s<Status.length;s++)
HoldStatus[b][Status[s]]=MainXMLList.(attribute(“ows_Project_x0020_BU”)== BU[b] && attribute(“ows_Status”) == Status[s] ).length()  //If Project BU =”BU1″ and Status =”Pipleline” Takes the count.
}

var myChart:BarChart = new BarChart(); // Create barchart dynamically
myChart.dataProvider = HoldStatus; //Assigning the HoldStatus (Arraycollection as a dataprovider for BarChart

// Setting up the Vertical Axis
var vAxis:CategoryAxis = new CategoryAxis();
vAxis.categoryField = “Title”;
myChart.verticalAxis = vAxis;
var hh:LinearAxis=new LinearAxis()
hh.interval=1
myChart.horizontalAxis=hh

 // Setting up the chart type as stacked bar
var innerSet:BarSet = new BarSet();
innerSet.type = “stacked”;

 //Creating barseries (Pipeline, Closed, Complete) where Status array
holds “Pipeline, Closed and Complete”
for(var c:int=0;c<Status.length;c++)
{
var BS:BarSeries = new BarSeries();
BS.xField = Status[c];
BS.displayName = Status[c];
innerSet.series.push(BS) // innerSet is the barSet where all Bar Series
into the InnerSet, It groups the series into stacked show
}

myChart.series=[innerSet] // Assigning the Barset to the chart
myChart.percentWidth=95;
myChart.percentHeight=80;
var l:Legend = new Legend();
l.dataProvider = myChart;
l.direction=”horizontal”
l.percentWidth=95;
l.percentHeight=20;

myPanel.addChild(myChart);
myPanel.addChild(l);
}
private function removeDuplicates(arr:Array):Array  // This function retrieves the unique values from the Array
{
var currentValue:String = “”;
var tempArray:Array = new Array();
arr.sort(Array.CASEINSENSITIVE);
arr.forEach(
function(item:*, index:uint, array:Array):void {
if (currentValue != item) {
tempArray.push(item);
currentValue= item;
}
}
);

return tempArray;

}

]]>

</mx:Script>

<mx:Panel id=”myPanel” width=”300″ height=”300″>

</mx:Panel>

</mx:Application>

This will collect all BU Values and Status
for(var i:int=0;i<MainXMLList.length();i++)
{
BU.push(MainXMLList[i].attribute(“ows_Project_x0020_BU”))
Status.push(MainXMLList[i].attribute(“ows_Status”))
}Below code retrieves unique values and stores back into the same
array variable
BU=removeDuplicates(BU)

Status=removeDuplicates(Status)

Below code construct a loop for BUs and inner loop for status to
get the count of projects

for(var b:int=0;b<BU.length;b++)
{
HoldStatus.source.push({Dummy:”April”})
HoldStatus[b][“Title”]=BU[b]
for(var s:int=0;s<Status.length;s++)
HoldStatus[b][Status[s]]=MainXMLList.(attribute(“ows_Project_x0020_BU”)== BU[b] && attribute(“ows_Status”) == Status[s] ).length()
}

This will create an array collection like below 

{Title :BU1 , Pipeline: 2, Closed: 2}
{Title :BU2 , Pipeline: 1, Closed: 2}

 

Output

Advanced Datagrid from SharePoint data

Step 1: Create a sharepoint list with the following data in it.

Step 2: Retrieving custom list data as XML in flex. In order to create the
advanced datagrid with grouping (Column grouping lets you collect multiple
columns under a single column heading)

<?xml version=”1.0″ encoding=”utf-8″?><mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”
layout=”absolute” initialize=”initializeHandler();” ><mx:Script>
<![CDATA[
import mx.controls.Alert
import mx.controls.dataGridClasses.DataGridColumn
import mx.collections.*;
private var FinalXML:XMLList
[Bindable]
private var dpFlat:ArrayCollection=new ArrayCollection();
private function initializeHandler():void
{
var XMLLoader:URLLoader=
new URLLoader(new URLRequest(“<sharepointLini>/_vti_bin/owssvr.dll?Cmd=Display&List=%7BA6FFA525%2D0AC1%2D411E%2DA1F4%2D687402312595%7D&XMLDATA=TRUE”))
XMLLoader.addEventListener(Event.COMPLETE,openXMLValues)
function openXMLValues(e:Event):void
{
var XMLL:XML=new XML(e.target.data)
var rsNS:Namespace = XMLL.namespace(“rs”);
var zNS:Namespace = XMLL.namespace(“z”);
var sNS:Namespace = XMLL.namespace(“s”);
FinalXML=new XMLList(XMLL.rsNS::data.zNS::row)
var Obj:Object
for(var i:int=0;i<FinalXML.length();i++)
{
var attNames:XMLList = FinalXML[0].attributes();
Obj=new Object();
for (var i:int=0; i<attNames.length(); i++)
{
Obj[attNames[i].name().toString()]=attNames[i]
}
dpFlat.addItem(Obj)
}
}

}

]]>
</mx:Script>

<mx:Panel title=”AdvancedDataGrid Control Example”
height=”300″ width=”400″ layout=”horizontal”
paddingTop=”10″ paddingBottom=”10″ paddingLeft=”10″ paddingRight=”10″>

<mx:AdvancedDataGrid id=”myADG”>

<mx:dataProvider>
<mx:GroupingCollection2 id=”gc” source=”{dpFlat}”>
<mx:grouping>
<mx:Grouping>
<mx:GroupingField name=”ows_LinkTitle”/>
<mx:GroupingField name=”ows_Category2″/>
</mx:Grouping>
</mx:grouping>
</mx:GroupingCollection2>
</mx:dataProvider>

<mx:columns>
<mx:AdvancedDataGridColumn dataField=”ows_LinkTitle” headerText=”Category1″/>
<mx:AdvancedDataGridColumn dataField=”ows_Category2″ headerText=”Category2″/>
<mx:AdvancedDataGridColumn dataField=”ows_Value” headerText=”Value”/>
</mx:columns>
</mx:AdvancedDataGrid>
</mx:Panel>
</mx:Application>

Notes: XMLList values should be converted to ArrayCollection. ArrayCollection
will hold Objects. Grouping in advanced datagrid cannot be XMLList without
having a hierarchial data.

If XML value is like Below
<item value=”server”>
<citem value=”Integrity” >
<sitem value=”580i” />
<sitem value=”670i” />
</citem>
</item>

It can be treated as Hierarchical data and can be set as datasource to advanced
Datagrid. In this case, Group is very simple.

But Sharepoint gives the XML structure as

<z:row ows_Attachments=”0″ ows_LinkTitle=”Server” ows_Category2=”ProLiant”ows_Value=”580i” />
<z:row ows_Attachments=”0″ ows_LinkTitle=”Server” ows_Category2=”ProLiant”ows_Value=”670i”/>
<z:row ows_Attachments=”0″ ows_LinkTitle=”Server” ows_Category2=”Integrity”ows_Value=”580i”/>
<z:row ows_Attachments=”0″ ows_LinkTitle=”Server” ows_Category2=”Integrity”ows_Value=”780i2″/>
 
So this needs to be converted to either hierarchical XML structure or Array Collection like below dynamically

DP:   var ArrayCollection=new ArrayCollection(
{ows_Attachments:”0″, ows_LinkTitle:”Server”,ows_Category2:”ProLiant”, ows_Value:”580i”}
{ows_Attachments:”0″, ows_LinkTitle:”Server”,ows_Category2:”ProLiant”, ows_Value:”670i”}
{ows_Attachments:”0″, ows_LinkTitle:”Server”,ows_Category2:”Integrity”, ows_Value:”580i”}
{ows_Attachments:”0″, ows_LinkTitle:”Server”,ows_Category2:”Integrity”, ows_Value:”780i”}
)
for(var i:int=0;i<FinalXML.length();i++)
{
var attNames:XMLList = FinalXML[0].attributes(); // It retrieves all attributes from the xml node
Obj=new Object();
for (var i:int=0; i<attNames.length(); i++)  // Loop through attributes and create object with identifier name as attribute
{
Obj[attNames[i].name().toString()]=attNames[i]  // Creating identifier in object and assign the attribute values from xml.
}
dpFlat.addItem(Obj)  // add Object into the ArrayCollection (sample ows_Attachments:”0″, ows_LinkTitle:”Server”, ows_Category2:”ProLiant”, ows_Value:”580i” )

Output

Flex Charts with Defined Colors

This tutorial will show you how to create  chart programmatically out of an XML file with filling colors.Step1 :Create Country.xml with the following data and upload into <src> folder
Country.XML


<main>
<item Country= “USA” Gold=”35″ Silver=”39″ Bronze=”29″/>
<item Country= “China” Gold=”32″ Silver=”17″ Bronze=”14″/>
<item Country= “Russia” Gold=”27″ Silver=”27″ Bronze=”38″/>
</main>

Step2: Load Country.xml and convert it to XMLList

ColumnSeries: Defines a data series for a ColumnChart control.
CategoryAxis: CategoryAxis class to define a set of labels that appear along an axis of a chart.
setStyle: Sets a style property on any component instance. Here setStyle has been used to define the colors for each series
Color Codes which are applied for the Charts below


ChartDataXML.mxml

<?xml version=”1.0″ encoding=”utf-8″?><mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”
layout=”absolute” creationComplete=”createComplete(event)”><mx:Script><![CDATA[

import mx.charts.CategoryAxis;
import mx.charts.series.ColumnSeries;
import mx.charts.ColumnChart;


private
 function
createComplete(e:Event):void
{
var MainXML:URLLoader=new URLLoader(new URLRequest(“Country.XML”))
MainXML.addEventListener(Event.COMPLETE,LoadCountry)
}

private function LoadCountry(e:Event):void
{

var CL:XML=new XML(e.target.data)
var CountryList:XMLList=new XMLList(CL.item)
var IColumn1:ColumnChart=new ColumnChart();
IColumn1.dataProvider=CountryList

//Setting Horizontal Axis
var xAxis:CategoryAxis=new CategoryAxis()
xAxis.categoryField =”@Country”
IColumn1.horizontalAxis=xAxis;


//Column Serier for Silver

var NS:ColumnSeries= new ColumnSeries();
NS.yField = “@Silver”;
NS.setStyle(“fill”,”0x1b6528″) // Filling Color to the columnseries

IColumn1.series.push(NS) //Pushing columnseries to the column chart (just like a values been pushed to
an array)


//Column Serier for Bronze
var NS:ColumnSeries= new ColumnSeries();
NS.yField = “@Bronze”;
NS.setStyle(“fill”,”0x7cba87″) // Filling Color to the columnseries

IColumn1.series.push(NS) //Pushing columnseries to the column chart (just like a values been pushed to
an array)

//Column Serier for Gold

var NS:ColumnSeries = new ColumnSeries();
NS.yField = “@Gold”;
NS.setStyle(“fill”,”0x26b540″) // Filling Color to the columnseries
IColumn1.series.push(NS) //Pushing columnseries to the column chart (just like a values been pushed to an array)addChild(IColumn1)
}
]]>
</mx:Script>
</mx:Application>

You typically use the CategoryAxis class to define a set of labels that
appear along an axis of a chart

Output

Refresh Chart data

This tutorial will show you how to create component chart out of an XML file.

Step1 :Create Country.xml with the following data and upload into <src> folder

Country.XML

<main>
<item Country= “USA” Gold=”35″ Silver=”39″ Bronze=”29″/>
<item Country= “China” Gold=”32″ Silver=”17″ Bronze=”14″/>
<item Country= “Russia” Gold=”27″ Silver=”27″ Bronze=”38″/>
</main>

Step2: Create panel component with ColumnChart

ColumnCompChart.mxml
 Adding the below function  to the component column chart, that again assigns the medalAC to the columnchart dataprovider (Updates the data),  If there is a delay between the component initiation and data assignation, Chart wouldnt show the value. So refreshing the data would help to get the data updated. So below function help to manual trigger the chart to get the updated data.

public function refresh() :void  // A function to refresh the data for chart component
{
Column1.dataProvider=medalAC;  //again it assigns medalAC to the columnchart in order to get the data refreshed for column chart
}

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Panel xmlns:mx=”http://www.adobe.com/2006/mxml”
width=”400″ height=”300″ title=”My Chart”>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
public var medalsAC:XMLList; //as the data coming from XML , creating public
variable as XMLList.
public function refresh() :void  // A function to refresh the data for chart component
{
Column1.dataProvider=medalAC;  //again it assigns medalAC to the columnchart in order to get the data refreshed for column chart

}
]]>

</mx:Script>>
<mx:ColumnChart id=”Column1″ height=”100%” width=”100%” dataProvider=”{medalsAC}”>

<mx:horizontalAxis>
<mx:CategoryAxis categoryField=”@Country”/>
</mx:horizontalAxis>

<mx:series>
<mx:ColumnSeries xField=”@Country” yField=”@Gold”
displayName=”@Country” />
<mx:ColumnSeries xField=”@Country” yField=”@Silver”
displayName=”@Country” />
<mx:ColumnSeries xField=”@Country” yField=”@Bronze”
displayName=”@Country” />
</mx:series>
</mx:ColumnChart>

<mx:Legend dataProvider=”{Column1}” />
</mx:Panel >

Step3: Load Country.xml and convert it to XMLList and pass on to the
public variable medalsAC.

ChartDataXML.mxml

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”
layout=”absolute” creationComplete=”createComplete(event)”>
<mx:Script>
<![CDATA[
import MyComps.*;
import flash.utils.getDefinitionByName;
importmx.controls.*;private var CC:ColumnCompChart
private function createComplete(e:Event):void
{
var MainXML:URLLoader=new URLLoader(new URLRequest(“Country.XML”))
MainXML.addEventListener(Event.COMPLETE,MainXMLL)

}
private function MainXMLL(e:Event):void
{
var CL:XML=new XML(e.target.data)
var CountryList:XMLList=new
XMLList
(CL.item)
var cls:Class = getDefinitionByName(“MyComps.ColumnCompChart”) as Class;
var instance:Object = new cls();
instance.medalsAC=CountryList;
instance.refresh() //Calling refresh function to get the data updated
addChild(DisplayObject(instance) );
}

]]>
</mx:Script>
</mx:Application>

Programmatically create chart in flex

This tutorial will show you how to create  chart programmatically out of an XML file.

Step1 :Create Country.xml with the following data and upload into <src> folder

Country.XML

<main>
<item Country= “USA” Gold=”35″ Silver=”39″ Bronze=”29″/>
<item Country= “China” Gold=”32″ Silver=”17″ Bronze=”14″/>
<item Country= “Russia” Gold=”27″ Silver=”27″ Bronze=”38″/>
</main>


Step2: Load Country.xml and convert it to XMLList

ColumnSeries: Defines a data series for a ColumnChart control.
CategoryAxis: CategoryAxis class to define a set of labels that appear along an axis of a chart.

ChartDataXML.mxml

<?xml version=”1.0″ encoding=”utf-8″?>

<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” creationComplete=”createComplete(event)”>
<mx:Script>
<![CDATA[
import mx.charts.CategoryAxis;
import mx.charts.series.ColumnSeries;
import mx.charts.ColumnChart;
private function createComplete(e:Event):void
{
var MainXML:URLLoader=new URLLoader(new URLRequest(“Country.XML”))
MainXML.addEventListener(Event.COMPLETE,LoadCountry)
}
private function LoadCountry(e:Event):void
{
var CL:XML=new XML(e.target.data)
var CountryList:XMLList=new XMLList(CL.item)
var IColumn1:ColumnChart=new ColumnChart();
IColumn1.dataProvider=CountryList

 //Setting Horizontal Axis
var xAxis:CategoryAxis=new CategoryAxis()
xAxis.categoryField = “@Country”
IColumn1.horizontalAxis=xAxis;

//Column Serier for Silver
var NS:ColumnSeries = new ColumnSeries();
NS.yField = “@Silver”;
IColumn1.series.push(NS) //Pushing columnseries to the column chart (just like a values been pushed to an array)

//Column Serier for Bronze
var NS:ColumnSeries = new ColumnSeries();
NS.yField = “@Bronze”;
IColumn1.series.push(NS) //Pushing columnseries to the column chart (just like a values been pushed to an array)

//Column Serier for Gold
var NS:ColumnSeries = new ColumnSeries();
NS.yField = “@Gold”;
IColumn1.series.push(NS) //Pushing columnseries to the column chart (just like a values been pushed to an array)

addChild(IColumn1)
}
]]>
</mx:Script>
</mx:Application>

You typically use the CategoryAxis class to define a set of labels that appear along an axis of a chart

Pass parameters to event Listener

This tutorial will show you how to pass extra parameters apart from event to event listener.

Event Listener

var ULoader:URLLoader=new URLLoader(new URLRequest(“test.xml”))
ULoader.addEventlistener(Event.COMPLETE, CallFunction(param1,param2))

Function Definition

function callFunction(param1,param2):Function(
return function(e:Event):void

{

<!–
Code goes here
–>

}
}

XML to Chart Component

This tutorial will show you how to create component chart out of an XML file.

Step1 :Create Country.xml with the following data and upload into <src> folder

Country.XML

<main>
<item Country= "USA" Gold="35" Silver="39" Bronze="29"/>
<item Country= "China" Gold="32" Silver="17" Bronze="14"/>
<item Country= "Russia" Gold="27" Silver="27" Bronze="38"/>
</main>

Step2: Create panel component with ColumnChart

ColumnCompChart.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml"
width="400" height="300" title="My Chart">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
public var medalsAC:XMLList; //as the data coming from XML , creating public
variable as XMLList.
]]>
</mx:Script>>
<mx:ColumnChart id="Column1" height="100%" width="100%" dataProvider="{medalsAC}">

<mx:horizontalAxis>
<mx:CategoryAxis categoryField="@Country"/>
</mx:horizontalAxis>

<mx:series>
<mx:ColumnSeries xField="@Country" yField="@Gold"
displayName="@Country" />
<mx:ColumnSeries xField="@Country" yField="@Silver"
displayName="@Country" />
<mx:ColumnSeries xField="@Country" yField="@Bronze"
displayName="@Country" />
</mx:series>
</mx:ColumnChart>

<mx:Legend dataProvider="{Column1}" />
</mx:Panel >

Step3: Load Country.xml and convert it to XMLList and pass on to the
public variable medalsAC.

ChartDataXML.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" creationComplete="createComplete(event)">
<mx:Script>
<![CDATA[
import MyComps.*;
import flash.utils.getDefinitionByName;
import mx.controls.*;

private var CC:ColumnCompChart
private function createComplete(e:Event):void
{
var MainXML:URLLoader=new URLLoader(new URLRequest("Country.XML"))
MainXML.addEventListener(Event.COMPLETE,MainXMLL)

}
private function MainXMLL(e:Event):void
{
var CL:XML=new XML(e.target.data)
var CountryList:XMLList=new
XMLList
(CL.item)
var cls:Class = getDefinitionByName("MyComps.ColumnCompChart") as Class;
var instance:Object = new cls();
instance.medalsAC=CountryList;
addChild(DisplayObject(instance) );
}
]]>
</mx:Script>
</mx:Application>

Custom component chart

The following example shows how you can create a column chart as a component and invoke in application and access component variable.
dynamic values by calling the getDefintionByName() method.


ChartDef.mxml

<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”
creationComplete=”createComplete(event)”>
<mx:Script>
<![CDATA[
import MyComps.*;
import flash.utils.getDefinitionByName;
import mx.controls.*;
private var CC:PanelComp;ColumnCompChart // Initiate ColumnCompChart component.
private var MData:ArrayCollection = new ArrayCollection( [
{ Country: “USA”, Gold: 35, Silver:39, Bronze: 29 },
{ Country: “China”, Gold: 32, Silver:17, Bronze: 14 },
{ Country: “Russia”, Gold: 27, Silver:27, Bronze: 38 } ]);

private function createComplete(e:Event):void
{
var cls:Class = getDefinitionByName(“MyComps.ColumnCompChart”) as Class;
var instance:Object = new cls();
instance.medalsAC=MData; // Assigning MData arrayCollection to medalAC [Component Variable]
addChild(DisplayObject(instance) );
}
]]>
</mx:Script>
</mx:Application>

ColumnCompChart.mxml

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Panel xmlns:mx=”http://www.adobe.com/2006/mxml” width=”400″ height=”300″title=”My Chart”>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
public var medalsAC:ArrayCollection;
]]>
</mx:Script>
<mx:ColumnChart id=”Column1″ height=”100%” width=”100%”
dataProvider=”{medalsAC}”>
<mx:horizontalAxis>
<mx:CategoryAxis categoryField=”Country”/>
</mx:horizontalAxis>

<mx:series>
<mx:ColumnSeries xField=”Country” yField=”Gold” displayName=”Country” />
<mx:ColumnSeries xField=”Country” yField=”Silver” displayName=”Country” />
<mx:ColumnSeries xField=”Country” yField=”Bronze” displayName=”Country” />
</mx:series>

</mx:ColumnChart>
<mx:Legend dataProvider=”{Column1}” />
</mx:Panel>

Create Column Chart Component

The following example shows how you can create a column chart as a component and invoke in application
by calling the getDefintionByName() method.


ChartDef.mxml

<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”
creationComplete=”createComplete(event)”>
<mx:Script>
<![CDATA[
import MyComps.*;
import flash.utils.getDefinitionByName;
import mx.controls.*;
private var CC:PanelComp;ColumnCompChart // Initiate ColumnCompChart component.
private function createComplete(e:Event):void
{
var cls:Class = getDefinitionByName(“MyComps.ColumnCompChart”) as Class;
var instance:Object = new cls();
addChild(DisplayObject(instance) );
}
]]>
</mx:Script>
</mx:Application>

ColumnCompChart.mxml

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Panel xmlns:mx=”http://www.adobe.com/2006/mxml” width=”400″ height=”300″ 

title=”My Chart”>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
public var medalsAC:ArrayCollection = new ArrayCollection( [
{ Country: “USA”, Gold: 35, Silver:39, Bronze: 29 },
{ Country: “China”, Gold: 32, Silver:17, Bronze: 14 },
{ Country: “Russia”, Gold: 27, Silver:27, Bronze: 38 } ]);
]]>
</mx:Script>
<mx:ColumnChart id=”Column1″ height=”100%” width=”100%”
dataProvider=”{medalsAC}”>
<mx:horizontalAxis>
<mx:CategoryAxis categoryField=”Country”/>
</mx:horizontalAxis>

<mx:series>
<mx:ColumnSeries xField=”Country” yField=”Gold” displayName=”Country” />
<mx:ColumnSeries xField=”Country” yField=”Silver” displayName=”Country” />
<mx:ColumnSeries xField=”Country” yField=”Bronze” displayName=”Country” />
</mx:series>

</mx:ColumnChart>
<mx:Legend dataProvider=”{Column1}” />
</mx:Panel>