Remove weekends from date axis

8 posts, 0 answers
  1. Raj
    Raj avatar
    34 posts
    Member since:
    Aug 2009

    Posted 15 Dec 2010 Link to this post

    The x axis of my chart is a series of dates. Since my data will never have weekend dates, is it possible to remove weekends from the chart. So, Instead of the x axis being...

    M T W T F S S M T...

    It should be

    M T W T F M T....
  2. Giuseppe
    Admin
    Giuseppe avatar
    2363 posts

    Posted 20 Dec 2010 Link to this post

    Hello Raj,

    You can use the built-in filtering functionality to achieve the desired effect (note that you will need to introduce one additional read-only property of type System.DayOfWeek that will be used by the filtering mechanism):

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
     
            var rand = new Random(123456);
     
            List<ChartData> data = new List<ChartData>();
     
            for (int i = 0; i < 21; i++)
            {
                data.Add(new ChartData() { Date = DateTime.Today.AddDays(i), Value = rand.Next(10, 100) });
            }
     
            SeriesMapping sm = new SeriesMapping();
            sm.SeriesDefinition = new BarSeriesDefinition();
            sm.ItemMappings.Add(new ItemMapping("Date", DataPointMember.XCategory));
            sm.ItemMappings.Add(new ItemMapping("Value", DataPointMember.YValue));
            sm.FilterDescriptors.Add(new ChartFilterDescriptor("FormattedDate", typeof(DayOfWeek), FilterOperator.IsNotEqualTo, DayOfWeek.Saturday));
            sm.FilterDescriptors.Add(new ChartFilterDescriptor("FormattedDate", typeof(DayOfWeek), FilterOperator.IsNotEqualTo, DayOfWeek.Sunday));
     
            RadChart1.DefaultView.ChartArea.AxisX.DefaultLabelFormat = "ddd";
     
            RadChart1.SeriesMappings.Add(sm);
            RadChart1.ItemsSource = data;
        }
    }
     
    public class ChartData
    {
        public DateTime Date
        {
            get;
            set;
        }
     
        public DayOfWeek FormattedDate
        {
            get
            {
                return this.Date.DayOfWeek;
            }
        }
     
        public double Value
        {
            get;
            set;
        }
    }

    Hope this helps.


    All the best,
    Freddie
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  3. Raj
    Raj avatar
    34 posts
    Member since:
    Aug 2009

    Posted 20 Dec 2010 Link to this post

    Hi Freddie,

    Thank you for the response. Your sample app works wonderfully. I changed my chart to use the XCategory DataPointMember instead of XValue in order to conform to your sample. However, when I do this the chart appears blank even though the data is properly bound and there is valid data. Still trying to track this down but if you have any ideas please let me know.

    Thanks,
    Raj
  4. Raj
    Raj avatar
    34 posts
    Member since:
    Aug 2009

    Posted 21 Dec 2010 Link to this post

    Update to my last post...

    I was originally manually setting the Min, Max and Step values of the x-axis. Setting the AutoRange back on fixes the issues where the chart appears blank. Is is possible to manually set up your range using XCategory instead of XValue?
  5. Giuseppe
    Admin
    Giuseppe avatar
    2363 posts

    Posted 22 Dec 2010 Link to this post

    Hello Raj,

    Generally setting manual range makes sense only for numerical axis (using XValue) and is not supported for the categorical (string-based) axis.

    Could you elaborate what you are trying to achieve by customizing the range for the axis populated with categorical data?


    Greetings,
    Freddie
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  6. Raj
    Raj avatar
    34 posts
    Member since:
    Aug 2009

    Posted 23 Dec 2010 Link to this post

    It's essentially NOT categorical data. The only reason I was using XCategory was to be able to filter out weekends as described in my original post. The solution you had presented works great for small date ranges. However, if I have a date range of a year for example, the x-axis labels start overlapping each other because (using XCategory) each day is essentially a different category and there is no way to eliminate any of these by using a Step as you would with XValue.

    One solution would be to change the sampling period - one data point per week/month instead of one per day for example. But for this to look acceptable (where the x axis labels are not overlapping each other), the sampling points would have to be so far apart that the chart will lose all fidelity.
  7. Giuseppe
    Admin
    Giuseppe avatar
    2363 posts

    Posted 23 Dec 2010 Link to this post

    Hi Raj,

    Besides changing the sampling period, you can also try the following properties (demo here):
    • AxisX.StepLabelLevelCount -- Gets or sets the number of levels (steps) that will be used to distribute the axis item labels.
    • AxisX.LabelStep -- Gets or sets the value indicating that only one out of n axis labels should be visible, where n is the value of the property.
    • AxisX.LabelRotationAngle -- Gets or sets the rotation angle that will be applied on all axis item labels.


    Regards,
    Freddie
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  8. Raj
    Raj avatar
    34 posts
    Member since:
    Aug 2009

    Posted 23 Dec 2010 Link to this post

    Hi Freddie,

    The LabelStep sounds promising. Will give this a shot. 

    Thanks again for all your valuable input.

    Raj
Back to Top