use CustomDateTime against DateTime in a chart

2 posts, 0 answers
  1. Dani
    Dani avatar
    1 posts
    Member since:
    Jan 2012

    Posted 28 May 2012 Link to this post

    Hi. there is not problem with using DateTime and setting format for it in a chart. for example the "dd-yyyy" format will convert it correctly. but i want to use CustomDateTime and assign it to value of an axis. the CustomDateTime is the following:

    public struct CustomDateTime : IFormattable
        private DateTime dateTime;
        public CustomDateTime(int year, int month, int day)
            this.dateTime = new DateTime(year, month, day);
               public string ToString(string format, IFormatProvider provider)
                          return this.dateTime.ToString(format, provider);
        public string ToString(IFormatProvider provider)
            return this.dateTime.ToString(provider);

    and using it:

    RadChart1.DefaultView.ChartArea.AxisX.DefaultLabelFormat = "MMMM yyyy";
    testMapping.ItemMappings.Add(new ItemMapping("CustomDateTime", DataPointMember.XValue));

    but it doesn't work. what is wrong in the above codes?
  2. Rosko
    Rosko avatar
    137 posts

    Posted 31 May 2012 Link to this post

    Hi Dani,

    We tested the code that you provided. The issue can be solved in more than one way. The DataPointMember XValue requires a numeric type like int or double. If you use the DateTime class, it has a method ToOADate(), which returns a floating-point value. RadChart can convert that number to the corresponding date, but you need to set DefaultView.ChartArea.AxisX.IsDateTime property to true. The other way is to use the DataPointMember XCategory instead of XValue. In both cases, If you want the DefaultView.ChartArea.AxisX.DefaultLabelFormat to take effect, you have to set DefaultView.ChartArea.AxisX.IsDateTime property to true.

    All the best,
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top