Series.DefaultLabelValue format to ShortDate

2 posts, 0 answers
  1. Fernan
    Fernan avatar
    15 posts
    Member since:
    May 2010

    Posted 28 Dec 2010 Link to this post

    Hi,

    I am using RadChart to show datetime values in X Axis. I need to show in the values, both values: x and y.

    I use the property DefaultLabelValue in Series object: this.radChart1.Series[0].DefaultLabelValue = "#Y";      

    I need show X value in shortdate format. If I write this.radChart1.Series[0].DefaultLabelValue = "#X,#Y"; , the values are doubles and not datetime.

    Thanks,      
  2. Evgenia
    Admin
    Evgenia avatar
    1406 posts

    Posted 29 Dec 2010 Link to this post

    Hello Fernan,

    To be able to show Series Labels as DateTime you can use the Label property of each ChartSeriesItem and you convert the OADate to DateTime. Here is what I mean:
     
    public partial class Form1 : Form
        {
             
            public Form1()
            {
                InitializeComponent();
                RadChart RadChart1 = new RadChart();
                DataTable chartdata = new DataTable();
                chartdata.Columns.Add("date", typeof(DateTime));
                chartdata.Columns.Add("value", typeof(double));
      
                List<string> dates = new List<string>();
                dates.Add("2010-06-01");
                dates.Add("2010-07-01");
                dates.Add("2010-08-01");
                dates.Add("2010-09-01");
                dates.Add("2010-10-01");
                dates.Add("2010-11-01");
      
                List<double> values2 = new List<double>();
                values2.Add(197);
                values2.Add(441);
                values2.Add(425);
                values2.Add(396);
                values2.Add(108);
                values2.Add(170);
      
                int k = 0;
      
                foreach (string strdate in dates)
                {
                    DataRow cdr = chartdata.NewRow();
                    cdr[0] = DateTime.Parse(strdate);
                    cdr[1] = values2[k];
      
                    chartdata.Rows.Add(cdr);
      
                    k++;
                }
      
                ChartSeries series2 = RadChart1.CreateSeries("value", Color.LightGreen, Color.LightGreen, ChartSeriesType.StackedBar);
      
                foreach (DataRowView drv in chartdata.DefaultView)
                {
                    ChartSeriesItem item = new ChartSeriesItem();
                    item.XValue = DateTime.Parse(drv["date"].ToString()).ToOADate();
                    item.YValue = Math.Round(double.Parse(drv["value"].ToString()), 1);
                    item.Label.TextBlock.Text = DateTime.FromOADate(item.XValue).ToShortDateString();
                    series2.Items.Add(item);
                };
      
                RadChart1.AutoLayout = true;
                RadChart1.Series.Add(series2);
      
                RadChart1.PlotArea.XAxis.AutoScale = false;
                RadChart1.PlotArea.XAxis.LayoutMode = Telerik.Charting.Styles.ChartAxisLayoutMode.Inside;
                RadChart1.PlotArea.XAxis.Appearance.ValueFormat = Telerik.Charting.Styles.ChartValueFormat.ShortDate;
                RadChart1.PlotArea.XAxis.Appearance.CustomFormat = "MMM-yyyy";
                double step = DateTime.Parse("2010-06-01").AddMonths(1).ToOADate();
                RadChart1.PlotArea.XAxis.AddRange(DateTime.Parse("2010-06-01").ToOADate(), DateTime.Parse("2010-12-01").ToOADate(), 31);
                RadChart1.PlotArea.XAxis.Appearance.LabelAppearance.RotationAngle = -45;
                RadChart1.PlotArea.YAxis.AutoScale = true;
                this.Controls.Add(RadChart1);
            }
        }

    Best wishes,
    Evgenia
    the Telerik team
    Q3’10 SP1 of RadControls for WinForms is available for download; also available is the Q1'11 Roadmap for Telerik Windows Forms controls.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top