SharpPlot Tutorials > General Tutorials > Using Date and Time axes

Using Date and Time axes

Timeseries data from Excel or from a database query is often held internally as floating-point numbers where the integer part represents the date (the number of days elapsed since 30 December 1899) and the decimal part the time of day. This scheme is accurate to around 2 sec with normal floating-point precision and is equivalent to the OLE date format, and can be obtained by the System.DateTime.ToOADate method.

Formatting dates

For example day number 35414 is actually 15/12/96 (in DD/MM/YY format) so 35414.5 would be 15/12/96 12:00, as times run from midnight to 23:59 spanning the range 0-1. SharpPlot can accept ‘Date’ and ‘Time’ styles (or both together) on either the X axis or Y axis, and will format the labels accordingly. The dates are shown by default in the format of your current Windows ‘short date’ setting and the times are normally shown as ‘HH:MM’ on a 24-hour clock.

Value ... Style ... Shown as
35414.25 ... (default) ... 35400
35414.25 ... Date ... 15/12/96
35414.25 ... Time ... 06:00
35414.25 ... Date,Time ... 15/12/96 06:00

For more detailed control over the presentation of the date/time combination, you can use the XDateFormat and XTimeFormat properties, for example to label tickmarks ‘Jan-01’ ‘Feb-01’ .... ‘Dec 02’ (in your local language) a suitable format would be “MMM-yy”.

The full reference can be found in the MSDN on Custom Date and Time Format Strings.

The X-axis will also accept integers in the range 19000000 to 20991231 and (if the XAxisStyle is one of Date/Annual/Monthly) treat these as if they are OLE date values. This may be convenient if you have imported your dates from an old time-series database, as this was a very common format in many database designs.

Setting Tickmarks

Tickmarks, Datum lines, etc are always given in data units, so OLE dates here.

Default tickmarks normally look for intervals which divide into 10, but for time-based axes the choice is made between values which divide into 12 which gives a better selection of intervals.

Calendar styles

In addition to ‘Date’ you can set the XAxisStyle (only) to ‘AnnualTicks’ which will put tickmarks at the year boundaries (so the tick spacing will be very slightly irregular). In this case the tick interval gives the number of years, so to mark decades you could use:

sp.XAxisStyle = XAxisStyles.AnnualTicks;

This style would very likely go along with MiddleLabels to get the labels written between the tick marks. The style ‘MonthlyTicks’ works in a similar way – in this case the tickmark setting is the interval between months.

See also ...

Duration Label formatting | SharpPlot.XAxisStyle Property | SharpPlot.XDateFormat Property | SharpPlot.XTimeFormat Property | SharpPlot.YAxisStyle Property | SharpPlot.YDateFormat Property | SharpPlot.YTimeFormat Property

Send comments on this topic
© Dyalog Ltd 2021