11 Replies Latest reply on Oct 18, 2007 8:57 AM by Mush15

    Help with creating objects for ArrayCollection

      Hi all,

      I am trying to update an ArrayCollection with Objects created in ActionScript

      In the end the array collection will provide data to a chart

      I can add the objects to my arrayCollection and access the values of the properties via text field output but the data is not Bindable

      So my text field shows the values from my AC but my chart isn't getting the data
        • 1. Re: Help with creating objects for ArrayCollection
          AaffordableWeb
          Some sample code would be helpfull.
          • 2. Re: Help with creating objects for ArrayCollection
            ctzn99 Level 1
            Instead of using an ArrayCollection full of Objects, try using an ArrayCollection full of ObjectProxys. The ObjectProxy class is specially designed to track changes to it's underlying Object class. I think this will provide you with the level of databinding that you want.

            Zach
            • 3. Re: Help with creating objects for ArrayCollection
              Level 1
              Sorry about that

              here is the code where I am creating the objects
              [Bindable]
              public var lPrime:ArrayCollection = new ArrayCollection();



              public function pedata():*{
              /// begin loop

              for(var n:int = 0; n < nPchange.length; n++){
              var value:* = nPchange[n];
              if(value == 0){value = .0001;}else{value = value;}

              tempval = -1 * value / (cMargin + value);
              tempval2 = (tempval / value);

              ylovert.push(tempval2);


              var o:Object = new Object;
              o.PriceChange= value;
              o.PriceElasticity= tempval2;

              lPrime.addItem(o);

              } // end loop
              • 4. Re: Help with creating objects for ArrayCollection
                AaffordableWeb Level 1
                In this case, ctzn99 is correct. You will need to use the ObjectProxy. Here is an example:
                IPrim.addItem(new ObjectProxy(o));

                This will create the bindings needed for o to announce changes to it's values.
                • 5. Help with creating objects for ArrayCollection
                  Level 1
                  AaffordableWeb:

                  Wrapping the Object in an ObjectProxy seems to have worked in that the properties are bound but my Chart shows nothing.

                  I have posted the code below (keep in mind its a work in progress)

                  Any help would be appreciated.

                  //////Code

                  <?xml version="1.0"?>
                  <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="handleResult(event);">
                  <mx:Script>
                  <![CDATA[
                  import mx.collections.ArrayCollection;
                  import mx.events.FlexEvent;
                  import mx.rpc.events.ResultEvent;
                  import mx.utils.ObjectUtil;
                  import mx.utils.ObjectProxy;



                  //// set price change range for the four arrays
                  [Bindable]
                  public var nPchange:Array = new Array(-12, -11, -10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0);
                  [Bindable]
                  public var pPchange:Array = new Array(12, 11, 10,9,8,7,6,5,4,3,2,1,0);



                  //// Set up temprary var
                  [Bindable]
                  public var tempval:*;
                  [Bindable]
                  public var tempval2:*;
                  [Bindable]
                  public var tempvaly:*;
                  [Bindable]
                  public var tempvalr:*;
                  [Bindable]
                  public var tempvalr2:*;
                  [Bindable]
                  public var tempvalry:*;
                  [Bindable]
                  public var ylovert:Array = new Array;
                  [Bindable]
                  public var rylovert:Array = new Array;
                  [Bindable]
                  public var ymin:*;
                  [Bindable]
                  public var testarray:Array;


                  /// declare the lPrime ac




                  // declare the rprime ac
                  [Bindable]
                  public var rPrime:ArrayCollection = new ArrayCollection();

                  // declare rylover ac
                  [Bindable]
                  public var ryLover:ArrayCollection = new ArrayCollection();

                  // declare ylover ac
                  [Bindable]
                  public var yLover:ArrayCollection = new ArrayCollection();

                  /// declare default cMargin
                  [Bindable]
                  public var cMargin:* = 80;

                  [Bindable]
                  public var lPrime:ArrayCollection;






                  public function handleResult(event:FlexEvent):void
                  {
                  pedata();
                  }

                  public function pedata():*{
                  /// begin loop
                  lPrime = new ArrayCollection();
                  for(var n:int = 0; n < nPchange.length; n++){
                  var value:* = nPchange[n];
                  if(value == 0){value = .0001;}else{value = value;}

                  tempval = -1 * value / (cMargin + value);
                  tempval2 = (tempval / value);

                  ylovert.push(tempval2);


                  var o:Object = new Object;
                  o.PriceChange= value;
                  o.PriceElasticity= tempval2;

                  lPrime.addItem(new ObjectProxy(o));


                  } // end loop



                  lprime.xField= "PriceChange";
                  lprime.yField= "PriceElasticity";
                  lprime.dataProvider = lPrime;
                  }

                  ///last brkt
                  ]]>
                  </mx:Script>



                  <mx:AreaChart id="myChart" showDataTips="true" mouseSensitivity="25" width="100%" >
                  <mx:horizontalAxis>
                  <mx:LinearAxis maximumLabelPrecision="1" title="Pricec2a" minimum="-12" maximum="12"/>
                  </mx:horizontalAxis>
                  <mx:verticalAxis>
                  <mx:LinearAxis displayName="Price Elasticity to Break Even" minimum="-1.5" maximum="-1" />
                  </mx:verticalAxis>
                  <mx:series>

                  <mx:AreaSeries id="lprime" form="curve" >
                  <mx:areaFill>
                  <mx:SolidColor color="red"/>
                  </mx:areaFill></mx:AreaSeries>

                  </mx:series>
                  </mx:AreaChart>

                  <mx:Text id="txt2" text="{lPrime.getItemAt(3).PriceElasticity}"/>
                  </mx:Application>
                  • 6. Re: Help with creating objects for ArrayCollection
                    AaffordableWeb Level 1
                    You need to set a dataProvider for the AreaChart. Looking at your code, I am guessing lPrime should be the dataProvider.
                    • 7. Re: Help with creating objects for ArrayCollection
                      Level 1
                      Actually I am setting the dataprovider on the series lprime so there is no need for a dataProvider on the chart.

                      Thanks
                      • 8. Re: Help with creating objects for ArrayCollection
                        AaffordableWeb Level 1
                        I see that now. My mistake. At the end of pedata() add:
                        lprime.invalidateData();

                        This will force the chart to recognize the new dataProvider and build the chart.
                        • 9. Re: Help with creating objects for ArrayCollection
                          Level 1
                          Aaffordablweb


                          lprime.invalidateData();


                          Didn't work.

                          I get a protected method error.

                          Any other ideas?

                          When I debug I can see the Object and values have been added to the AC I can even access them as output in Text but the chart still doesn't recognize them.
                          • 10. Re: Help with creating objects for ArrayCollection
                            AaffordableWeb Level 1
                            Probably a step backwards, but should shed some light:
                            remove this line:
                            lprime.dataProvider = lPrime;

                            modify this line:
                            <mx:AreaSeries id="lprime" form="curve" >
                            to
                            <mx:AreaSeries id="lprime" form="curve" dataProvider="{lPrime}" >

                            This should fix it, but then your series will be tied to the lPrime object. If you want to dynamically change the dataProvider, you will need to extend the AreaSeries Object with your own and call the invalidateData() method from your extended object.
                            • 11. Re: Help with creating objects for ArrayCollection
                              Level 1
                              Affordable

                              Thanks for all your help.

                              I ended up using an HTTP Service in the end.

                              I am currently working on another chart but will return to this and let you know the result