vega-time
v2.1.2
Published
JavaScript date/time utilities for Vega.
Downloads
645,175
Readme
vega-time
JavaScript date-time utilities for Vega. Provides a set of helper methods for working with date objects (or, equivalently, with UNIX timestamps).
API Reference
Time Units
The date-time utilities support a set of pre-defined time units. A single unit value is one of the following strings:
'year'
- Gregorian calendar years.'quarter'
- Three-month intervals, starting in one of January, April, July, and October.'month'
- Calendar months (January, February, etc.).'date'
- Calendar day of the month (January 1, January 2, etc.).'week'
- Sunday-based weeks. Days before the first Sunday of the year are considered to be in week 0, the first Sunday of the year is the start of week 1, the second Sunday week 2, etc..'day'
- Day of the week (Sunday, Monday, etc.).'dayofyear'
- Day of the year (1, 2, ..., 365, etc.).'hours'
- Hours of the day (12:00am, 1:00am, etc.).'minutes'
- Minutes in an hour (12:00, 12:01, etc.).'seconds'
- Seconds in a minute (12:00:00, 12:00:01, etc.).'milliseconds'
- Milliseconds in a second.
Multiple units can be listed in an array to indicate desired intervals of time. For example, ['year', 'month', 'date']
indicates chronological time sensitive to year, month, and date (but not to hours, minutes, or seconds). The specifier ['month', 'date']
is sensitive to month and date, but not year, which can be useful for binning time values to look at seasonal patterns only.
# vega.timeUnits(units) <>
Returns a standardized and sorted specifier for the given units, which must be an array of one or more valid time unit strings. The returned array contains the same units, sorted in decreasing over of unit size, such that the most granular unit is last (for example, ['year', 'month', 'date']
). This method throws an error if the units array is empty, contains an invalid unit, or contains incompatible units. Specifically, the 'quarter'
, 'month'
, and 'date'
units can not be used in conjunction with the 'week'
or 'day'
units.
# vega.timeUnitSpecifier(units[, specifiers]) <>
Returns a time format specifier string for the given time units. The optional specifiers object provides a set of desired specifier sub-strings for customizing the resulting time formats. The specifiers object may contain keys for both single time units ("year"
) and time unit sequences ("year-month-date"
). This method will first standardize the input time units using the timeUnits method. It will then search, starting from the beginning of the units array, for the largest matching sequence defined in the specifiers object. Matching entries are then concatenated together, and the resulting string is whitespace-trimmed and returned.
If no specifiers object is provided, the following defaults are used:
{
"year": "%Y ",
"year-month": "%Y-%m ",
"year-month-date": "%Y-%m-%d ",
"quarter": "Q%q ",
"month": "%b ",
"date": "%d ",
"week": "W%U ",
"day": "%a ",
"hours": "%H:00",
"hours-minutes": "%H:%M",
"minutes": "00:%M",
"seconds": ":%S",
"milliseconds": ".%L"
}
If a specifiers object is provided, its values are merged with the defaults above. As a result, for complete control callees may wish to override the multi-unit "year-month"
, "year-month-date"
, and "hours-minutes"
entries in addition to any individual unit entries. The input specifiers object can use a null
value to invalidate an entry in the defaults.
# vega.timeBin(options) <>
Determine a temporal binning scheme, for example to create a histogram. Based on the options provided given, this method will search over a space of possible time unit bins, applying constraints such as the maximum number of allowable bins. Given a set of options (see below), returns an object describing the binning scheme, in terms of units
and step
properties. These values can then be used as input to the timeFloor or utcFloor methods.
The supported options properties are:
- extent: (required) A two-element (
[min, max]
) array indicating the date range over which the bin values are defined. - maxbins: The maximum number of allowable bins (default
40
). There will often be fewer bins as the domain gets sliced at "nicely" rounded values.
Local Time Utilities
# vega.timeFloor(units[, step]) <>
Returns a function that performs flooring (truncation) of input dates to given time units in the local timezone. The units argument must be an array of valid time unit strings, for example ['year', 'month']
or ['week', 'date']
. The optional step argument (default 1) indicates the number of time unit steps (of the smallest provided unit) to include as part of the truncation scheme. For example, utcFloor(['quarter'])
is equivalent to utcFloor(['month'], 3)
.
# vega.timeInterval(unit) <>
Returns a d3-time interval for the given time unit in the local timezone.
# vega.timeOffset(unit, date[, step]) <>
Returns a new Date instance that offsets the given date by the specified time unit in the local timezone. The optional step argument indicates the number of time unit steps to offset by (default 1).
# vega.timeSequence(unit, start, stop[, step]) <>
Returns an array of Date instances from start (inclusive) to stop (exclusive), with each entry separated by the given time unit in the local timezone. The optional step argument indicates the number of time unit steps to take between each sequence entry (default 1).
# vega.dayofyear(date) <>
Returns the one-based day of the year for the given date, which should be either a Date
object or timestamp value.
# vega.week(date) <>
Returns the week number of the year for the given date, which should be either a Date
object or timestamp value. This function assumes Sunday-based weeks. Days before the first Sunday of the year are considered to be in week 0, the first Sunday of the year is the start of week 1, the second Sunday week 2, etc..
UTC Time Utilities
# vega.utcFloor(units[, step]) <>
Returns a function that performs flooring (truncation) of input dates to given time units in Coordinated Universal Time (UTC). The units argument must be an array of valid time unit strings, for example ['year', 'month']
or ['week', 'date']
. The optional step argument (default 1) indicates the number of time unit steps (of the smallest provided unit) to include as part of the truncation scheme. For example, utcFloor(['quarter'])
is equivalent to utcFloor(['month'], 3)
.
# vega.utcInterval(unit) <>
Returns a d3-time interval for the given time unit in Coordinated Universal Time (UTC).
# vega.utcOffset(unit, date[, step]) <>
Returns a new Date instance that offsets the given date by the specified time unit in Coordinated Universal Time (UTC). The optional step argument indicates the number of time unit steps to offset by (default 1).
# vega.utcSequence(unit, start, stop[, step]) <>
Returns an array of Date instances from start (inclusive) to stop (exclusive), with each entry separated by the given time unit in Coordinated Universal Time (UTC). The optional step argument indicates the number of time unit steps to take between each sequence entry (default 1).
# vega.utcdayofyear(date) <>
Returns the one-based day of the year for the given date in Coordinated Universal Time (UTC), which should be either a Date
object or timestamp value.
# vega.utcweek(date) <>
Returns the week number of the year for the given date in Coordinated Universal Time (UTC), which should be either a Date
object or timestamp value. This function assumes Sunday-based weeks. Days before the first Sunday of the year are considered to be in week 0, the first Sunday of the year is the start of week 1, the second Sunday week 2, etc..