DateTimeContinuousAxis and custom axis label formatting for Week number (and Quarter)

2 posts, 1 answers
  1. Mitchell
    Mitchell avatar
    45 posts
    Member since:
    Mar 2013

    Posted 12 Dec 2013 Link to this post

    I have a RadCartesianChart chart with a dynamic number of date/double series.  So, I'm using a ChartSeriesProvider and programmatically generating the CategoricalSeriesDescriptor instances for the series.  The x values for the data are of type DateTime, but the query results are actually grouped by the actual query already so that they are per year, month, etc.  So, if the query was grouped by month, I would have a single point to plot where the Datetime value is on the first of the month.  For most groupings, I can use the axis.LabelFormat property to provide an x axis label that is appropriate to the grouping.  Ex. for month, I'd provide something like "MMM, yyyy". However, there is no format specifier for week number (of the year) or quarter number for me to generate labels like "Week 12, 2013".  Is there a way for me to generate that kind of label? Maybe some other method besides using Axis.LabelFormat?


    Thanks - Mitch

  2. Answer
    Peshito
    Admin
    Peshito avatar
    497 posts

    Posted 17 Dec 2013 Link to this post

    Hi Mitch,

    This is not supported out of the box. However using a converter for your DateTimeCategoricalAxis.LabelTemplate will help you solve this issue. This is how it can be done:
    <telerik:RadCartesianChart.HorizontalAxis>
         <telerik:DateTimeCategoricalAxis >
             <telerik:DateTimeCategoricalAxis.LabelTemplate>
                 <DataTemplate>
                     <TextBlock Text="{Binding Converter={StaticResource ResourceKey=converter}}" />
    and here is the converter:
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        DateTime newValue = (DateTime)value;
        var weekNumber = System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(newValue, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday);
        var year = System.Globalization.CultureInfo.CurrentCulture.Calendar.GetYear(newValue);
        return string.Format("Week {0}, {1}", weekNumber, year);
    }

    Please have in mind that getting of the week of year is specific in the different cultures.

    Hope this helps.

    Regards,
    Peshito
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top