16 Replies Latest reply on Jun 22, 2007 4:48 PM by aprildavid

    How to create array collection

    aprildavid

      Following is the data format I recieved:(yy/mm/dd, value)
      2007:12:3, 123
      2007:12:5,1124
      ....

      After I got following data I want to draw a line chart.

      According to sample code provided by Adobe, I need to create a arrayCollection, like
      [Bindable]
      public var aapl:ArrayCollection = new ArrayCollection([
      {date: "2005, 8, 1", close: 42.71},
      {date: "2005, 8, 2", close: 42.99},
      {date: "2005, 8, 3", close: 44}
      ]);

      How can I do it?

      A lot of thanks!

      April



        • 1. Re: How to create array collection
          John_Milton
          What format is the data in now?

          Depending on the format it may be bindable directly rather than needing to be converted to an ArrayCollection eg if it's already XML...

          John
          • 2. Re: How to create array collection
            aprildavid Level 1
            It is text format, string. I really don't know how to convert it into ArrayCollection
            • 3. Re: How to create array collection
              ntsiii Level 3
              String delimited how?

              How are you getting the data?

              What do you see if you trace() the data?

              We need much more information.

              Tracy
              • 4. How to create array collection
                JoeADSK
                April,

                You are hardcoding the array collection there, and if all you have is 4 dates that is great, but if you have more you want to make a loop.
                you can do something like

                loop( c < data.length ) //psudo code
                aapl.source.push( [date: data.date (depending if its XML or CF), close: data.value])

                i would suggest searching the web for examples of formatting dates with actionscript. Actionscript 3.0 provides very handy date functions for turning strings into dates and vice versa.
                there are a couple of examples out there that actually change the format, and i have an example in my code i can dig out if you would like.

                -Joe
                • 5. Re: How to create array collection
                  aprildavid Level 1


                  Following is the xml file I received:
                  <?xml version="1.0" encoding="utf-8" ?>
                  - <resultset>
                  <about>Test Query</about>
                  - <chartformat>
                  <xaxis>ts</xaxis>
                  <yaxis>value</yaxis>
                  <displayName>Line chart performance</displayName>
                  <title>Line Chart</title>
                  </chartformat>
                  <row>1000:1:1,1000,1001:1:1,1001,1002:1:1,1002,1003:1:1,1003,1004:1:1,1004,1005:1:1,1005, 1006:1:1,1006,1007:1:1,1007,1008:1:1,1008,1009:1:1,1009,1010:1:1,1010</row>
                  </resultset>

                  Line data is in <row>, in the format of time:value.

                  I got it in as String. Then How I can generate a ArrayCollection in the format of:
                  public var aapl:ArrayCollection = new ArrayCollection([
                  {date: "2005, 8, 1", close: 42.71},
                  {date: "2005, 8, 2", close: 42.99},
                  {date: "2005, 8, 3", close: 44}
                  ]);
                  Date and close should be dynamically generated based on <xais> and <yaxis> in the XML file I received.

                  A lot of thanks for the guys reply to my question.
                  • 6. Re: How to create array collection
                    ntsiii Level 3
                    You can use split(",") to put the string in an array, then you can loop over the array with for()

                    Tracy
                    • 7. Re: How to create array collection
                      aprildavid Level 1
                      I used split and put it into array,

                      what I got is( ["1000:1:1","1000","1001:1:1","1001",...]),

                      But I need Arrayc Collection like({ts: "1000:1:1", value: 1000},
                      ts: 1001:1:1, value: 1001},)

                      "ts" and "value" is from xml file.

                      I am new to flex, your help is greatly apprecaited

                      • 8. Re: How to create array collection
                        aprildavid Level 1
                        Please, someone please help me.
                        • 9. Re: How to create array collection
                          AndyWW
                          I have only just looked at this thread, and so please forgive me if I am misinterpreting your question! It appears that you have the values for the data in pairs separated by a comma, and that the two values in each pair are also separated by a comma. You wish to convert the pair "1000:1:1","1000" to "2005,8,1",42.71 ... is that correct? How can the two values ("1000:1:1" and "1000") translate to the 'date' and 'close' value that you have identified? That is, how does 1000:1:1 become the date 2005/8/1, and "1000" the value 42.71? If you can tell me this then I can try to give you the code to translate the XML element value into the collection.

                          Andy


                          Andy
                          • 10. Re: How to create array collection
                            aprildavid Level 1
                            Andy,

                            thank you for the reply.

                            1001:1:1 is in year:mm:day format. It is fake date now.I need to translate the string i got into a array collection.
                            But it doesn't work. code is in following:

                            private function displaylineCommachart(chartID:int, panel:Panel, chart:LineChart, result:Object):void
                            {
                            var lineChart:LineChart = chart;
                            var dataString:String = result.resultset.row;
                            var chartFormat:Object = result.resultset.chartformat;
                            var dataArray:ArrayCollection=new ArrayCollection();
                            //var dataArray:ICollectionView =new ArrayCollection();
                            var axis:DateTimeAxis = new DateTimeAxis();
                            axis.parseFunction=myParseFunction2;
                            axis.dataUnits = "days";
                            axis.baseAtZero=false;

                            var series:LineSeries=new LineSeries();
                            series.xField = chartFormat.xaxis;
                            series.yField = chartFormat.yaxis;
                            series.displayName=chartFormat.yaxis;

                            var a:Array =dataString.split(",");

                            for(var i:int=0;i<a.length;i=i+2)
                            {
                            var time:String=chartFormat.xaxis;
                            var value:String=chartFormat.yaxis;
                            dataArray.addItem({time:a , value:a[i+1]});
                            }

                            lineChart.dataProvider = dataArray;
                            lineChart.horizontalAxis = axis;

                            try
                            {
                            var chartIndex:int;

                            chartIndex = panel.getChildIndex(lineChart);
                            }
                            catch(e:ArgumentError) {
                            lineChart.percentHeight=100;
                            lineChart.percentWidth=100;
                            lineChart.showDataTips=true;

                            panel.addChildAt(lineChart,0);
                            panel.title = chartFormat.title;
                            }
                            • 11. Re: How to create array collection
                              AndyWW Level 1
                              I have not used chart, but I am curious about the "for" loop that you are using to populate the dataArray Collection. Specifically, what are you expecting the addItem() to actually add to the collection? I am not certain why you are defining and assigning values to the variables "time" and "value" ... you do not use these variables anywhere. When you add the items to dataArray you are creating objects that have the property names "time" and "value" (you are not creating objects with the property names that are taken from the values in chartFormat.xaxis and yaxis). If you try the following:

                              var ac: ArrayCollection = new ArrayCollection();
                              var time: String = "t";
                              var value: String = "v";
                              ac.addItem({time: "t1", value: "v1"});
                              for (var s: String in ac.getItemAt(0))
                              {
                              Alert.show(s);
                              }

                              you will see that the properties in the object added to the collection are named "time" and "value", and not "t" and "v".
                              • 12. Re: How to create array collection
                                aprildavid Level 1
                                That is exactly what I have to solve.

                                When I create ac.addItem({time: "t1", value: "v1"});
                                how can I dynamically define "time" and "value" based on XML file I received.

                                Thanks, andy!
                                • 13. Re: How to create array collection
                                  AndyWW Level 1
                                  I am not very familiar with AS (I work mostly with Java), but I think you can do this by instantiating an object for each entry in your target collection. You can dynamically add properties to objects, of course, and so I think you could use the xaxis and yaxis fields from your chartFormat object as the field names in a new object. There may very well be a much more appropriate way to do this! I am thinking of something along the lines of:

                                  var ac: ArrayCollection = new ArrayCollection();
                                  var acEntry: Object = new Object();
                                  var time: String = "xaxis";
                                  var value: String = "yaxis";
                                  acEntry[time] = 1;
                                  acEntry[value] = 2;
                                  ac.addItem(acEntry);
                                  for (var s: String in ac.getItemAt(0))
                                  {
                                  Alert.show(s);
                                  }

                                  This will show that the names of the properties in the collection entry are "xaxis" and "yaxis". If this works for you then you just need to create an Object for every two entries in your split strings. You only need to get the field names once, of course (you don't need to get them in every iteration of the loop). I am not certain what you need to do in the way of garbage collection ... I don't know whether you need to "delete" the objects when you're done with them.

                                  Andy
                                  • 14. Re: How to create array collection
                                    aprildavid Level 1
                                    Andy,

                                    Thank you for the answer very much.

                                    I am java programmer too. New to flex, 1 week experience.
                                    Flex is fun, but performence is not very good? what do you think?
                                    • 15. Re: How to create array collection
                                      AndyWW Level 1
                                      April,

                                      I am not using Action Script much ... my partner is doing the Flex side. I am just creating the POJOs and sending them to him in response to an RMI request through RTMP.

                                      Did you get the chart to work?

                                      Andy
                                      • 16. Re: How to create array collection
                                        aprildavid Level 1
                                        yes. It seems line chart need objectProxy, I don't know why. How about your partner's though about the performance of flex?

                                        Code:
                                        private function displaylineCommachart(chartID:int, panel:Panel, chart:LineChart, result:Object):void
                                        {
                                        var lineChart:LineChart = chart;
                                        var dataString:String = result.resultset.row;
                                        var chartFormat:Object = result.resultset.chartformat;
                                        var dataArray:ArrayCollection=new ArrayCollection();
                                        var axis:DateTimeAxis = new DateTimeAxis();
                                        axis.parseFunction=myParseFunction2;
                                        axis.dataUnits = "days";
                                        axis.baseAtZero=true;

                                        var series:LineSeries=new LineSeries();
                                        series.xField = chartFormat.xaxis;
                                        series.yField = chartFormat.yaxis;
                                        series.displayName=chartFormat.yaxis;

                                        var a:Array =dataString.split(",");

                                        for(var i:int=0;i<a.length;i=i+2)
                                        {
                                        var acEntry: Object = new Object();
                                        var time: String = series.xField;
                                        var value: String = series.yField;
                                        acEntry[time] = a ;
                                        acEntry[value] =10;
                                        var p:ObjectProxy = new ObjectProxy(acEntry);

                                        dataArray.addItem(p);
                                        }

                                        var seriesArray:Array = new Array();
                                        seriesArray.push(series);

                                        lineChart.dataProvider = dataArray;
                                        lineChart.horizontalAxis = axis;
                                        lineChart.series = seriesArray;

                                        try
                                        {
                                        var chartIndex:int;

                                        chartIndex = panel.getChildIndex(lineChart);
                                        }
                                        catch(e:ArgumentError) {
                                        lineChart.percentHeight=100;
                                        lineChart.percentWidth=100;
                                        lineChart.showDataTips=true;

                                        panel.addChildAt(lineChart,0);
                                        panel.title = chartFormat.title;
                                        }