This is a migrated thread and some comments may be shown as answers.

[Solved] How to implement custom aggregates

1 Answer 145 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Phillip
Top achievements
Rank 1
Phillip asked on 11 Jun 2012, 05:53 PM
I have a Telerik Grid which has a footer that needs to display column sums. However, one of the colums' data types is TimeSpan, which isn't supported by Telerik's Sum aggregate. I need to useGridBoundColumnBuilder.Aggregate() to add the aggregates. Using this article I created a class for my custom aggregate, called SumAggregate, shown below. (note that this isn't finished- its taken from the article. It actually implements a totally different aggregate)

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
Telerik.Web.Mvc;

namespace
TelerikPOC.CustomAggregates
{
   
public class SumAggregate : AggregateFunction
   
{
       
private System.Collections.Generic.List<object> distinctValues;

       
/// <summary>
       
/// Initializes the current aggregate function to its initial
       
/// state ready to accumulate and merge values.
       
/// </summary>
       
/// <remarks>
       
/// This method is called every time the accumulation of values
       
/// must start over for a new subset of records from the data source.
       
/// </remarks>
       
public void Init()
       
{
           
this.distinctValues = new System.Collections.Generic.List<object>();
       
}

       
/// <summary>
       
/// Accumulates new argument values to the current aggregate function.
       
/// </summary>
       
/// <remarks>
       
/// This aggregate function accepts one argument:
       
/// number - a numeric value to accumulate to the aggregate function;
       
/// </remarks>
       
public void Accumulate(object[] values)
       
{
           
if (!distinctValues.Contains(values[0]))
           
{
                distinctValues
.Add(values[0]);
           
}
       
}

       
/// <summary>
       
/// Merges the specified aggregate function to the current one.
       
/// </summary>
       
/// <param name="Aggregate">
       
/// Specifies an aggregate function to be merged to the current one.
       
/// </param>
       
/// <remarks>
       
/// This method allows the reporting engine to merge two accumulated
       
/// subsets of the same aggregate function into a single result.
       
/// </remarks>
       
public void Merge(AggregateFunction aggregate)
       
{
           
// Accumulate the values of the specified aggregate function.
           
System.Collections.Generic.List<object> sums1 =     ((SumAggregate)aggregate).distinctValues;
           
foreach (object o in sums1)
           
{
               
this.Accumulate(new object[] { o });
           
}
       
}

       
/// <summary>
       
/// Returns the currently accumulated value of the aggregate function.
       
/// </summary>
       
/// <returns>
       
/// The currently accumulated numeric value of the aggregate function.
       
/// </returns>
       
public object GetValue()
       
{
           
return this.distinctValues.Count;
       
}
   
}
}

And here is the code for adding the aggregates. This conflicts with the code below from index.cshtml, but I wanted to include both methods of adding aggregates just to give more options on the answer. This needs to be modified to use a custom aggregate rather than a built-in one, like it is using now.

GridHelper.cs (unfinished -- will add logic to loop through the columns and such later. By the way, if anyone would like to help me out with that too, I'd be very grateful, though I'll admit I haven't tried anything yet.)

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
Telerik.Web.Mvc.UI.Fluent;
using
TelerikPOC.CustomAggregates;

namespace
TelerikPOC.Helpers
{
   
public class GridHelper
   
{
       
public static void AddAggregateToColumn(GridBoundColumnBuilder<dynamic> columnBuilder, string Aggregate)
       
{
           
switch (Aggregate)
           
{
               
case "Sum":
                   
{
                        columnBuilder
.Aggregate(aggregates => aggregates.Sum())
                           
.GroupFooterTemplate(result => "Sum:" + result.Sum)
                           
.ClientFooterTemplate("Sum: <#= Sum #>")
                           
.FooterTemplate(result => "Total: " + result.Sum);
                   
}
                   
break;
           
}
       
}
   
}
}

And then I'm using the HtmlHelper class to build/render the telerik grid, like this:

From Index.cshtml: (be sure to read the comments to the right)

@{
   
Html.Telerik()
   
.Grid(Model)
   
.Name("statisticalGrid")
   
.Columns(columns =>
   
{
        columns
.Bound(o => o.PlanID).Aggregate(something);         //This is probably going to be where
        columns
.Bound(o => o.SessionID).Aggregate(something);      //I need the help. Just not sure
        columns
.Bound(o => o.TimeSpan).Aggregate(something);       //how to reference the custom
        columns
.Bound(o => o.TimeSpanDouble).Aggregate(something); //aggregate here, in
   
})                                                             //place of `something`
   
.Sortable(sortable => sortable.Enabled(true))
   
.Filterable()
   
.Pageable(page => page.PageSize(25))
   
.Reorderable(reorder => reorder.Columns(true))
   
.Groupable(groupable => groupable.Enabled(true))
   
.ClientEvents(events => events
       
.OnColumnReorder("onReorder"))
   
.Render();
}

So I guess basically the question is how to reference my custom aggregate in telerik's Aggregate()method. And if you notice anything else I'm doing wrong, feel free to point it out :)

Edit: Just noticed that I have to implement the method CreateAggregateExpression(Expression, bool) in the SumAggregate class. Not entirely sure how to implement it, though.

1 Answer, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 14 Jun 2012, 02:36 PM
Hello Philip,

Currently custom aggregates are not supported. You can check this forum thread which uses custom binding work-around. However you should change the aggregate type since there is no equivalent to TimeSpan in JavaScript. For example you could use total Seconds/Minutes/Hours/Days or months and calculate their aggregated value.

Kind regards,
Petur Subev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
Tags
Grid
Asked by
Phillip
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Share this question
or