Angular ChartsTime Axis

Version 14.2.0

Time axes are used to display time-based data in a chart. They can be used to show data at different levels of granularity, such as years, months, days, or even hours and minutes.

Time can be provided as a Date object, a number which is interpreted as timestamps derived from Unix time or an ISO 8601 string.

There are three methods of displaying time along an axis.

The difference between a Unit Time Axis, an Ordinal Time Axis, and a Continuous Time Axis is demonstrated in the following example:

Unit Time Copy Link

The Unit Time Axis will plot time values with evenly spaced bands for each unit of time in the domain, regardless of the actual time span between them or missing data.

For example, with a month unit, dates of 01 Jan and 30 Mar will appear as three evenly spaced items representing "January", "February" and "March".

{
    axes: {
        x: { type: 'unit-time' },
    },
}

The Unit Time Axis does not aggregate data, so you should ensure that each series only has one value per unit.

Custom Unit Copy Link

The Unit Time Axis assumes that data is provided with one item per unit, and will infer a unit based on the data.

For some scenarios - such as datasets with missing data points - it may be necessary to specify the unit explicitly. To explicitly set the unit, provide an AgTimeIntervalUnit string or AgTimeInterval object.

{
    axes: {
        x: {
            type: 'unit-time',
            unit: 'day', // every day
        },
    },
}

In this configuration:

  • unit must be one of millisecond, second, minute, hour, day, month, or year.

The axes[].unit property controls how time is bucketed for display. See Time Interval to control the spacing of ticks, grid lines, and labels.

Weekly Data Example Copy Link

The AgTimeIntervalUnit object supports advanced configuration for more complex unit requirements such as weekly data.

{
    axes: {
        x: {
            type: 'unit-time',
            unit: {
                unit: 'day',
                step: 7, //every 7 days (weekly)
                epoch: new Date(2024, 0, 1), //start the week on Monday
            },
        },
    },
}

In this example:

  • step specifies the multiple of the unit to use. In this example, step: 7 combined with unit: 'day' will display one data point for any provided date within each week.
  • epoch is an optional Date that specifies the starting point for the unit. In this example, the week runs from Monday to Monday.
  • In this example, a Formatter is used to format the labels as numbered weeks of the year.

For a full list of configuration options see Unit Time Axis Options.

Ordinal Time Copy Link

Data points plotted along an Ordinal Time Axis will be arranged according to their position in time, ignoring the time intervals between them. This is unlike the Unit Time and Continuous Time Axis, which represent time with consistent intervals.

For example, if the Ordinal Time Axis is used to plot daily values but there are no data points for the weekend, values for Friday and Monday will be equally spaced, without gaps for Saturday and Sunday.

Ordinal Time axes are commonly used for financial data on the x-axis, usually placed at the bottom of a chart.

A basic Ordinal Time Axis configuration looks like this:

{
    axes: {
        x: {
            type: 'ordinal-time',
        },
    },
}

For a full list of configuration options see Ordinal Time Axis Options.

Continuous Time Copy Link

The time axis is similar to the number axis in the sense that it is also used to plot continuous values.

Time axes are typically used as x-axes and placed at the bottom of a chart. The simplest time axis config looks like this:

{
    axes: {
        x: {
            type: 'time',
        },
    },
}

For a full list of configuration options see Continuous Time Axis Options.

Time Intervals Copy Link

The Axis Interval of Time Axes labels, grid lines and ticks can be customised with the interval.step parameter, which should be a AgTimeInterval or AgTimeIntervalUnit.

{
    interval: {
        step: 'month',
    },
}
{
    interval: {
        step: {
            unit: 'day',
            step: 7, // every 7 days (weekly)
            epoch: new Date('2024-01-01'), //start the week on a Monday
        },
    },
}

In this configuration:

  • unit must be one of millisecond, second, minute, hour, day, month, or year.
  • step is an optional number that specifies the multiple of the unit to use.
  • epoch is an optional Date that specifies the starting point for the interval.

The interval.step property controls the spacing of ticks, grid lines, and labels. Use a Unit Time Axis to control how time is bucketed for display.

See Time Interval for more information on the interval.step property.

Parent Levels Copy Link

The parentLevel option allows displaying labels and ticks for the parent level of the time axes. For example, if the axis is showing monthly data, the parent level would be the years. These levels are based on the data displayed, and adjust dynamically as the user zooms in and out.

