4 Replies Latest reply on Nov 29, 2010 6:50 PM by Bobm_10

    Setting data to a chart

    Bobm_10

      AS:

       

      Hi All,

       

      I am new to Flex but have a simple question on setting data to a bar chart.  Curious, in the code below the values in my BarChart become all 4 because of the binding to the array?  If MyArray is local to the button click function, won't that become a problem when execution leaves the function since the DataProvider will be pointing to an array that does not exist.  Although it does seem to work fine.  Thanks  Bob

       

      protected function button1_clickHandler(event:MouseEvent):void
         {
          // TODO Auto-generated method stub
          var MyArray : Array = new Array();
          MyArray[0] = 1;
          MyArray[1] = 2;
          MyArray[2] = 3;
            
          series1.dataProvider = MyArray;       

          barchart1.invalidateSeriesStyles();
         
          MyArray[0] = 4;
          MyArray[1] = 4;
          MyArray[2] = 4; }
                                           
         

      MX:

      <mx:BarChart x="27" y="91" id="barchart1" height="259" type="stacked">
        <mx:series>
         <mx:BarSeries id="series1" displayName="Series 1" xField=""/>
         <mx:BarSeries id="series2" displayName="Series 2" xField=""/>
        </mx:series>
      </mx:BarChart>

        • 1. Re: Setting data to a chart
          flex2008 Level 3

          When you write series1.dataProvider = MyArray,you are actually setting the dataProvider property of the series with the  contents of myArray.So even if the myArray variable goes out of scope,the dataProvider of series will still have the contents,since the dataProvider remains intact.Hope I answered your question.

          • 2. Re: Setting data to a chart
            Bobm_10 Level 1

            > setting the dataProvider property of the series with the  contents of myArray.<

            Interesting, but you will notice I am setting the contents of myArray to 4 after I make the assignment to dataProvider and dataPRovider sees the change.  So it seems that it is "watching" the array?  Is it possible that once it sees that the array leaves the scope that it no longer "Watches?"  Thanks  Bob

            • 3. Re: Setting data to a chart
              flex2008 Level 3

              Basically when you say series.dataProvider=myArray,the source property of the collection object created will have a source property which will be referencing the same memory (lets say 0x000000)referenced by myArray.Whenever you are saying myArray[0]=5,you are modifying the memory referenced through the handle myArray.Therefore,the change will be reflected in the collection also.When function ends,what is happening is that the myArray handle to the memory 0x000000 is lost.But the memory content will remain since there is a handle via the source of the collection.

               

              This is my explanation for the scenario.I assume this is correct.I feel it is better to confirm with other experts in the forum before coming to a conclusion.

              • 4. Re: Setting data to a chart
                Bobm_10 Level 1

                Perfect answer,  thanks  Bob