6 Replies Latest reply on Sep 24, 2009 6:30 AM by adityaavinash

    chart created in actionscript can not access xml file..Plz help

    adityaavinash

      In the following code i get the xml data ver http which is converted to arraycollection. but when i assign this as dataprovider for the barchart,blank chart is formed.the xml data  is received by the program.(i have verified it). there is no runtime or compile time error. Plz suggest me the way to solve this problem.

      thanks in advance.

       

      code is s follow:

       

      <?xml version="1.0"?>
      <!-- charts/CreateChartInActionScript.mxml -->
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
      creationComplete="init()">
      <mx:HTTPService id="srv1" url="/dashboard1/"
            useProxy="false"
           result="myData=ArrayCollection(srv1.lastResult.brands.name)"
      />
        <mx:Script><![CDATA[

       

           import mx.collections.ArrayCollection;
           import mx.charts.BarChart;
           import mx.charts.series.BarSeries;
           import mx.charts.CategoryAxis;
           import mx.charts.Legend;
      [Bindable]
           public var myData:ArrayCollection;
           [Bindable]
           public var root_url:String ="http://127.0.0.1:8000";


           [Bindable]
           public var myChart:BarChart;
               [Bindable]
           public var series1:BarSeries;
               [Bindable]
           public var series2:BarSeries;
               [Bindable]
                public var series3:BarSeries;
               [Bindable]
                public var series4:BarSeries;
               [Bindable]
                public var series5:BarSeries;
           [Bindable]
                public var legend1:Legend;
             
           public function init():void {
              // Create the chart object and set some
              // basic properties.
              srv1.url=root_url+srv1.url;
              srv1.send();
              myChart = new BarChart();
              myChart.showDataTips = true;
              myChart.dataProvider =myData;
              myChart.type="100%"
              mychart1.type="100%"
              // Define the category axis.
              var vAxis:CategoryAxis = new CategoryAxis();
              vAxis.categoryField = "brand";
              vAxis.dataProvider =  myData;
              myChart.verticalAxis = vAxis;

       

              // Add the series.
              var mySeries:Array=new Array();
              series1 = new BarSeries();
              series1.xField="very_positive";
              series1.yField="brand";
              series1.displayName = "very_positive";
              mySeries.push(series1);

       

              series2 = new BarSeries();
              series2.xField="positive";
              series2.yField="brand";
              series2.displayName="positive"
              mySeries.push(series2);
             
              series3 = new BarSeries();
              series3.xField="neutral";
              series3.yField="brand";
              series3.displayName="neutral"
              mySeries.push(series3);
             
              series4 = new BarSeries();
              series4.xField="negative";
              series4.yField="brand";
              series4.displayName="negative"
              mySeries.push(series4);
             
              series5 = new BarSeries();
              series5.xField="very_negative";
              series5.yField="brand";
              series5.displayName="very_negative"
              mySeries.push(series5);

       

              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>

        • 1. Re: chart created in actionscript can not access xml file..Plz help
          _Natasha_ Level 4

          Hi,

          the problem is very simple:

          dataProvider property for chart is not bindable!

           

          You create a chart, myData is null.

          You get data and create an ArrayCollection to myData.

          Check in debugger the value of dataProvider for you chart. As I remember it should be changed to new value of myData.

          So after that you should write string like:

           

          myChart.dataProvider = myChart.dataProvider;

          If it's not changed. Reinit it.

          • 2. Re: chart created in actionscript can not access xml file..Plz help
            adityaavinash Level 1

            @natasha:  even that did not help. the debugger shows the value of myData to be null. Plz suggest me any alternate way of creating chart in action script and get the xml data from the server to populate the chart. Thanks in advance.

            • 3. Re: chart created in actionscript can not access xml file..Plz help
              _Natasha_ Level 4

              hee...

              If you get XML from server, you cann't automatically get an ArrayCollection!

               

              <mx:HTTPService id="srv1" url="/dashboard1/"
                    useProxy="false"
                   result="myData=ArrayCollection(srv1.lastResult.brands.name)"
              />

               

              If srv1.lastResult is XML

              srv1.lastResult.brands.name is XMLList

               

              XMLList is also not bindable, so you should use XMLListCollection.

              Try somethings like this:

               

              myData = new XMLListCollection(srv1.lastResult.brands.name)

              • 4. Re: chart created in actionscript can not access xml file..Plz help
                adityaavinash Level 1

                Thanks a lot!! But even this did not work.I can access the myData and able to populate the chart if i create the chart in the  mxml (and not actionscript). so i think the problem does not lie in  the data received. the chart data provider of actionsscript chart can not get the data stored in myData. Can you plz send me the link of any example in which the chart is created in the actionscript and chart is populated using data accessed through server.

                • 5. Re: chart created in actionscript can not access xml file..Plz help
                  _Natasha_ Level 4

                  The only difference between mxml and as is that in mxml all dataProvider="{someething}" converted to BindingUtils.bindProperty().

                   

                  In your code is just assigning. So you need to change this.

                  • 6. Re: chart created in actionscript can not access xml file..Plz help
                    adityaavinash Level 1

                    Hey I got the solution..   Thanks to natasha for response.

                     

                    the perfectly working code is as follow:

                     

                    <?xml version="1.0"?>
                    <!-- charts/CreateChartInActionScript.mxml -->
                    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                    creationComplete="init()">
                      <mx:Script><![CDATA[

                     

                         import mx.collections.ArrayCollection;
                         import mx.charts.BarChart;
                         import mx.charts.series.BarSeries;
                         import mx.charts.CategoryAxis;
                         import mx.charts.Legend;
                          [Bindable]
                         public var myData:ArrayCollection;
                         [Bindable]
                         public var myChart:BarChart;
                             [Bindable]
                         public var series1:BarSeries;
                             [Bindable]
                         public var series2:BarSeries;
                             [Bindable]
                              public var series3:BarSeries;
                             [Bindable]
                              public var series4:BarSeries;
                             [Bindable]
                              public var series5:BarSeries;
                         [Bindable]
                              public var legend1:Legend;
                            [Bindable] public var vAxis:CategoryAxis = new CategoryAxis();
                                import mx.controls.Alert;
                          
                                import mx.rpc.events.ResultEvent;
                                import mx.rpc.events.FaultEvent;
                                import mx.rpc.http.HTTPService;
                                public var service:HTTPService

                     

                                public function useHttpService():void {
                                    service = new HTTPService();
                                    service.method = "POST";
                                    service.url="http://127.0.0.1:8000/dashboard1";
                                    service.addEventListener("result", httpResult);
                                    service.addEventListener("fault", httpFault);
                                    service.send();
                                     }

                     

                                public function httpResult(event:ResultEvent):void {
                                              var result:Object = event.result;
                                myChart.dataProvider =ArrayCollection(event.result.brands.name);
                                        vAxis.categoryField = "brand";
                                    vAxis.dataProvider = ArrayCollection(event.result.brands.name);;
                            vAxis.dataProvider = vAxis.dataProvider;

                     

                                }

                     

                                public function httpFault(event:FaultEvent):void {
                                    var faultstring:String = event.fault.faultString;
                                    Alert.show(faultstring);
                                }

                     

                         public function init():void {
                            useHttpService()
                           
                            myChart = new BarChart();
                            myChart.showDataTips = true;
                            myChart.dataProvider = myChart.dataProvider;
                            myChart.type="100%"
                            myChart.verticalAxis = vAxis;
                            var mySeries:Array=new Array();
                            series1 = new BarSeries();
                            series1.xField="very_positive";
                            series1.yField="brand";
                            series1.displayName = "very_positive";
                            mySeries.push(series1);

                     

                            series2 = new BarSeries();
                            series2.xField="positive";
                            series2.yField="brand";
                            series2.displayName="positive"
                            mySeries.push(series2);
                           
                            series3 = new BarSeries();
                            series3.xField="neutral";
                            series3.yField="brand";
                            series3.displayName="neutral"
                            mySeries.push(series3);
                           
                            series4 = new BarSeries();
                            series4.xField="negative";
                            series4.yField="brand";
                            series4.displayName="negative"
                            mySeries.push(series4);
                           
                            series5 = new BarSeries();
                            series5.xField="very_negative";
                            series5.yField="brand";
                            series5.displayName="very_negative"
                            mySeries.push(series5);

                     

                            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>