Skip to content

TimeAxisHelper

ClassChartGuruChartGuru
public static class TimeAxisHelper

Utilities for converting between DateTime and the float "axis seconds" values that flow through the rendering pipeline, and for generating calendar-aligned ticks on a time axis.

Contract:

public const int HardTickSafetyCap = 512

Hard safety cap on the number of calendar ticks we will ever emit, regardless of span or stride. Prevents pathological cases (e.g. 10 years stepped by Second) from hanging the renderer. Swift Charts has an equivalent internal guard.

Returns

System.Int32

public static float AxisSeconds(DateTime dt, double origin = 0)

Converts a DateTime to the float value expected on a time axis (seconds since the Unix epoch, minus the optional origin offset). A date of DateTimeKind.Unspecified kind reads as UTC, as dates in chart data do, so it keeps its calendar values on the axis; a local date converts to UTC.

Parameters

NameType
dtSystem.DateTime
originSystem.Double

Returns

System.Single

public static DateTime FromAxisSeconds(float seconds, double origin = 0)

Converts an axis-seconds float back to a UTC DateTime, adding back the origin offset.

Parameters

NameType
secondsSystem.Single
originSystem.Double

Returns

System.DateTime

public static TimeUnit DetectBestUnit(TimeSpan span)

Picks a sensible TimeUnit for the given span.

Parameters

NameType
spanSystem.TimeSpan

Returns

ChartGuru.TimeUnit

public static string DefaultFormat(TimeUnit unit)

Default tick label format for a given time unit (applied when AxisConfig.TimeFormat is empty).

Parameters

NameType
unitChartGuru.TimeUnit

Returns

System.String

public static DateTime RoundDown(DateTime dt, TimeUnit unit)

Rounds dt down to the nearest whole unit boundary.

Parameters

NameType
dtSystem.DateTime
unitChartGuru.TimeUnit

Returns

System.DateTime

public static DateTime AddUnit(DateTime dt, TimeUnit unit, int amount)

Adds amount units of unit to dt.

Parameters

NameType
dtSystem.DateTime
unitChartGuru.TimeUnit
amountSystem.Int32

Returns

System.DateTime

PickNiceStep(DateTime, DateTime, TimeUnit, int, out TimeUnit, out int)

Section titled “PickNiceStep(DateTime, DateTime, TimeUnit, int, out TimeUnit, out int)”
public static void PickNiceStep(DateTime min, DateTime max, TimeUnit preferredUnit, int desiredCount, out TimeUnit unit, out int multiplier)

Picks a nice (unit, multiplier) pair so that roughly desiredCount ticks span the domain. Swift Charts equivalent: the internal “AutomaticAxisMarks” resolver for temporal scales.

Parameters

NameType
minSystem.DateTime
maxSystem.DateTime
preferredUnitChartGuru.TimeUnit
desiredCountSystem.Int32
unitChartGuru.TimeUnit
multiplierSystem.Int32

GenerateStrideTickValues(float, float, TimeUnit, int, double)

Section titled “GenerateStrideTickValues(float, float, TimeUnit, int, double)”
public static List<float> GenerateStrideTickValues(float minSeconds, float maxSeconds, TimeUnit unit, int multiplier, double origin = 0)

Emits calendar-aligned tick values between minSeconds and maxSeconds, stepping multiplier x unit at a time across the full domain. Swift Charts equivalent: AxisMarks(values: .Stride(by: unit, count: multiplier)).

Parameters

NameType
minSecondsSystem.Single
maxSecondsSystem.Single
unitChartGuru.TimeUnit
multiplierSystem.Int32
originSystem.Double

Returns

System.Collections.Generic.List{System.Single}

GenerateStrideTickValues(float, float, TimeUnit, int, List, double)

Section titled “GenerateStrideTickValues(float, float, TimeUnit, int, List, double)”
public static void GenerateStrideTickValues(float minSeconds, float maxSeconds, TimeUnit unit, int multiplier, List<float> result, double origin = 0)

