Dates and Aggregation

6 posts, 1 answers
  1. Neil Alderson
    Neil Alderson avatar
    4 posts
    Member since:
    Jan 2010

    Posted 15 Jan 2010 Link to this post

    Hi Telerik,

    We recently purchased your Silverlight controls for one of our new projects. I'm trying to something a little complicated with the chart control but I'm not sure how to approach it. Basically I have a collection of items like so:

    Date                            Category
    01/01/2010 00:00        Category1
    01/01/2010 01:00        Category2
    02/01/2010 06:00        Category2
    01/01/2010 10:00        Category1
    02/01/2010 20:00        Category1

    What I would like to do is have a bar chart, one bar per Category
    DateTime along the X-Axis
    Count of Categories per day along the Y-Axis.

    So far I've managed to use your grouping function and Count aggregator to achieve a count of categories over time, however what I'm struggling to achieve is aggregating the count per day, instead I have a count per exact datetime.

    Please could you give me some hints as to how I can manipulate the data in the way I would like?

    Kind Regards,
    Neil
  2. Answer
    Vladimir Milev
    Admin
    Vladimir Milev avatar
    1061 posts

    Posted 20 Jan 2010 Link to this post

    Hi Neil Alderson,

    Unfortunately you cannot do this out-of-the-box with the chart, however, there is an easy work-around. You can use LINQ to project your data and round it to the day.

    Consider this sample code:
    public class BusinessObject
    {
        public string Category { get; set; }
        public DateTime Date { get; set; }
    }
    DateTime now = DateTime.Now;
    List<BusinessObject> list = new List<BusinessObject>();
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            list.Add(new BusinessObject() { Category = "A", Date = now });
            now = now.AddMinutes(1);
        }
        now = now.AddDays(1);
    }
    var result = from item in list select new BusinessObject() { Category = item.Category, Date = new DateTime(item.Date.Year, item.Date.Month, item.Date.Day) };

    Hope this helps.

    Greetings,
    Vladimir Milev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Neil Alderson
    Neil Alderson avatar
    4 posts
    Member since:
    Jan 2010

    Posted 21 Jan 2010 Link to this post

    Thanks for your response. Actually this is how I ended up solving this problem, so I've marked it as the answer.

    Neil
  4. Tim
    Tim avatar
    87 posts
    Member since:
    Aug 2008

    Posted 16 Mar 2010 Link to this post

    I too am running into this situation where the end-user can select how they want the time (x-axis) to be grouped (Yearly, Monthly, etc).  It would be nice if the ChartGroupDescriptor supported a custom function that could be used to return a value.  This would give almost the same functionality as an "ItemDataBind" event.

    <telerikCharting:SeriesMapping.GroupingSettings> 
        <telerikCharting:GroupingSettings> 
            <telerikCharting:GroupingSettings.GroupDescriptors> 
                <telerikCharting:ChartGroupDescriptor MemberFunction="FormatDate(#X)"/> 
                <telerikCharting:ChartGroupDescriptor Member="Region"/> 
            </telerikCharting:GroupingSettings.GroupDescriptors> 
        </telerikCharting:GroupingSettings> 
    </telerikCharting:SeriesMapping.GroupingSettings> 

    So in the example above, my code (either in Code Behind or in VM) would return "DateTime.Year" or whatever it needed to depending on what the user selected.
  5. Vladimir Milev
    Admin
    Vladimir Milev avatar
    1061 posts

    Posted 18 Mar 2010 Link to this post

    Hello Tim,

    That's an excellent idea! We should definitely consider this approach. What kinds of time intervals would you like to see included? Day, Week, Month, Year?

    All the best,
    Vladimir Milev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  6. Tim
    Tim avatar
    87 posts
    Member since:
    Aug 2008

    Posted 18 Mar 2010 Link to this post

    Well, what I meant was that the developer could create a custom function in their code-behind or View Model that the GroupDescriptor would call passing in any arguments needed (in my above example I was passing in #X to get the information about the data in the x-axis).  But, if the chart somehow just supported aggregated Dates by Hour, Day, Week, Month, Quarter, Year, etc, I guess that would be great!  At this time, using the charts with Dates is very intensive because I have a flat datasource of data with a Date and I am writing several LINQ queries just to "group" the data first before passing it to the Chart.
Back to Top