1 Reply Latest reply on Mar 3, 2010 10:16 AM by MrFork

    Using The Summary Function in ADG

    MrFork Level 1

      Hello All,

       

      Please see my code below. Im using the advance data grid to create a summary row. The summary row currenty SUMs up the 'unqiuevists' and 'redemptions' columns and all is working well.

       

      What i want to do is then create a 3rd row called 'conversion rate' this conversion rate is a calculation between the uniquevisits and the redemptions columns. If you review the code at the bottom I have managed to create a custom summary func and that function seems to be working fine (using trace)..

       

      However, I cannot then get the calculated result back into the advance data grid, am i missing something really obvious or is what Im trying to do not possible using the summary row function?

       

      Please try and help, Ive been on this for days!

       

      Thanks

       

      <?xml version="1.0"?>
      <!-- dpcontrols/adg/SummaryGroupADGCustomSummary.mxml -->
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="createGrouping(dpFlat)">

       

          <mx:Script>
              <![CDATA[
                  import mx.collections.GroupingField;
                  import mx.collections.Grouping;
                  import mx.collections.SummaryField;
                  import mx.collections.SummaryRow;
                  import mx.collections.GroupingCollection;
                  import mx.collections.ArrayCollection;
                  import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
                  import mx.collections.IViewCursor;    
                  import mx.collections.SummaryObject;
                   
                  private var groupingCollection:GroupingCollection = new GroupingCollection();
                       
                  private function createGrouping(tmpArray:ArrayCollection):GroupingCollection
                  {
                      groupingCollection.source = tmpArray;
                     
                      var summaryRow:SummaryRow = new SummaryRow;
                     
                      var summaryField:SummaryField = new SummaryField;
                      summaryField.dataField="redemptions";
                      summaryField.operation="SUM";
                     
                      var summaryField1:SummaryField = new SummaryField;
                      summaryField1.dataField="newvisits";
                      summaryField1.operation="SUM";
                     
                      var summaryField2:SummaryField = new SummaryField;
                      summaryField2.dataField="conversionrate";
                      summaryField2.summaryFunction = summFunc;
                     
                      summaryRow.fields = [summaryField,summaryField1,summaryField2];
                      summaryRow.summaryPlacement = "group";
                      summaryRow.summaryObjectFunction = summObjFunc;
                     
                      var grouping:Grouping = new Grouping();
                      var groupingField:GroupingField = new GroupingField("site");
                      var groupingField1:GroupingField = new GroupingField("merchant");
                     
                      grouping.fields=[groupingField,groupingField1];
                      groupingField.summaries = [summaryRow];
                      groupingField1.summaries = [summaryRow];
                     
                      groupingCollection.grouping = grouping;
                      groupingCollection.refresh();
                     
                      return groupingCollection;
                  }              
                       
                 [Bindable]
                  private var dpFlat:ArrayCollection = new ArrayCollection([
                    {site:"BBVA", merchant:"TALLINK", Offer:"10% Off", newvisits:38865, redemptions:40000},
                    {site:"BBVA", merchant:"LIVEIT", Offer:"10% Off", newvisits:29885, redemptions:30000}, 
                    {site:"CartaSi", merchant:"Central California", Offer:"10% Off", newvisits:29134, redemptions:30000}, 
                    {site:"CartaSi", merchant:"Nevada", Offer:"10% Off", newvisits:52888, redemptions:45000}, 
                    {site:"BarclayCard", merchant:"Northern California", Offer:"10% Off", newvisits:38805, redemptions:40000},
                    {site:"BarclayCard", merchant:"Northern California", Offer:"10% Off", newvisits:55498, redemptions:40000}, 
                    {site:"Nordea", merchant:"Southern California", Offer:"10% Off", newvisits:44985, redemptions:45000},
                    {site:"Nordea", merchant:"Southern California", Offer:"10% Off", newvisits:44913, redemptions:45000},
                    {site:"BBVA", merchant:"TALLINK", Offer:"10% Off", newvisits:38865, redemptions:40000},
                    {site:"BBVA", merchant:"LIVEIT", Offer:"10% Off", newvisits:29885, redemptions:30000}, 
                    {site:"CartaSi", merchant:"Central California", Offer:"10% Off", newvisits:29134, redemptions:30000}, 
                    {site:"CartaSi", merchant:"Nevada", Offer:"10% Off", newvisits:52888, redemptions:45000}, 
                    {site:"BarclayCard", merchant:"Northern California", Offer:"10% Off", newvisits:38805, redemptions:40000},
                    {site:"BarclayCard", merchant:"Northern California", Offer:"10% Off", newvisits:55498, redemptions:40000}, 
                    {site:"Nordea", merchant:"Southern California", Offer:"10% Off", newvisits:44985, redemptions:45000},
                    {site:"Nordea", merchant:"Southern California", Offer:"10% Off", newvisits:44913, redemptions:45000},
                  ]);
             
                
                  private function summFunc(cursor:IViewCursor, dataField:String,
                      operation:String):Number {
                     
                      var conversionrate:Number = 0;
                      conversionrate +=  (cursor.current["redemptions"] / cursor.current["newvisits"]) * 100
                     
                      trace (dataField)
                
                      trace (conversionrate);      
                      return conversionrate;
                  }
                 
                   private function summObjFunc():SummaryObject {
                 
                      // Define the object containing the summary data.
                      var obj:SummaryObject = new SummaryObject();
                      // Add a field containing a value for the Territory_Rep column.
                      obj.conversionrate2 = "Alternating Reps";
                     
                      return obj;
                   }
                 
            ]]>
          </mx:Script>
          <mx:DataGrid id="test" dataProvider="{dpFlat}">
            
          </mx:DataGrid>

       

          <mx:AdvancedDataGrid id="myADG"  width="100%" height="100%"  dataProvider="{createGrouping(dpFlat)}">       
              <mx:columns>
                  <mx:AdvancedDataGridColumn dataField="site"/>
                  <mx:AdvancedDataGridColumn dataField="offer"/>
                  <mx:AdvancedDataGridColumn dataField="newvisits"/>
                  <mx:AdvancedDataGridColumn dataField="redemptions"/>
                  <mx:AdvancedDataGridColumn dataField="conversionrate"/>
                  <mx:AdvancedDataGridColumn dataField="conversionrate2"/>
              </mx:columns>
         </mx:AdvancedDataGrid>
          <mx:Button label="Button" click="groupingCollection.refresh()"/>
      </mx:Application>

        • 1. Re: Using The Summary Function in ADG
          MrFork Level 1

          For those who are interested this is the corrected custom summary function -

           

          private function summFunc(cursor:IViewCursor, dataField:String,
                          operation:String):Number {
                         
                          var totalNewVisits:Number = 0
                          var totalNewRedemptions:Number = 0
                         
                          var tmpVisits:Number = 0;
                          var tmpRedemptions:Number = 0;      
                         
                          while (!cursor.afterLast)
                          {
                              tmpVisits = cursor.current["newvisits"];
                              totalNewVisits += tmpVisits;
                              tmpRedemptions = cursor.current["redemptions"];
                              totalNewRedemptions += tmpRedemptions;
                             
                              cursor.moveNext()
                          }
                          var conversionrate:Number = 0;
                          conversionrate =  (totalNewRedemptions / totalNewVisits) * 100
                        
                          return conversionrate;
                      }