Mega Code Archive

 
Categories / Flex / Chart
 

Creating charts in ActionScript

<?xml version="1.0"?> <!-- charts/CreateChartInActionScript.mxml --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"   creationComplete="init()">   <mx:Script>     import mx.collections.ArrayCollection;     import mx.charts.BarChart;     import mx.charts.series.BarSeries;     import mx.charts.CategoryAxis;     import mx.charts.Legend;     [Bindable]     public var expenses:ArrayCollection = new ArrayCollection([       {Month:"Jan", Profit:2000, Expenses:1500},       {Month:"Feb", Profit:1000, Expenses:200},       {Month:"Mar", Profit:1500, Expenses:500}       ]);     public var myChart:BarChart;     public var series1:BarSeries;     public var series2:BarSeries;     public var legend1:Legend;     public function init():void {       // Create the chart object and set some       // basic properties.       myChart = new BarChart();       myChart.showDataTips = true;       myChart.dataProvider = expenses;       // Define the category axis.       var vAxis:CategoryAxis = new CategoryAxis();       vAxis.categoryField = "Month" ;       vAxis.dataProvider = expenses;       myChart.verticalAxis = vAxis;       // Add the series.       var mySeries:Array=new Array();       series1 = new BarSeries();       series1.xField="Profit";       series1.yField="Month";       series1.displayName = "Profit";       mySeries.push(series1);       series2 = new BarSeries();       series2.xField="Expenses";       series2.yField="Month";       series2.displayName = "Expenses";       mySeries.push(series2);       myChart.series = mySeries;       // Create a legend.       legend1 = new Legend();       legend1.dataProvider = myChart;       // Attach chart and legend to the display list.       p1.addChild(myChart);       p1.addChild(legend1);     }     </mx:Script>   <mx:Panel id="p1" title="Bar Chart Created in ActionScript" /> </mx:Application>