How to: Exclude Weekends and Holidays from the Axis Scale
- 3 minutes to read
This example shows how to exclude non-working days (weekdays and holidays) from an axis range.
The first chart excludes holidays and non-working days from the axis scale and shows how to set custom workdays and holidays. Sunday is a working day and Saturday is a day off. March 6th (Saturday) is an additional working day and March 8th (Monday) is an additional holiday. Configure the DateTimeScaleOptions.WorkdaysOnly and DateTimeScaleOptions.WorkdaysOptions properties to enable such a display.
The second chart shows how to exclude axis ranges without data points. In this example, March 8th has no data points to display and is not displayed on the X-axis. The DateTimeScaleOptions.SkipRangesWithoutPoints property controls this behavior.
using DevExpress.XtraCharts;
using System;
using System.Data;
using System.Windows.Forms;
// ...
private void Form1_Load(object sender, EventArgs e) {
// ...
#region Chart1
// ...
DateTimeScaleOptions dateTimeScaleOptions = ((XYDiagram)chartControl1.Diagram).AxisX.DateTimeScaleOptions;
// Enables workday options.
dateTimeScaleOptions.WorkdaysOnly = true;
// Specifies custom work days.
// In this example, Sunday is a work day and Saturday is a day off.
dateTimeScaleOptions.WorkdaysOptions.Workdays = Weekday.Sunday | Weekday.Monday | Weekday.Tuesday |
Weekday.Wednesday | Weekday.Thursday | Weekday.Friday;
// Specifies custom holidays.
// In this example, March 8th (Monday) is an additional holiday.
dateTimeScaleOptions.WorkdaysOptions.Holidays.Add(
new KnownDate("Custom Holiday", new DateTime(2021, 3, 8, 0, 0, 0, 0)));
// Specifies work days, which have priority over specified holidays.
// In this example, March 6th (Saturday) is an additional work day.
dateTimeScaleOptions.WorkdaysOptions.ExactWorkdays.Add(
new KnownDate("Community Work Day", new DateTime(2021, 3, 6, 0, 0, 0, 0)));
#endregion
#region Chart2
// ...
DateTimeScaleOptions dateTimeScaleOptions2 = ((XYDiagram)chartControl2.Diagram).AxisX.DateTimeScaleOptions;
// Excludes all axis ranges without data points.
// In this example, March 8th has no data points to display and is not displayed on the X-axis.
dateTimeScaleOptions2.SkipRangesWithoutPoints = true;
#endregion
}
The following images demonstrate a chart with non-working days excluded, and included.
WorkdaysOnly = false | WorkdaysOnly = true |
---|---|