Time Cartesian Axis

The time scale is used to display times and dates. Data are spread according to the amount of time between data points. When building its ticks, it will automatically calculate the most comfortable unit base on the size of the scale.

Date Adapters

The time scale requires both a date library and a corresponding adapter to be present. Please choose from the available adapters Time Cartesian Axis - 图1 (opens new window).

Data Sets

Input Data

See data structures.

Date Formats

When providing data for the time scale, Chart.js uses timestamps defined as milliseconds since the epoch (midnight January 1, 1970, UTC) internally. However, Chart.js also supports all of the formats that your chosen date adapter accepts. You should use timestamps if you’d like to set parsing: false for better performance.

Configuration Options

Time Axis specific options

Namespace: options.scales[scaleId]

NameTypeDefaultDescription
adapters.dateobject{}Options for adapter for external date library if that adapter needs or supports options
boundsstring‘data’Determines the scale bounds. more…
ticks.sourcestring‘auto’How ticks are generated. more…
time.displayFormatsobjectSets how different time units are displayed. more…
time.isoWeekdayboolean|numberfalseIf boolean and true and the unit is set to ‘week’, then the first day of the week will be Monday. Otherwise, it will be Sunday. If number, the index of the first day of the week (0 - Sunday, 6 - Saturday)
time.parserstring|functionCustom parser for dates. more…
time.roundstringfalseIf defined, dates will be rounded to the start of this unit. See Time Units below for the allowed units.
time.tooltipFormatstringThe format string to use for the tooltip.
time.unitstringfalseIf defined, will force the unit to be a certain type. See Time Units section below for details.
time.stepSizenumber1The number of units between grid lines.
time.minUnitstring‘millisecond’The minimum display format to be used for a time unit.

Common options to all cartesian axes

Namespace: options.scales[scaleId]

NameTypeDefaultDescription
boundsstring‘ticks’Determines the scale bounds. more…
positionstringPosition of the axis. more…
stackstringStack group. Axes at the same position with same stack are stacked.
stackWeightnumber1Weight of the scale in stack group. Used to determine the amount of allocated space for the scale within the group.
axisstringWhich type of axis this is. Possible values are: ‘x’, ‘y’. If not set, this is inferred from the first character of the ID which should be ‘x’ or ‘y’.
offsetbooleanfalseIf true, extra space is added to the both edges and the axis is scaled to fit into the chart area. This is set to true for a bar chart by default.
titleobjectScale title configuration. more…

Common options to all axes

Namespace: options.scales[scaleId]

NameTypeDefaultDescription
typestringType of scale being employed. Custom scales can be created and registered with a string key. This allows changing the type of an axis for a chart.
alignToPixelsbooleanfalseAlign pixel values to device pixels.
backgroundColorColorBackground color of the scale area.
displayboolean|stringtrueControls the axis global visibility (visible when true, hidden when false). When display: ‘auto’, the axis is visible only if at least one associated dataset is visible.
gridobjectGrid line configuration. more…
minnumberUser defined minimum number for the scale, overrides minimum value from data. more…
maxnumberUser defined maximum number for the scale, overrides maximum value from data. more…
reversebooleanfalseReverse the scale.
stackedboolean|stringfalseShould the data be stacked. more…
suggestedMaxnumberAdjustment used when calculating the maximum data value. more…
suggestedMinnumberAdjustment used when calculating the minimum data value. more…
ticksobjectTick configuration. more…
weightnumber0The weight used to sort the axis. Higher weights are further away from the chart area.

Time Units

The following time measurements are supported. The names can be passed as strings to the time.unit config option to force a certain unit.

  • 'millisecond'
  • 'second'
  • 'minute'
  • 'hour'
  • 'day'
  • 'week'
  • 'month'
  • 'quarter'
  • 'year'

For example, to create a chart with a time scale that always displayed units per month, the following config could be used.

  1. var chart = new Chart(ctx, {
  2. type: 'line',
  3. data: data,
  4. options: {
  5. scales: {
  6. x: {
  7. type: 'time',
  8. time: {
  9. unit: 'month'
  10. }
  11. }
  12. }
  13. }
  14. });

Display Formats

You may specify a map of display formats with a key for each unit:

  • millisecond
  • second
  • minute
  • hour
  • day
  • week
  • month
  • quarter
  • year

The format string used as a value depends on the date adapter you chose to use.

For example, to set the display format for the quarter unit to show the month and year, the following config might be passed to the chart constructor.

  1. var chart = new Chart(ctx, {
  2. type: 'line',
  3. data: data,
  4. options: {
  5. scales: {
  6. x: {
  7. type: 'time',
  8. time: {
  9. displayFormats: {
  10. quarter: 'MMM YYYY'
  11. }
  12. }
  13. }
  14. }
  15. }
  16. });

Ticks Source

The ticks.source property controls the ticks generation.

  • 'auto': generates “optimal” ticks based on scale size and time options
  • 'data': generates ticks from data (including labels from data {x|y} objects)
  • 'labels': generates ticks from user given labels ONLY

Parser

If this property is defined as a string, it is interpreted as a custom format to be used by the date adapter to parse the date.

If this is a function, it must return a type that can be handled by your date adapter’s parse method.

Internal data format

Internally time scale uses milliseconds since epoch