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

use CustomDateTime against DateTime in a chart

1 Answer 56 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Dani
Top achievements
Rank 1
Dani asked on 28 May 2012, 10:37 AM
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?

1 Answer, 1 is accepted

Sort by
0
Rosko
Telerik team
answered on 31 May 2012, 08:27 AM
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,
Rosko
the Telerik team

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

Tags
Chart
Asked by
Dani
Top achievements
Rank 1
Answers by
Rosko
Telerik team
Share this question
or