Week number (calendar week) on xAxis

14 posts, 1 answers
  1. Tim
    Tim avatar
    114 posts
    Member since:
    Aug 2011

    Posted 13 Mar 2012 Link to this post

    Hello.

    I am using the following xaml:
    <telerik:AxisX IsDateTime="{Binding IsDateTime}" DefaultLabelFormat="{Binding DateTimeLabelFormat}"

    Sometimes, I want to display the week number (Week 1, Week 2, ...) on the xaxis. How can something like that be achieved??? Is there a special format string? Can I somehow use a converter?

    Thanks a lot for any information on this topic!
    All the best,
    Tim.
  2. Giuseppe
    Admin
    Giuseppe avatar
    2363 posts

    Posted 16 Mar 2012 Link to this post

    Hello Tim,

    The standard format strings do not support concepts like Week, Quarter, etc. so you will need to create custom converter in order to achieve the desired appearance. Find attached a runnable sample application to get you started.

    Hope this helps.


    All the best,
    Giuseppe
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. DevCraft banner
  4. Tim
    Tim avatar
    114 posts
    Member since:
    Aug 2011

    Posted 16 Mar 2012 Link to this post

    Hello Giuseppe,

    thanks a lot for your reply! But can you compile the project for Silverlight 4? That would be great! A big thanks in advance!
    All the best,
    Tim.
  5. Tim
    Tim avatar
    114 posts
    Member since:
    Aug 2011

    Posted 16 Mar 2012 Link to this post

    Hello Giuseppe, it's me again! :) I'll try to figure it out myself. Doesn't look to hard! Please ignore my last reply! (for now :))
    Thanks again,
    Tim.
  6. Giuseppe
    Admin
    Giuseppe avatar
    2363 posts

    Posted 16 Mar 2012 Link to this post

    Hello Tim,

    Find attached the Silverlight 4 version of the application.

    Hope this helps.


    Regards,
    Giuseppe
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  7. Tim
    Tim avatar
    114 posts
    Member since:
    Aug 2011

    Posted 16 Mar 2012 Link to this post

    Giuseppe, it helped a lot! Thank you! But unfortunately I still got one big problem. I was using XCategory but as these are indexed I cannot use it but I want the same result as using XCategory. I attached two screenshots to make things clear. Although I set LabelStep to one, not all values are displayed. Or can I continue to use XCategory? How can I pass the DateTime value to the converter? Any help?
    Thanks again and a lot,
    Tim.
  8. Tim
    Tim avatar
    114 posts
    Member since:
    Aug 2011

    Posted 16 Mar 2012 Link to this post

    Another problem with XValue is that values are displayed more than once and the bars are beginning to look strange (too thin)... I've attached a screenshot... Looks ugly! :)
  9. Tim
    Tim avatar
    114 posts
    Member since:
    Aug 2011

    Posted 20 Mar 2012 Link to this post

    Nothing? If it is not possible to solve this problem I cannot use the calendar weeks. That would be very disappointing... Any help? Thanks really a lot!!! All the best, Tim.
  10. Giuseppe
    Admin
    Giuseppe avatar
    2363 posts

    Posted 21 Mar 2012 Link to this post

    Hello Tim,

    If you are using XCategory you can extract the date from the TickPoint like this:
    public class WeekFormatConverter : IValueConverter
    {
        private LabelFormatConverter fallbackConverter = new LabelFormatConverter();
     
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var tickPoint = value as TickPoint;
            if (tickPoint.IsDateTime)
            {
                DateTime date = DateTime.ParseExact(tickPoint.Label, tickPoint.LabelFormat, CultureInfo.InvariantCulture);
     
                return string.Format("Week {0}", GetWeekOfYear(date));
            }
     
            // vertical axis formatting goes here (as the implicit style we are applying to AxisLabel2D applies to both horizonta/vertical axes).
            return fallbackConverter.Convert(value, targetType, parameter, culture);
        }
     
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
     
        public static int GetWeekOfYear(DateTime dateTime)
        {
            return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
                dateTime,
                CalendarWeekRule.FirstFourDayWeek,
                CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
        }
    }

    Hope this helps.


    Greetings,
    Giuseppe
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  11. Tim
    Tim avatar
    114 posts
    Member since:
    Aug 2011

    Posted 21 Mar 2012 Link to this post

    Hi Giuseppe, that would work if one _always_ wants to display weeks. But depending on the timespan I get years back or months or weeks or days... So to use your suggestion I would need to pass the whole date as a string but also a value indicating if this should be transformed to weeks or days or months... The only solution I can think of to pass a string that contains all information needed that later gets parsed - but I really don't like that solution. Better ideas? I am very thankful for any feedback! All the best and a big thanks for your answer, Tim.
  12. Tim
    Tim avatar
    114 posts
    Member since:
    Aug 2011

    Posted 21 Mar 2012 Link to this post

    I think now I know why I didn't like my solution - it is not possible to pass all the information...So now I am even more thankfull for any hint...
  13. Tim
    Tim avatar
    114 posts
    Member since:
    Aug 2011

    Posted 21 Mar 2012 Link to this post

    I think the solution is to not use the DateTime feature. Although I have DateTime values I think for me it is best to use a property in the viewModel that just converts the DateTime value to a proper String value that then will be displayed using a categorical axis. Easy enough and without any converters... Giuseppe, a big thanks to you!
  14. Answer
    Giuseppe
    Admin
    Giuseppe avatar
    2363 posts

    Posted 26 Mar 2012 Link to this post

    Hello Tim,

    Indeed if you want to display mixed label formatting and you cannot extract the necessary information in the label converter, it would probably be better to use purely categorical axis and manipulate the categories in your view model.


    Kind regards,
    Giuseppe
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  15. Tim
    Tim avatar
    114 posts
    Member since:
    Aug 2011

    Posted 30 Mar 2012 Link to this post

    Hello Giuseppe, you are absolutely right! Now it is working like a charm! :) A big thanks for all your help! All the best, Tim.
Back to Top
DevCraft banner