How do I implement a Telerik Custom Aggregate?

Phillip Schmidt picture Phillip Schmidt · Jun 11, 2012 · Viewed 8.2k times · Source

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 use GridBoundColumnBuilder.Aggregate() to add the aggregates. 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 :)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)

SumAggregate.cs

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.

Last Edit: I use a custom column builder method to build out the columms, so I'm not sure exactly how to do the formatting here. I was able to format the sum, but not the rest of the column, since outside the context of the telerik call I don't have access to the item variable. Here is basically what my column building logic is like:

In the telerik code,

.Columns(a => GridHelper.GenerateColumns(a, Model.SelectedReport))

And generate columns looks something like:

public static void GenerateColumns(GridColumnFactory<dynamic> columnFactory, Company.Project.Data.Entity.Report reportStructure)
        {
            foreach (var columnLayout in reportStructure.ReportCols.OrderBy(o => o.ColumnSequence))
            {
                GridBoundColumnBuilder<dynamic> columnBuilder = columnFactory.Bound(columnLayout.ColumnType);
                //do other stuff here (add aggregates, formatting, etc)
            }

So how would I do the formatting in this context?

Answer

Daniel picture Daniel · Jun 12, 2012

One thing you could do is add another column that represents the TimeSpan in "Days". Then you would not have to use custom aggregates.

Model:

public List<Objects.Temp> GetTemp()
{
  List<Objects.Temp> ltemp = new List<Objects.Temp>();
  System.Random r = new Random();
  Objects.Temp t = new Objects.Temp();
  t.Name = "One";
  t.start = DateTime.Now;
  t.Value = r.NextDouble();
  t.ts = DateTime.Today.AddDays(25) - t.start;
  t.tsDays = t.ts.Days;
  ltemp.Add(t);
  t = new Objects.Temp();
  t.Name = "Two";
  t.start = DateTime.Now;
  t.Value = r.NextDouble();
  t.ts = DateTime.Today.AddDays(15) - t.start;
  t.tsDays = t.ts.Days;
  ltemp.Add(t);
  t = new Objects.Temp();
  t.Name = "Three";
  t.start = DateTime.Now;
  t.Value = r.NextDouble();
  t.ts = DateTime.Today.AddDays(55) - t.start;
  t.tsDays = t.ts.Days;
  ltemp.Add(t);

  return ltemp;
}

View:

@(Html.Telerik().Grid(Model)
  .Name("Grid")
  .Columns(columns =>
  {
    columns.Bound(o => o.Name)
            .Aggregate(aggregates => aggregates.Count())
            .FooterTemplate(@<text>Total Count: @item.Count</text>)
            .GroupFooterTemplate(@<text>Count: @item.Count</text>);

    columns.Bound(o => o.start)
            .Width(200)
            .Aggregate(aggreages => aggreages.Max())
      //.Format("{0:c}")
            .FooterTemplate(@<text>Max: @item.Max</text>)
            .GroupFooterTemplate(@<text>Max: @item.Max</text>);

    columns.Bound(o => o.Value)
            .Width(200)
            .Aggregate(aggregates => aggregates.Average())
            .FooterTemplate(@<text>Average: @item.Average</text>)
            .GroupFooterTemplate(@<text>Average: @item.Average</text>);

    columns.Bound(o => o.ts)
            .Width(100)
            .Aggregate(aggregates => aggregates.Count().Min().Max())
            .FooterTemplate(
            @<text>
                <div>Min: @item.Min</div>
                <div>Max: @item.Max</div>
            </text>)
            .GroupHeaderTemplate(@<text>@item.Title: @item.Key (Count: @item.Count)</text>);

    columns.Bound(o => o.tsDays)
          .Width(100)
          .Aggregate(aggregates => aggregates.Sum())
          .FooterTemplate(
          @<text>
                <div>Sum: @item.Sum Days</div>
            </text>)
          .GroupHeaderTemplate(@<text>@item.Title: @item.Key (Sum: @item.Sum)</text>);
  })
    .Sortable()

)

You get something that looks like this:

enter image description here

Version 2

I updated my answer so that there is only one TimeSpan column with sums the TimeSpan and displays it in TimeSpan format. The data in the column is really TimeSpan.TotalMilliseconds, but it displays as TimeSpan by using a template and formatting a TimeSpan using the TimeSpan.FromMilliseconds method. I think this way doing it is easier than creating a custom aggregate class if that is even possible with MVC extensions.

Model:

public class Temp
{
  public string Name { get; set; }
  public DateTime Start { get; set; }
  public double Value { get; set; }
  public TimeSpan ts { get; set; }
  public double tsMilliseconds { get; set; }
}


  List<Objects.Temp> ltemp = new List<Objects.Temp>();
  System.Random r = new Random();
  Objects.Temp t = new Objects.Temp();
  t.Name = "One";
  t.Start = DateTime.Now;
  t.Value = r.NextDouble();
  t.ts = DateTime.Today.AddDays(25) - t.Start;
  t.tsMilliseconds = t.ts.TotalMilliseconds;
  ltemp.Add(t);
  t = new Objects.Temp();
  t.Name = "Two";
  t.Start = DateTime.Now;
  t.Value = r.NextDouble();
  t.ts = DateTime.Today.AddDays(15) - t.Start;
  t.tsMilliseconds = t.ts.TotalMilliseconds;
  ltemp.Add(t);
  t = new Objects.Temp();
  t.Name = "Three";
  t.Start = DateTime.Now;
  t.Value = r.NextDouble();
  t.ts = DateTime.Today.AddDays(55) - t.Start;
  t.tsMilliseconds = t.ts.TotalMilliseconds;
  ltemp.Add(t);

View:

@(Html.Telerik().Grid(Model)
  .Name("Grid")
  .Columns(columns =>
  {
    columns.Bound(o => o.Name)
            .Aggregate(aggregates => aggregates.Count())
            .FooterTemplate(@<text>Total Count: @item.Count</text>)
            .GroupFooterTemplate(@<text>Count: @item.Count</text>);

    columns.Bound(o => o.Start)
            .Template(@<text>@item.Start.ToShortDateString()</text>)
            .Aggregate(aggreages => aggreages.Max())
            .FooterTemplate(@<text>Max: @item.Max.Format("{0:d}")</text>)
            .GroupHeaderTemplate(@<text>Max: @item.Max.Format("{0:d}")</text>)
            .GroupFooterTemplate(@<text>Max: @item.Max.Format("{0:d}")</text>);

    columns.Bound(o => o.Value)
            .Width(200)
            .Aggregate(aggregates => aggregates.Average())
            .FooterTemplate(@<text>Average: @item.Average</text>)
            .GroupFooterTemplate(@<text>Average: @item.Average</text>);

    columns.Bound(o => o.tsMilliseconds)
          .Width(100)
          .Aggregate(aggregates => aggregates.Sum())
          .Template(@<text>@TimeSpan.FromMilliseconds(@item.tsMilliseconds)</text>)
          .Title("TimeSpan")
          .FooterTemplate(
          @<text>
                <div>Sum: @TimeSpan.FromMilliseconds( @Convert.ToDouble( @item.Sum.Value.ToString() ) ) </div>
            </text>)
          //header if you group by TimeSpan
          .GroupHeaderTemplate(@<text>@item.Title: @item.Key (Sum: @TimeSpan.FromMilliseconds(@Convert.ToDouble(@item.Sum.Value.ToString())))</text>)
          //footer for grouping
          .GroupFooterTemplate(@<text>Sum: @TimeSpan.FromMilliseconds(@Convert.ToDouble(@item.Sum.Value.ToString()))</text>);
  })
    .Sortable()
    .Groupable(settings => settings.Groups(groups => groups.Add(o => o.Start)))
) 

Without grouping produces this:

TimeSpan Aggregate

With grouping produces this:

With Grouping