Appends time-axis ticks at the requested calendar stride inside the supplied second-based domain.

Parameters

NameType
minSecondsSystem.Single
maxSecondsSystem.Single
unitChartGuru.TimeUnit
multiplierSystem.Int32
resultSystem.Collections.Generic.List{System.Single}
originSystem.Double

GenerateAutomaticTickValues(float, float, TimeUnit, int, double)

Section titled “GenerateAutomaticTickValues(float, float, TimeUnit, int, double)”
public static List<float> GenerateAutomaticTickValues(float minSeconds, float maxSeconds, TimeUnit preferredUnit, int desiredCount, double origin = 0)

Emits roughly desiredCount calendar-aligned tick values between minSeconds and maxSeconds, choosing a nice stride so ticks are distributed evenly. Swift Charts equivalent: AxisMarks(values: .Automatic(desiredCount:)) on a temporal scale.

Parameters

NameType
minSecondsSystem.Single
maxSecondsSystem.Single
preferredUnitChartGuru.TimeUnit
desiredCountSystem.Int32
originSystem.Double

Returns

System.Collections.Generic.List{System.Single}

GenerateAutomaticTickValues(float, float, TimeUnit, int, double, out TimeUnit)

Section titled “GenerateAutomaticTickValues(float, float, TimeUnit, int, double, out TimeUnit)”
public static List<float> GenerateAutomaticTickValues(float minSeconds, float maxSeconds, TimeUnit preferredUnit, int desiredCount, double origin, out TimeUnit resolvedUnit)

Chooses an appropriate time unit and appends readable ticks across the supplied second-based domain.

Parameters

NameType
minSecondsSystem.Single
maxSecondsSystem.Single
preferredUnitChartGuru.TimeUnit
desiredCountSystem.Int32
originSystem.Double
resolvedUnitChartGuru.TimeUnit

Returns

System.Collections.Generic.List{System.Single}

GenerateAutomaticTickValues(float, float, TimeUnit, int, double, List, out TimeUnit)

Section titled “GenerateAutomaticTickValues(float, float, TimeUnit, int, double, List, out TimeUnit)”
public static void GenerateAutomaticTickValues(float minSeconds, float maxSeconds, TimeUnit preferredUnit, int desiredCount, double origin, List<float> result, out TimeUnit resolvedUnit)

Chooses an appropriate time unit and appends readable ticks across the supplied second-based domain.

Parameters

NameType
minSecondsSystem.Single
maxSecondsSystem.Single
preferredUnitChartGuru.TimeUnit
desiredCountSystem.Int32
originSystem.Double
resultSystem.Collections.Generic.List{System.Single}
resolvedUnitChartGuru.TimeUnit

FormatValue(float, string, TimeUnit, double, CultureInfo)

Section titled “FormatValue(float, string, TimeUnit, double, CultureInfo)”
public static string FormatValue(float seconds, string format, TimeUnit unit, double origin = 0, CultureInfo culture = null)

Formats an axis-seconds value as a date string using format (or a unit-appropriate default when empty). Month and weekday names come from culture, the current culture when null, like the numbers on the other axis.

Parameters

NameType
secondsSystem.Single
formatSystem.String
unitChartGuru.TimeUnit
originSystem.Double
cultureSystem.Globalization.CultureInfo

Returns

System.String

IsAlignedToBoundary(float, TimeUnit, int, double)

Section titled “IsAlignedToBoundary(float, TimeUnit, int, double)”
public static bool IsAlignedToBoundary(float seconds, TimeUnit unit, int multiplier, double origin = 0)

Returns true when the axis-seconds value falls on a calendar boundary for the given unit and multiplier. Used by the label-stride convenience to decide which ticks should show labels.

Parameters

NameType
secondsSystem.Single
unitChartGuru.TimeUnit
multiplierSystem.Int32
originSystem.Double

Returns

System.Boolean