Add Labes to x-axis manually

3 posts, 1 answers
  1. Glen Fernandes
    Glen Fernandes avatar
    2 posts
    Member since:
    Dec 2008

    Posted 04 May 2009 Link to this post

    i have a chart with dates on X-axis , Chart is bound to a dataSource and there are 700 items  .
    with the default settings all the dates are displayed as labels and there is a overlap of labels on X-axis. 

    what i want to do is have only three labels on x-axis.
     1) at start   i.e Min of Dates (the x-axis starts from here)
    2) at end i.e Max of Dates (the x-axis Ends here)
    3) at  the middle (approximately)  (between the min and max date)

    in Chart wizard, labels can be added to axis manually . but it only accepts INT's.
    How can i achieve this with dates?

    below is the code generated by the designer while adding labels manually for INT Data types

     

     

     

    1             Telerik.Charting.ChartAxisItem chartAxisItem1 = new Telerik.Charting.ChartAxisItem();  
    2             Telerik.Charting.ChartAxisItem chartAxisItem2 = new Telerik.Charting.ChartAxisItem();  
    3             Telerik.Charting.ChartAxisItem chartAxisItem3 = new Telerik.Charting.ChartAxisItem();  
    4             chartAxisItem1.Value = new decimal(new int[] {1, 0,0,0});  
    5             chartAxisItem2.Value = new decimal(new int[] {6,0,0,0});  
    6             chartAxisItem3.Value = new decimal(new int[] {11,0,0,0});  
    7             this.radChart1.PlotArea.XAxis.Items.AddRange(new Telerik.Charting.ChartAxisItem[] {  
    8             chartAxisItem1,  
    9             chartAxisItem2,  
    10             chartAxisItem3}); 

     

  2. Answer
    Ves
    Admin
    Ves avatar
    2879 posts

    Posted 06 May 2009 Link to this post

    Hi Glen ,

    One possible solution here would be to use the LabelStep property of the XAxis. This property defines how often the labels will appear along the axis -- setting it to 3 would force every third label to be visible, while the rest will be hidden. For more detailed control, you can wire the BeforeLayout event. At this moment the axis items are already created (using the default AutoScale=true, that is you will not need to add items manually), so you can loop through them and hide those that you do not need. Here is an example:

    void radChart1_BeforeLayout(object sender, EventArgs e) 
            { 
                int count = radChart1.PlotArea.XAxis.Items.Count; 
                foreach (var item in radChart1.PlotArea.XAxis.Items) 
                { 
                    int ind = radChart1.PlotArea.XAxis.Items.IndexOf(item); 
                    if (ind != 0 && ind != count / 2 && ind != count - 1) 
                        item.Visible = false
                } 
            } 


    Kind regards,
    Ves
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Glen Fernandes
    Glen Fernandes avatar
    2 posts
    Member since:
    Dec 2008

    Posted 07 May 2009 Link to this post

    Thanks for the reply Ves. it was helpful
Back to Top