It is enabled by default for the Unit Time Axis, and can be opted into for the Continuous Time Axis and Ordinal Time Axis.

{
    parentLevel: {
        enabled: true,
    },
}

In this example:

  • The data is shown with a 'day' unit, and the parent level of 'month' is shown in bold.
  • As you zoom out, the labels will change to 'month', with a parent level of 'year'.

Customisation Copy Link

Parent level labels and ticks options are inherited from the axes label and tick options, but can be customised with the parentLevel options.

{
    tick: {
        width: 0,
    },
    label: {
        format: {
            day: '%e',
            month: '%b',
        },
    },
    parentLevel: {
        enabled: true,
        tick: {
            width: 1,
        },
        label: {
            format: {
                month: '%e\n%b',
                year: '%b\n%Y',
            },
        },
    },
}

In this configuration:

  • The label format is set to show the day and month for the day level, and the month for the month level.
  • The parent level tick is given a width of 1 to show the parent level ticks.
  • The parent level label format is set to show the day and month on separate lines for the month level, and the month and year on separate lines for the year level.

See Axis Labels and the Formatters pages for more information on formatting labels.

API Reference Copy Link

Properties available on the AgUnitTimeAxisOptions interface.

type 'unit-time'
Axis type identifier.
crossLines AgCartesianCrossLineOptions[]
Add cross-lines or regions corresponding to data values.
parentLevel AgTimeAxisParentLevel
Options for labels and ticks for the parent level intervals.
unit AgTimeInterval | AgTimeIntervalUnit
The size of each band. A unit keyword (or number), or an object describing the interval.
interval AgAxisDiscreteTimeIntervalOptions
Configuration for the axis ticks interval.
paddingInner Ratio
The size of the gap between the categories as a proportion, between 0 and 1. This value is a fraction of the “step”, which is the interval between the start of a band and the start of the next band.
paddingOuter Ratio
The padding on the outside i.e. left and right of the first and last category. In association with `paddingInner`, this value can be between 0 and 1.
groupPaddingInner Ratio
This property is for grouped column/bar series plotted on a category axis. It is a proportion between 0 and 1 which determines the size of the gap between the bars or columns within a single group along the axis.
bandHighlight AgBandHighlightOptions
Configuration for the axis band highlight.
bandAlignment AgBandAlignment default: 'justify'
The alignment of bands when used with bar-like series with fixed widths.
skipNullBars boolean default: false
Set to `true` to prevent bars with `null`, `undefined` or missing values from taking up space in each category.
position AgCartesianAxisPosition
The position on the chart where the axis should be rendered.
crossAt AgCartesianAxisCrossAt
Value on the first perpendicular axis' domain where this axis should intersect.
thickness PixelSize
Sets the axis thickness regardless of its content.
maxThicknessRatio Ratio default: 0.3
The maximum thickness of the axis, as a ratio of the chart's width or height depending on axis direction. Used to prevent the axis from growing too large when labels or content are oversized.
title AgCartesianAxisCaptionOptions
Configuration for the title shown next to the axis.
crosshair AgCrosshairOptions
Configuration for the axis crosshair.
listeners AgAxisListeners
A map of event names to event listeners.
context ContextDefault
Context object to use in callbacks.
reverse boolean
Reverse the axis scale domain if `true`.
line AgAxisLineOptions
Configuration for the axis line.
gridLine AgAxisGridLineOptions
Configuration for the axis grid lines.
label AgCartesianTimeAxisLabelOptions
Configuration for the axis labels, shown next to the ticks.
tick AgAxisBaseTickOptions
Configuration for the axis ticks.
min AgTimeValue
The min value for the axis domain.
max AgTimeValue
The max value for the axis domain.
preferredMin AgTimeValue
The min value for the axis, unless extended by the series data or `nice` option.
preferredMax AgTimeValue
The max value for the axis, unless extended by the series data or `nice` option.

Properties available on the AgOrdinalTimeAxisOptions interface.

