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

Custom date range GroupInterval

1 Answer 47 Views
PivotGrid
This is a migrated thread and some comments may be shown as answers.
Jeremy
Top achievements
Rank 1
Jeremy asked on 07 Apr 2014, 11:05 AM
How would you go about trying to group a certain range of years within a custom column group interval?

Wanted:
1992-2009 | 2010 | 2011 | 2012 | 2013 | 2014

Caveat: the 1992-2009 range is from the oldest date in the dataset to the oldest date with a stop at 5 years from the current year. So in 2015 the group would be 1997-2010

Current (not working) code below produces what I find unexpected:
1992-2009 | 1993 - 2009 | 1994 - 2009 etc etc

Any advice is greatly appreciated

public class OldYearGroup : IComparable, IComparable<OldYearGroup>, IEquatable<OldYearGroup>
{
    public OldYearGroup()
    {
 
    }
 
    internal OldYearGroup(int year)
    {
        this.year = year;
    }
 
    public int year
    {
        get;
        set;
    }
 
    public override string ToString()
    {
        if (year > DateTime.Now.AddYears(-5).Year)
            return string.Format(CultureInfo.InvariantCulture.NumberFormat, "{0}", this.year);
 
        return string.Format(CultureInfo.InvariantCulture.NumberFormat, "{0}-{1}", this.year, DateTime.Now.AddYears(-5).Year);
    }
 
    public override int GetHashCode()
    {
        return this.year * 7933;
    }
 
    public override bool Equals(object obj)
    {
        return this.Equals(obj as OldYearGroup);
    }
 
    public int CompareTo(object obj)
    {
        if (obj is OldYearGroup)
        {
            return this.CompareTo(obj as OldYearGroup);
        }
 
        throw new ArgumentException("Can not compare.", "obj");
    }
 
    public int CompareTo(OldYearGroup other)
    {
        if (other == null)
        {
            throw new ArgumentNullException("other");
        }
        return this.year.CompareTo(other.year);
    }
 
    public bool Equals(OldYearGroup other)
    {
        return other != null && this.year < DateTime.Now.AddYears(-5).Year;
    }
}


public class DefaultDateTimeGroupingDescription : PropertyGroupDescriptionBase
{
    public int Year { get; set; }
    public DefaultDateTimeGroupingDescription()
    {
 
    }
 
    protected override object GroupNameFromItem(object item, int level)
    {
 
        var baseValue = base.GroupNameFromItem(item, level);
         
        DateTime? year = ((DateTime?)baseValue);
        if (!year.HasValue)
            return null;
        return new OldYearGroup(year.Value.Year);
         
    }
 
    protected override void CloneOverride(Cloneable source)
    {
        var castedSource = source as DefaultDateTimeGroupingDescription;
        if (castedSource == null)
            return;
        if (castedSource.Year < this.Year)
            this.Year = castedSource.Year;
    }
 
    protected override Cloneable CreateInstanceCore()
    {
        return new DefaultDateTimeGroupingDescription();
    }
}

public class DefaultDateTimeGroupingField : PivotGridColumnField
{
    private GroupDescription _groupDescription;
 
    public override GroupDescription GroupDescription
    {
        get
        {
            if (_groupDescription== null)
            {
                _groupDescription = new DefaultDateTimeGroupingDescription();
            }
            return _groupDescription;
        }
        set
        {
            base.GroupDescription = value;
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 10 Apr 2014, 10:32 AM
Hello,

Currently the range field grouping in the PivotGrid is designed to group the data in equal intervals, for example a range of a few years or months, but in your case the requirement is to have intervals of different length (1992-2009 is 17 years long and the rest of the groups (2010, 2011 etc.) are just one year long) which is currently not supported by the range grouping feature of the control.

Regards,
Marin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
PivotGrid
Asked by
Jeremy
Top achievements
Rank 1
Answers by
Marin
Telerik team
Share this question
or