Doing math on aggregates

13 posts, 1 answers
  1. Brian Vallelunga
    Brian Vallelunga avatar
    88 posts
    Member since:
    May 2008

    Posted 24 Apr 2012 Link to this post

    I need a couple of my aggregate fields to be calculated based on the aggregates of other columns. Is this possible?

    For example, if I have the following fields:

    Revenue, Cost, Margin (Revenue - Cost), Margin % (Margin / Revenue)

    For aggregating these fields, sum works fine on Revenue, Cost, and Margin. However, if I want to find the total Margin %, I need to do: SUM(Margin) / SUM(Revenue). Is there a way to do this with the DataSource?
  2. Answer
    Alexander Valchev
    Admin
    Alexander Valchev avatar
    2877 posts

    Posted 26 Apr 2012 Link to this post

    Hello Brian,

    I am afraid that the dataSource currently does not support such functionality out of the box. It is possible to achieve it via custom code in the template function - for example:
    { field: "Revenue", title: "Revenue", footerTemplate: "Results: #= doMath() #"}
    //...
     
    function doMath() {
        var ds = $("#grid").data("kendoGrid").dataSource;
        var aggregates = ds.aggregates();
        var margin = aggregates.Revenue.sum - aggregates.Cost.sum;
        //...
        return "your result string";
    }

    This approach will work with pre-set dataSource aggregates though (like in this example), e.g. when groupable is not enabled.

    Greetings,
    Alexander Valchev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Kendo UI is VS 2017 Ready
  4. Brian Vallelunga
    Brian Vallelunga avatar
    88 posts
    Member since:
    May 2008

    Posted 26 Apr 2012 Link to this post

    That works just fine, thanks.
  5. Nathan
    Nathan avatar
    23 posts
    Member since:
    Jul 2012

    Posted 24 Apr 2014 in reply to Alexander Valchev Link to this post

    I'm trying to achieve the same functionality.  Would you please post a complete example.  The code for the grid and the script for doing the math on the aggregate.  Also, please don't point me to the example that you mentioned in your post.  It doesn't do custom functions on the aggregate.
  6. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2877 posts

    Posted 29 Apr 2014 Link to this post

    Hello Nathan,

    Please check this sample page: http://trykendoui.telerik.com/IdEd/2
    Inside you will find an example that demonstrates how to do math with aggregates, in this particular case the Grid displays half of the total products count.

    Regards,
    Alexander Valchev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  7. Nathan
    Nathan avatar
    23 posts
    Member since:
    Jul 2012

    Posted 29 Apr 2014 in reply to Alexander Valchev Link to this post

    Hi Alexander

    This is no longer an issue for me.  Thank you for providing an example.
  8. Rajesh
    Rajesh avatar
    1 posts
    Member since:
    Mar 2014

    Posted 23 Sep 2014 in reply to Alexander Valchev Link to this post

    Hello I'm using the example provided however I have requirement to show custom aggragated values on groupfooter as well. Can you show me how it can be achieved in below example?
    http://dojo.telerik.com/IdEd/10

    (Group half Count) value for each group.
  9. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2877 posts

    Posted 26 Sep 2014 Link to this post

    Hello Rajesh,

    To me it looks like that the example you provided is working and is displaying the Group half Count value for each group. I added the count aggregate to the group footer template and verified that the displayed values are correct. Please see the attached screen shot.

    Am I missing something?

    Regards,
    Alexander Valchev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  10. Vachara
    Vachara avatar
    3 posts
    Member since:
    Jul 2013

    Posted 13 Jan 2015 Link to this post

    Hi,

    How does this example work with AngularJS?
    Can I add a custom aggregate function to $scope and use it in footer template like your "doMath()" function?

    Thank you.
  11. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2877 posts

    Posted 16 Jan 2015 Link to this post

    Hello Vachara,

    The groupFooterTemplate can also accept a function. Please try to directly assign a function to it in the Grid configuration object.

    { field: "Revenue", title: "Revenue", footerTemplate: $scope.footerTemplateConstructor }


    Regards,
    Alexander Valchev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  12. Vachara
    Vachara avatar
    3 posts
    Member since:
    Jul 2013

    Posted 16 Jan 2015 in reply to Alexander Valchev Link to this post

    Hi Alexander,

    I could see that we can use $scope to directly return a template string.
    But my point is, can we use $scope function inside said template? Something like this

    // In an AngularJS controller...
    { field: "Revenue", title: "Revenue", footerTemplate: "Results: #= $scope.doMath() #"}
     
    //...
      
    $scope.doMath() {
         // Whatever calculation regarding the grid data
        return "your result string";
    }

    Thank you.
  13. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2877 posts

    Posted 21 Jan 2015 Link to this post

    Hello Vachara,

    This is not possible. Templates are executed in the global window scope, $scope is not available in the global window scope.

    Regards,
    Alexander Valchev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  14. Vachara
    Vachara avatar
    3 posts
    Member since:
    Jul 2013

    Posted 21 Jan 2015 in reply to Alexander Valchev Link to this post

    Thank you.
Back to Top
Kendo UI is VS 2017 Ready