type 'ordinal-time'
Axis type identifier.
crossLines AgCartesianCrossLineOptions[]
Add cross-lines or regions corresponding to data values.
parentLevel AgTimeAxisParentLevel
Options for labels and ticks for the parent level intervals.
interval AgAxisDiscreteTimeIntervalOptions
Configuration for the axis ticks interval.
paddingInner Ratio
The size of the gap between the categories as a proportion, between 0 and 1. This value is a fraction of the “step”, which is the interval between the start of a band and the start of the next band.
paddingOuter Ratio
The padding on the outside i.e. left and right of the first and last category. In association with `paddingInner`, this value can be between 0 and 1.
groupPaddingInner Ratio
This property is for grouped column/bar series plotted on a category axis. It is a proportion between 0 and 1 which determines the size of the gap between the bars or columns within a single group along the axis.
bandHighlight AgBandHighlightOptions
Configuration for the axis band highlight.
bandAlignment AgBandAlignment default: 'justify'
The alignment of bands when used with bar-like series with fixed widths.
skipNullBars boolean default: false
Set to `true` to prevent bars with `null`, `undefined` or missing values from taking up space in each category.
position AgCartesianAxisPosition
The position on the chart where the axis should be rendered.
crossAt AgCartesianAxisCrossAt
Value on the first perpendicular axis' domain where this axis should intersect.
thickness PixelSize
Sets the axis thickness regardless of its content.
maxThicknessRatio Ratio default: 0.3
The maximum thickness of the axis, as a ratio of the chart's width or height depending on axis direction. Used to prevent the axis from growing too large when labels or content are oversized.
title AgCartesianAxisCaptionOptions
Configuration for the title shown next to the axis.
crosshair AgCrosshairOptions
Configuration for the axis crosshair.
listeners AgAxisListeners
A map of event names to event listeners.
context ContextDefault
Context object to use in callbacks.
reverse boolean
Reverse the axis scale domain if `true`.
line AgAxisLineOptions
Configuration for the axis line.
gridLine AgAxisGridLineOptions
Configuration for the axis grid lines.
label AgCartesianTimeAxisLabelOptions
Configuration for the axis labels, shown next to the ticks.
tick AgAxisBaseTickOptions
Configuration for the axis ticks.

Properties available on the AgTimeAxisOptions interface.

type 'time'
Axis type identifier.
parentLevel AgTimeAxisParentLevel
Options for labels and ticks for the parent level intervals.
crossLines AgCartesianCrossLineOptions[]
Add cross-lines or regions corresponding to data values.
position AgCartesianAxisPosition
The position on the chart where the axis should be rendered.
crossAt AgCartesianAxisCrossAt
Value on the first perpendicular axis' domain where this axis should intersect.
thickness PixelSize
Sets the axis thickness regardless of its content.
maxThicknessRatio Ratio default: 0.3
The maximum thickness of the axis, as a ratio of the chart's width or height depending on axis direction. Used to prevent the axis from growing too large when labels or content are oversized.
title AgCartesianAxisCaptionOptions
Configuration for the title shown next to the axis.
crosshair AgCrosshairOptions
Configuration for the axis crosshair.
listeners AgAxisListeners
A map of event names to event listeners.
context ContextDefault
Context object to use in callbacks.
reverse boolean
Reverse the axis scale domain if `true`.
line AgAxisLineOptions
Configuration for the axis line.
gridLine AgAxisGridLineOptions
Configuration for the axis grid lines.
label AgCartesianTimeAxisLabelOptions
Configuration for the axis labels, shown next to the ticks.
tick AgAxisBaseTickOptions
Configuration for the axis ticks.
nice boolean
If `true`, the range will be rounded up to ensure nice equal spacing between the ticks. __Note:__ This does not override the `min` or `max` options.
interval AgAxisContinuousIntervalOptions
Configuration for the axis ticks interval. A unit keyword (or number), or an object describing the interval.
min AgTimeValue
The min value for the axis domain.
max AgTimeValue
The max value for the axis domain.
preferredMin AgTimeValue
The min value for the axis, unless extended by the series data or `nice` option.
preferredMax AgTimeValue
The max value for the axis, unless extended by the series data or `nice` option.

Properties available on the AgTimeInterval interface.

unit required AgTimeIntervalUnit
The base duration of the time interval.
step number
A multiplier of the `unit`. For example, a unit of `'week'` and a step of `2` would be every two weeks.
epoch Date
Defines the alignment of time interval. For example, a unit of `'week'` with an epoch date of a Monday would be every Monday.
utc boolean
Whether all dates should be in UTC, or local time.