4 Replies Latest reply on Nov 16, 2009 9:35 AM by Yozef0

    Custom SummaryFunction for AdvancedDataGrid

    Yozef0 Level 1

      I have an AdvancedDataGrid that gets populated (XML > Objects > ArrayCollection).

      The Adv. DataGrid has summary rows (of GroupCollection) that gets updated (operation = either SUM or AVG depeding on a ComboBox selection).

      One of my Columns is more tricky than a simple AVG or SUM of it's rows.

       

      • I am trying to Find the SUM of the Rows ( of the group) / number of Rows + 1. The AVG operation does that, but does not add 1 to the num of rows.

       

      So I figure a SummaryFunction is the way to go. I searched for some, and found this:

      private function operationPercentChange(cursor:IViewCursor, dataField:String, operation:String):Number {
           var sumOfValues:Number = 0;
           while (!cursor.afterLast){
                sumOfValues += cursor.current["change"]; // change is the datafield in question
                trace ("cursor.current = " + cursor.current["change"]);
                cursor.moveNext();                       // I suppose this gets the next value?
           }
           return actualTotal;
      }
      

       

      So the above gives me nothing, though if I explicitly return a number, it shows it to me, so I've got the function set up correctly, now all I need is getting the rows of that column and Sum them up, and divide them by the number of rows (of that group) + 1.

       

      Another summaryFunction would return the Value of a different column's Last Row (in that group) - First Row (in that group) / First Row (in that group) * 100

       

      I've attached a snapshot which is much easier to understand.

        • 1. Re: Custom SummaryFunction for AdvancedDataGrid
          msakrejda Level 4

          You update sumOfValues but return actualTotal.

          1 person found this helpful
          • 2. Re: Custom SummaryFunction for AdvancedDataGrid
            Yozef0 Level 1

            Oh I know the function above is wrong...

            It's getting the variables that I'm having trouble with... Especially first and last of the summary childs... The graph explains it clearly I think.

             

            Thanks for your help though

            • 3. Re: Custom SummaryFunction for AdvancedDataGrid
              Sameer Bhatt Adobe Employee

              Hi Joseph,

               

              Try these summary functions -

               

              private function calculateSum(cursor:IViewCursor, dataField:String, operation:String):Number

              {

                  var sum:Number = 0;

                  var count:int = 1; // start with 1 as we need to calculate sum/(num + 1)

               

                  while (!cursor.afterLast)

                  {

                      sum += cursor.current[dataField];

                      count++;

                      cursor.moveNext();

                  }

               

                  return sum/count;

              }

               

               

              private function calculatePercent(cursor:IViewCursor, dataField:String, operation:String):Number

              {

                  var firstValue:Number = cursor.current[dataField];

               

                  var lastValue:Number = 0;

               

               

                  // move to the last value

                  while (!cursor.afterLast)

                  {

                      lastValue = cursor.current[dataField];

                      cursor.moveNext();

                  }

               

               

                  return (lastValue - firstValue) * 100 / firstValue;

              }

               

              Note: These methods needs some edge case handling.

              • 4. Re: Custom SummaryFunction for AdvancedDataGrid
                Yozef0 Level 1

                private function calculatePercent(cursor:IViewCursor, dataField:String, operation:String):Number

                {

                    var firstValue:Number = cursor.current["visits"];  // needed to get the values of a different column

                 

                    var lastValue:Number = 0;

                 

                 

                    // move to the last value

                    while (!cursor.afterLast)

                    {

                        lastValue = cursor.current["visits"];

                        cursor.moveNext();

                    }

                 

                 

                    return (lastValue - firstValue) * 100 / firstValue;

                }

                 

                The above worked perfectly... Thanks Sameer , and minor changes got the other function working:

                 

                private function AVGPercentChange(cursor:IViewCursor, dataField:String, operation:String):Number {
                            var sum:Number = 0;
                            var count:int = 0;
                            while (!cursor.afterLast) {
                                sum += Number(cursor.current[dataField]);
                                count++;
                                cursor.moveNext();
                            }

                 

                            return sum/count;     // removes the 0 valued row by reducing the num of rows
                }

                 

                Thanks Million Sameer