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

Date and Time on Xaxis?

2 Answers 179 Views
Chart (obsolete as of Q1 2013)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Edward
Top achievements
Rank 1
Edward asked on 08 Oct 2009, 07:40 PM
Is it possible to label the xaxis w/date and time?

i see
.PlotArea.XAxis.Appearance.ValueFormat = Telerik.Charting.Styles.ChartValueFormat.ShortDate;
and
.PlotArea.XAxis.Appearance.ValueFormat = Telerik.Charting.Styles.ChartValueFormat.ShortTime;

I have hourly-ish data over several-ish days...


2 Answers, 1 is accepted

Sort by
0
Giuseppe
Telerik team
answered on 13 Oct 2009, 01:01 PM
Hi Edward,

Here is a sample code snippet that demonstrates databinding RadChart to DateTime data:

private DateTime timeValue = new DateTime(2008, 4, 30, 8, 0, 0);
 
public Form1()
{
    InitializeComponent();
 
    ChartSeries series = new ChartSeries();
    series.Type = ChartSeriesType.Line;
    series.DataYColumn = "Value";
    series.DataXColumn = "Time";
 
    radChart1.Series.Add(series);
    radChart1.PlotArea.XAxis.Appearance.ValueFormat = Telerik.Charting.Styles.ChartValueFormat.ShortTime;
    radChart1.PlotArea.XAxis.Appearance.LabelAppearance.RotationAngle = 45;
 
    radChart1.PlotArea.XAxis.IsZeroBased = false;
    radChart1.PlotArea.XAxis.AutoScale = false;
 
    const double hourStep = 1 / 24.0;
    double startTime = timeValue.AddHours(-2).ToOADate();
    double endTime = timeValue.AddHours(6).ToOADate();
    radChart1.PlotArea.XAxis.AddRange(startTime, endTime, hourStep);
 
    radChart1.DataSource = CreateTimeSource();
    radChart1.DataBind();
}
 
private DataTable CreateTimeSource()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Time", typeof(double));
    dt.Columns.Add("Value", typeof(int));
 
    DataRow dr = dt.NewRow();
    dr[0] = timeValue.ToOADate();
    dr[1] = 1;
    dt.Rows.Add(dr);
 
    DataRow dr2 = dt.NewRow();
    dr2[0] = timeValue.AddHours(2).ToOADate();
    dr2[1] = 5;
    dt.Rows.Add(dr2);
 
    DataRow dr3 = dt.NewRow();
    dr3[0] = timeValue.AddHours(4).ToOADate();
    dr3[1] = 2;
    dt.Rows.Add(dr3);
 
    return dt;
}


Also, if you want to format the label to display date AND time simultaneously, you will need to set the radChart1.PlotArea.XAxis.Appearance.CustomFormat instead like this:

radChart1.PlotArea.XAxis.Appearance.CustomFormat = "dd/MM/yyyy hh:mm";



Sincerely yours,
Manuel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Edward
Top achievements
Rank 1
answered on 19 Oct 2009, 04:22 PM
Ahh.. Thank you.
Tags
Chart (obsolete as of Q1 2013)
Asked by
Edward
Top achievements
Rank 1
Answers by
Giuseppe
Telerik team
Edward
Top achievements
Rank 1
Share this question
or