Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
105
Changing Default Date Hierarchy
posted

Hu,

Is is possible to change the default date hierarchy from what it is right now: Year->Half->Quarter->Month->Day.

I would like to have a hierarchy of year->month.

Love this control btw. By far the best pivot grid out there!

Thanks,

Alex Schievink

 

 

  • 8831
    posted

    Hello Alex,

    You can override the default hierarchy descriptor used for DataTime properties with your one. You have to define one new hierarchy descriptor that applies to all of the properties with type of DataTime or if you need you can define new one only for the single property:

     

    HierarchyDescriptor<ProductData> dateTimeTodayDescriptor =

    new HierarchyDescriptor<ProductData>(pd => pd.Today);

    dateTimeTodayDescriptor.AddLevel(pd => "Today", "All Dates");

    dateTimeTodayDescriptor.AddLevel(pd => pd.Today.SemesterWithYear(), "Semesters");

    dateTimeTodayDescriptor.AddLevel(pd => pd.Today.MonthShort(), "Months");

     

    Here I assume that the flat data source contains items with type of ProductData and ProductData exposes property named Today.

     

    HierarchyDescriptor dateTimeDataDescriptor =

    new HierarchyDescriptor { AppliesToPropertiesOfType = typeof(DateTime) };

     

    dateTimeDataDescriptor.AddLevel<DateTime>(dataTime => "All Dates", "All Dates");

    dateTimeDataDescriptor.AddLevel<DateTime>(date => date.Year, "Years");

    dateTimeDataDescriptor.AddLevel<DateTime>(date => date.SemesterWithYear(), "Semesters");

    dateTimeDataDescriptor.AddLevel<DateTime>(date => date.MonthShort(), "Months");

    dateTimeDataDescriptor.AddLevel<DateTime>(dataTime => dataTime.ToShortDateString(), "Dates");

     

    flatDataSource.AddHierarchyDescriptor(dateTimeTodayDescriptor);
    flatDataSource.AddHierarchyDescriptor(dateTimeDataDescriptor);

    flatDataSource.ItemsSource = yourItemsCollection;

     

     

    The first (the generic) descriptor will be used for the dimension/hierarchy built over ProductData.Today property only. When dimensions are built over all other properties with type of DataTime will be used the second one.

     

    Just remove the unnecessary levels. Note that here you can call extension or instance methods inside the lambda and the result will form the value used when data is visualized.

     

    public static class Extenssions

    {

        public static string SemesterWithYear(this DateTime dateTime)

        {

            return dateTime.SemesterShort() + " " + dateTime.Year;

        }

    }

     

    There are a few DateTime extension methods like SemesterShort(this DataTime) already defined inside Infragistics.Olap.FlatData namespace.

    Also you have to add the descriptors before items source is set.

     

    Best regards.

    Plamen.