Date and Time on Xaxis?

3 posts, 0 answers
  1. Edward
    Edward avatar
    3 posts
    Member since:
    Sep 2008

    Posted 08 Oct 2009 Link to this post

    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. Giuseppe
    Admin
    Giuseppe avatar
    2363 posts

    Posted 13 Oct 2009 Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Edward
    Edward avatar
    3 posts
    Member since:
    Sep 2008

    Posted 19 Oct 2009 Link to this post

    Ahh.. Thank you.
Back to Top