2 Replies Latest reply on Jul 13, 2011 3:10 AM by PradeepValla

    BarSeries Using XML Data in ActionScript 3

    PradeepValla

      Hello,

       

      I am trying to create an application. I am getting the data form the HTTPService.

       

      I need to to create an Stackbar chart

       

      XML Data for the same is as follows:

       

      <?xml version="1.0" ?>

      <Data>

      <Brand BName='ICICI Bank' MainId='276439cd8a4514807d7180d0a2e886311a426147'>

           <Name Main='(author unknown)' Count='729' />

           <Name Main='info2india' Count='46' />

           <Name Main='bankcodes' Count='42' />

           <Name Main='sriprashant66' Count='36' />

           <Name Main='intellibitz' Count='33' />

      </Brand>

      <Brand BName='Yes Bank' MainId='4ce265a7f48e76faff32aa9764a5fb5c01f11b5e'>

           <Name Main='(author unknown)' Count='59' />

           <Name Main='khan_4u' Count='9' />

           <Name Main='STOCKSTIPS' Count='8' />

           <Name Main='stockezymobile' Count='6' />

           <Name Main='Intraday Ideas' Count='4' />

      </Brand>

      </Data>

       

      Please guide witht the code.

        • 1. Re: BarSeries Using XML Data in ActionScript 3
          Pooja Kuber Level 2

          Here is the sample code for Stacked BarSeries Using XML Data in ActionScript 3

           

          <?xml version="1.0"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
              creationComplete="srv.send()" >
            <mx:HTTPService id="srv" url="assets/data.xml"
               useProxy="false"
               result="chartData=ArrayCollection(srv.lastResult.Data.Brand);createBarSeries();"
          />

           

          <mx:Script>
            <![CDATA[
             import mx.graphics.SolidColor;
             import mx.charts.series.BarSeries;
             import mx.events.CollectionEvent;
             import mx.collections.ArrayCollection;
             import mx.utils.ArrayUtil;
             [Bindable]
             private var chartAC:ArrayCollection=new ArrayCollection();
             private var chartData:ArrayCollection=new ArrayCollection();
             [Bindable]
             private var colors:Array=[0xDF0101, 0x298A08, 0x2ECCFA, 0xDF01A5, 0xD7DF01, 0x800000, 0x778899, 0xFF00FF, 0x000080, 0xFFA07A, 0x00FF00];

           

             private function createBarSeries():void
             {
              var seriesDetails:ArrayCollection=new ArrayCollection();
              for(var j:int=0; j<chartData.length; j++)
              {
               var chartObj:Object=new Object();
               chartObj.BName=chartData[j].BName;
               for (var i:int=0; i < chartData[j].Name.length; i++)
               {
                var s:String=chartData[j].Name[i].Main;
                chartObj[s+j]=chartData[j].Name[i].Count;
                var seriesObj:Object={XField: s+j, DisplayName: s};
                seriesDetails.addItem(seriesObj);
               }
               chartAC.addItem(chartObj);
              }

           

              // Create a BarSeries, and an array to be
              // populated with dynamically generated BarSeries objects
              var barSeries:BarSeries;
              var seriesArray:Array=new Array();

           

              // Generate an array of BarSeries objects
              for (i=0; i < seriesDetails.length; i++)
              {
               barSeries=new BarSeries();
               barSeries.xField=seriesDetails[i].XField;
               barSeries.displayName=seriesDetails[i].DisplayName;
               var sc:SolidColor = new SolidColor(colors[i]);
                     sc.alpha = 0.9;
                      barSeries.setStyle("fill",sc);
               seriesArray.push(barSeries);
              }
              barChart.series=seriesArray;

           

             }
            ]]>
          </mx:Script>

           

          <mx:Panel title="Stacked Bar Chart Using XML data" width="500" height="500">
            <mx:BarChart id="barChart"
                type="stacked"
                dataProvider="{chartAC}"
                showDataTips="true"
                width="100%"
                height="100%">
             <mx:verticalAxis>
              <mx:CategoryAxis categoryField="BName"/>
             </mx:verticalAxis>
            </mx:BarChart>
            <mx:Legend dataProvider="{barChart}" direction="horizontal" width="100%"/>
          </mx:Panel>
          </mx:Application>

          • 2. Re: BarSeries Using XML Data in ActionScript 3
            PradeepValla Level 1

            Thank you this worked very well :

             

            This post is usefull those who are looking for dynanic BarSeries Creation using multiple level data . Nice one Tanks for the Help and code.