5 Replies Latest reply on Feb 17, 2009 12:46 PM by Gregory Lafrance

    AdvancedDataGrid in Actionscript 3

    G009 Level 1
      Hey Friends

      I was wondering if you can tell me how to create AdvancedDataGrid using Actionscript 3. I wrote one code but its not working. Please tell me how to write AdvancedDataGrid just using actionscript3. Complete code shoul'd be in AS3.
      Thanks in advance.
        • 1. Re: AdvancedDataGrid in Actionscript 3
          Gregory Lafrance Level 6
          FB3 help has examples. What code did you use and what went wrong?
          • 2. AdvancedDataGrid in Actionscript 3
            G009 Level 1
            Hi Greg

            Well this is my code. Only the advanceddatagrid part in switch-case 3 not working. I did the same thing which i did for normal datagrid, ofcourse with syntax change. I'm also attaching my xml files.

            <?xml version="1.0"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
            creationComplete="create()">

            <mx:HTTPService id="head" url="header.do"
            showBusyCursor="true" result="xmlheaderHandler(event)" resultFormat="e4x"/>
            <mx:HTTPService id="data" url="test.do"
            showBusyCursor="true" result="xmlHandler(event)" resultFormat="e4x"/>
            <mx:Script>
            <![CDATA[
            import mx.controls.AdvancedDataGrid;
            import mx.utils.ColorUtil;
            import mx.graphics.SolidColor;
            import mx.containers.VBox;
            import mx.containers.HBox;
            import mx.collections.XMLListCollection;
            import mx.charts.ColumnChart;
            import mx.charts.series.ColumnSeries;
            import mx.charts.CategoryAxis;
            import mx.charts.Legend;
            import mx.rpc.events.ResultEvent;
            import mx.controls.Alert;
            import mx.controls.dataGridClasses.DataGridColumn;
            import mx.controls.DataGrid;
            import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;

            public var tag1:Number;
            public var tag2:Number;
            [Bindable]public var expenses:XMLListCollection;
            [Bindable]public var header:XMLListCollection;

            public function xmlheaderHandler(evt:ResultEvent):void{
            // Sets testInfo's root as the student. Everything else
            // referenced in respect to this.
            var list:XMLList = evt.result..HEADER;
            header = new XMLListCollection(list);

            }

            public function xmlHandler(evt:ResultEvent):void{
            // Sets testInfo's root as the student. Everything else
            // referenced in respect to this.
            var list:XMLList = evt.result..VALUES;
            expenses = new XMLListCollection(list);

            }

            //Chart variables
            public var myChart1:ColumnChart;
            public var myChart2:ColumnChart;
            public var series1:ColumnSeries;
            public var series2:ColumnSeries;
            public var legend1:Legend;
            public var legend2:Legend;

            //Grid variables
            public var dg:DataGrid = new DataGrid();
            public var adg:AdvancedDataGrid = new AdvancedDataGrid();

            public function create():void {
            head.send();
            data.send();
            }


            public function showme():void {
            var value:String;
            for each(var item:XML in header){
            value = item.COUNT[0].toString();
            }
            Alert.show(value);

            }



            public function init(num:Number):void {
            var i:int;
            var max:int;
            var clms:Array;
            var xaxis:String;
            var yaxis1:String;
            var yaxis2:String;
            //Alert.show(xaxis);

            // Create the chart object and set some basic properties.
            myChart1 = new ColumnChart();
            myChart1.showDataTips = true;
            myChart1.dataProvider = expenses;
            myChart2 = new ColumnChart();
            myChart2.showDataTips = true;
            myChart2.dataProvider = expenses;

            //Creating X-axis and Y-axis
            for each(var item1:XML in header){
            xaxis = item1..ID[0].toString();
            }
            for each(var item2:XML in header){
            yaxis1 = item2..ID[1].toString();
            }
            for each(var item3:XML in header){
            yaxis2 = item3..ID[2].toString();
            }
            for each(var item4:XML in header){
            max = item3..COUNT[0].toString();
            }

            // Define the category axis.
            var hAxis1:CategoryAxis = new CategoryAxis();
            hAxis1.categoryField =xaxis ;
            hAxis1.dataProvider = expenses;
            myChart1.horizontalAxis = hAxis1;

            // Add the series.
            var mySeries1:Array=new Array();
            series1 = new ColumnSeries();
            series1.xField=xaxis;
            series1.yField=yaxis1;
            series1.displayName = "Merchant Amount Average";
            mySeries1.push(series1);
            myChart1.series = mySeries1;

            //var clr:SolidColor = new SolidColor();


            // Define the category axis.
            var hAxis2:CategoryAxis = new CategoryAxis();
            hAxis2.categoryField =xaxis ;
            hAxis2.dataProvider = expenses;
            myChart2.horizontalAxis = hAxis2;

            // Add the series.
            var mySeries2:Array=new Array();
            series2 = new ColumnSeries();
            series2.xField=xaxis;
            series2.yField=yaxis2;
            series2.displayName = "Account Count";
            mySeries2.push(series2);
            myChart2.series = mySeries2;

            // Create a legend.
            legend1 = new Legend();
            legend1.dataProvider = myChart1;
            legend2 = new Legend();
            legend2.dataProvider = myChart2;





            //VBox for Chart and legend
            var hbox:HBox = new HBox();

            var vbox1:VBox = new VBox();
            vbox1.addChild(myChart1);
            vbox1.addChild(legend1);

            var vbox2:VBox = new VBox();
            vbox2.addChild(myChart2);
            vbox2.addChild(legend2);

            hbox.addChild(vbox1);
            hbox.addChild(vbox2);

            switch(num)
            {
            case 1:{
            subpan.removeAllChildren();
            subpan.title = "Chart using Actionscript";
            subpan.height = 500;
            subpan.width = 850;
            subpan.addChild(hbox);
            }
            break;
            case 2:{
            for each(var item6:XML in header){
            max = Number(item6..COUNT[0]);
            }
            dg = new DataGrid();
            dg.dataProvider=expenses;
            dg.width = 500;
            dg.height = 500;

            clms = dg.columns;
            for(i=0;i<max;i++)
            {

            for each(var item5:XML in header){
            var dgc:DataGridColumn = new DataGridColumn();
            dgc.dataField = item5..ID .toString();
            dgc.headerText = item5..VALUE
            .toString();
            clms.push(dgc);
            }

            }
            dg.columns=clms;
            var hbox2:HBox = new HBox();
            hbox2.addChild(dg);


            subpan.removeAllChildren();
            subpan.title = "DataGrid using Actionscript";
            subpan.height = 500;
            subpan.width = 500;
            subpan.addChild(hbox2);
            }
            break;
            case 3:{
            for each(var item7:XML in header){
            max = Number(item7..COUNT[0]);
            }
            adg = new AdvancedDataGrid();
            adg.dataProvider=expenses;
            adg.width = 500;
            adg.height = 500;

            clms = adg.columns;
            for(i=0;i<max;i++)
            {

            for each(var item8:XML in header){
            var adgc:AdvancedDataGridColumn = new AdvancedDataGridColumn();
            adgc.dataField = item8..ID .toString();
            adgc.headerText = item8..VALUE
            .toString();
            clms.push(adgc);
            }

            }
            adg.columns=clms;
            var hbox3:HBox = new HBox();
            hbox3.addChild(adg);


            subpan.removeAllChildren();
            subpan.title = "DataGrid using Actionscript";
            subpan.height = 500;
            subpan.width = 500;
            subpan.addChild(hbox2);
            }
            break;
            default:
            Alert.show("Wrong Choice!!");
            break;
            }

            }
            ]]>
            </mx:Script>


            <mx:Panel id="pan">
            <mx:HBox>
            <mx:Button id="bt1" label="Alert" click="showme()"/>
            <mx:Button id="bt2" label="Chart" click="init(1)"/>
            <mx:Button id="bt3" label="Grid" click="init(2)"/>
            <mx:Button id="bt4" label="Chart2" click="init(3)"/>
            </mx:HBox>
            <mx:Panel id="subpan" height="200" width="200"/>
            </mx:Panel>
            </mx:Application>

            .......................................................................................... ...........................................................................

            header.xml

            <?xml version="1.0" encoding="utf-8"?>
            <XMLDocument>
            <HEADER>
            <COUNT>3</COUNT>
            <COLUMN><ID>c10</ID><VALUE>Bill Month</VALUE></COLUMN>
            <COLUMN><ID>c11</ID><VALUE>Merchant Amount Average</VALUE></COLUMN>
            <COLUMN><ID>c12</ID><VALUE>Account Count</VALUE></COLUMN>
            </HEADER>
            </XMLDocument>

            .......................................................................................... ..................................................................

            test.xml

            <?xml version="1.0" encoding="utf-8"?>
            <XMLDocument>
            <VALUES><c10>Nov 2005</c10><c11>205.2052</c11><c12>463290.84</c12></VALUES>
            <VALUES><c10>Dec 2005</c10><c11>241.817</c11><c12>465985.98</c12></VALUES>
            <VALUES><c10>Jan 2006</c10><c11>231.0464</c11><c12>468981.96</c12></VALUES>
            </XMLDocument>

            • 3. AdvancedDataGrid in Actionscript 3
              Gregory Lafrance Level 6
              You're creating hbox2 in the switch case 2 and accessing it in switch case 3. If you want to access it in both, create hbox2 outside the switch.
              • 4. Re: AdvancedDataGrid in Actionscript 3
                G009 Level 1
                Oh my god, that was the silly mistake i was doing. Ya i changed it, and its working now. Thanks a lot.
                • 5. Re: AdvancedDataGrid in Actionscript 3
                  Gregory Lafrance Level 6
                  It's the silly things that get ya (and me :-).