Back to Devexpress

Work Time and Workday Configuration

windowsforms-16474-controls-and-libraries-chart-control-data-representation-work-time-and-workday-configuration.md

latest9.8 KB
Original Source

Work Time and Workday Configuration

  • Sep 20, 2024
  • 5 minutes to read

This document explains how to exclude non-working days and time from the date-time X-axis scale. You will also find information on how to exclude all date-time axis ranges without data points.

Exclude Non-Working Days

Use Workdays Options to exclude non-working days or add strict workdays.

Axis.DateTimeScaleOptions.WorkdaysOnly = falseAxis.DateTimeScaleOptions.WorkdaysOnly = true

The following code demonstrates how to configure workday options:

csharp
Series series1 = chartControl1.Series[0];
series1.ArgumentScaleType = ScaleType.DateTime;
series1.DataSource = CreateChartData();
series1.SetFinancialDataMembers("Argument", "Low", "High", "Open", "Close");

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)));
vb
Dim series1 As Series = chartControl1.Series(0)
series1.ArgumentScaleType = ScaleType.DateTime
series1.DataSource = CreateChartData()
series1.SetFinancialDataMembers("Argument", "Low", "High", "Open", "Close")
Dim dateTimeScaleOptions As DateTimeScaleOptions = CType(chartControl1.Diagram, XYDiagram).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 Or Weekday.Monday Or Weekday.Tuesday Or Weekday.Wednesday Or Weekday.Thursday Or 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)))

The following members and classes are used in the code above:

Class or MemberDescription
DateTimeScaleOptions.WorkdaysOnlySpecifies whether holidays and non-working days should be excluded from the axis scale.
DateTimeScaleOptions.WorkdaysOptionsSpecifies information about working days.
WorkdaysOptions.WorkdaysSpecifies which days of the week are workdays.
WorkdaysOptions.HolidaysSpecifies holiday dates to be excluded from the date-time scale.
WorkdaysOptions.ExactWorkdaysSpecifies strict workday dates to be included in the date-time scale.
KnownDateA specific named date.

Note that you can import holidays from a DevExpress Scheduler ( .xml ) or Microsoft Office Outlook® ( .hol ) file. Use the Load Holidays button at design time, or the WorkdaysOptions.LoadHolidays method at runtime.

Exclude Non-Working Time

Work Time Rules allow you to specify time intervals that should be displayed on the axis. The following table demonstrates this feature in action.

Axis.DateTimeScaleOptions.WorkTimeOnly = falseAxis.DateTimeScaleOptions.WorkTimeOnly = true

The following code demonstrates how to configure work time rules.

csharp
XYDiagram chartDiagram = (XYDiagram)financialChart.Diagram;
DateTimeScaleOptions xAxisScaleOptions = chartDiagram.AxisX.DateTimeScaleOptions;

// This property turns on axis values filtering by rules.
xAxisScaleOptions.WorkTimeOnly = true;

// Create a new instance of the worktime rule which will be used to specify
// the worktime displayed on the X-axis.
WorkTimeRule weekdayRule = new WorkTimeRule();
xAxisScaleOptions.WorkTimeRules.Add(weekdayRule);
// Specify one or more work intervals of this rule.
weekdayRule.WorkIntervals.Add(new TimeInterval(09, 00, 00, 19, 00, 00));
// Add weekdays on which this rule should be applied.
weekdayRule.Weekdays = Weekday.Monday | Weekday.Tuesday | Weekday.Wednesday 
        | Weekday.Thursday | Weekday.Friday;

WorkTimeRule dateRule = new WorkTimeRule();
xAxisScaleOptions.WorkTimeRules.Add(dateRule);
dateRule.WorkIntervals.Add(new TimeInterval(12, 00, 00, 16, 00, 00));
// Besides Weekdays, you can specify the Date 
// on which this rule should be applied.
dateRule.Date = new DateTime(2017, 1, 1);
vb
Dim chartDiagram As XYDiagram = DirectCast(financialChart.Diagram, XYDiagram)
Dim xAxisScaleOptions As DateTimeScaleOptions = chartDiagram.AxisX.DateTimeScaleOptions

' This property turns on axis values filtering by rules.
xAxisScaleOptions.WorkTimeOnly = True

' Create a new instance of the worktime rule which will be used to specify 
' the worktime displayed on the X-axis.
Dim weekdayRule As New WorkTimeRule()
xAxisScaleOptions.WorkTimeRules.Add(weekdayRule)
' Specify one or more work intervals of this rule.
weekdayRule.WorkIntervals.Add(New TimeInterval(09, 00, 00, 19, 00, 00))
' Add weekdays on which this rule should be applied.
weekdayRule.Weekdays = Weekday.Monday Or Weekday.Tuesday Or Weekday.Wednesday _
        Or Weekday.Thursday Or Weekday.Friday

Dim dateRule As New WorkTimeRule()
xAxisScaleOptions.WorkTimeRules.Add(dateRule)
dateRule.WorkIntervals.Add(New TimeInterval(12, 00, 00, 16, 00, 00))
' Besides Weekdays, you can specify the Date 
' on which this rule should be applied.
dateRule.Date = New DateTime(2017, 1, 1)

Note

If you don’t specify work time rules for certain days, the work time is the entire 24-hour day.

The code below uses the following classes and methods.

Class or MemberDescription
DateTimeScaleOptions.WorkTimeOnlySpecifies the value indicating whether to show only the work time on a date-time axis.
DateTimeScaleOptions.WorkTimeRulesSpecifies a collection of work time rules that should be applied to the date-time axis scale.
WorkTimeRuleA work time rule.
WorkTimeRule.WorkIntervalsSpecifies working intervals within a day.
TimeIntervalA time interval.
WorkTimeRule.WeekdaysSpecifies weekdays on which the rule should be applied.
WorkTimeRule.DateSpecifies the date on which the rule should be applied.

Exclude Axis Ranges without Data

Use the DateTimeScaleOptions.SkipRangesWithoutPoints option to exclude all date-time axis ranges without data points. If your goal is to display work days/time only, enable the DateTimeScaleOptions.WorkdaysOnly and DateTimeScaleOptions.WorkTimeOnly properties instead.

The following images show a chart with different SkipRangesWithoutPoints values:

|

Property Value

|

Example

| | --- | --- | |

SkipRangesWithoutPoints = true

|

| |

SkipRangesWithoutPoints = false (Default)

|

|

Example

The following example demonstrates how you can exclude weekends and holidays from the X-axis range:

View Example: Charts - How to exclude weekends and holidays from the axis range

See Also

How to: Exclude Weekends and Holidays from the Axis Scale