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

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

1 Answer 266 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Mitchell
Top achievements
Rank 1
Mitchell asked on 12 Dec 2013, 08:27 PM

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

1 Answer, 1 is accepted

Sort by
0
Accepted
Peshito
Telerik team
answered on 17 Dec 2013, 01:58 PM
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 >>
Tags
ChartView
Asked by
Mitchell
Top achievements
Rank 1
Answers by
Peshito
Telerik team
Share this question
or