2 Replies Latest reply on Dec 19, 2009 9:04 PM by 100amplifiers

    Flex 3: syntax for populating ArrayCollection by loop

    100amplifiers

      Hi, I'm a newb. I can't find an example of how to do this. Any examples would be greatl appreciated.

       

      I want to chart some engineering data by populating my chart's dataprovider with an array collection (so far no problem). I want to poulate the array collection by looping through a math expression and I can't figure out the syntax for that. For example...

       

      [Bindable]
      private var myChartData:ArrayCollection = new ArrayCollection([
          {vertNum:100, horizNum:100}
      ]);

       

      I want to loop through and add more points programmatically so if I loop 5 times I end up with...

       

      [Bindable]
      private var myChartData:ArrayCollection = new ArrayCollection([
           {vertNum:100, horizNum:100},

          {vertNum:110, horizNum:110},

          {vertNum:43, horizNum:120},

          {vertNum:88, horizNum:130},

          {vertNum:140, horizNum:140},

           // etc...
      ]);

       

      The number of horizontal variables to plot ould be in the hundreds, so I don't want to set up a huge static array collection with expressions in each location. I'd prefer to solve the expression as a loop iterates and populate the array collection as it goes. But I haven't got a clue how to 'word' that.

       

      thanks!

        • 1. Re: Flex 3: syntax for populating ArrayCollection by loop
          chris.huston.t10 Level 3

          Here is an example of adding data points from a simple formula using a random number generator and scaling the randomness up. The process is just to create a new Object for each iteration of the loop and add the object to your ArrayCollection. You can assign as many properties to the object as you need, which can be helpful for using one ArrayCollection that is displayed in multiple charts.

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="build_model()">
              <mx:Script>
                  <![CDATA[
                      import mx.collections.ArrayCollection;
                     
                      private function build_model():void {
                          var obj:Object;
                          var chartAC:ArrayCollection = new ArrayCollection();
                         
                          for (var i:uint = 0; i < 100; i++) {
                              obj = new Object();
                              obj.horizNum = 100 + 10 * i;
                              obj.vertNum = i * Math.random();
                              chartAC.addItem(obj);
                          }
                          linechart1.dataProvider = chartAC;
                      }

           

                  ]]>
              </mx:Script>
              <mx:LineChart left="10" right="10" top="54" bottom="10" id="linechart1">
                  <mx:series>
                      <mx:LineSeries displayName="Series 1" yField="vertNum" xField="horizNum"/>
                  </mx:series>
              </mx:LineChart>
              <mx:Button x="10" y="10" label="Build Model" click="build_model()"/>
          </mx:Application>

           

          Chris

          • 2. Re: Flex 3: syntax for populating ArrayCollection by loop
            100amplifiers Level 1

            Thank you so much, Chris. That is exactly what I needed to see (the ActionScript part).

             

            ..Todd