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

Remove weekends from date axis

7 Answers 112 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Raj
Top achievements
Rank 1
Raj asked on 15 Dec 2010, 08:26 PM
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....

7 Answers, 1 is accepted

Sort by
0
Giuseppe
Telerik team
answered on 20 Dec 2010, 06:25 PM
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
0
Raj
Top achievements
Rank 1
answered on 20 Dec 2010, 09:46 PM
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
0
Raj
Top achievements
Rank 1
answered on 21 Dec 2010, 04:26 PM
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?
0
Giuseppe
Telerik team
answered on 22 Dec 2010, 03:07 PM
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
0
Raj
Top achievements
Rank 1
answered on 23 Dec 2010, 04:41 PM
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.
0
Giuseppe
Telerik team
answered on 23 Dec 2010, 05:15 PM
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
0
Raj
Top achievements
Rank 1
answered on 23 Dec 2010, 05:57 PM
Hi Freddie,

The LabelStep sounds promising. Will give this a shot. 

Thanks again for all your valuable input.

Raj
Tags
Chart
Asked by
Raj
Top achievements
Rank 1
Answers by
Giuseppe
Telerik team
Raj
Top achievements
Rank 1
Share this question
or