Documentation
¶
Overview ¶
Package format formats numbers and times with the syntax of d3-format and d3-time-format, which Vega uses for its axes, its legends and the functions format and timeFormat of its expressions. zuzu follows the documented behavior and is not a port (D6). The locale is US English by default (D27), and a Locale holds another.
Index ¶
- Variables
- func Number(spec string, loc Locale) (func(float64) string, error)
- func Parse(spec string, loc Locale, zone *time.Location) func(string) (time.Time, bool)
- func ParseDate(s string, zone *time.Location) (time.Time, bool)
- func PrecisionFixed(step float64) int
- func PrecisionPrefix(step, value float64) int
- func PrecisionRound(step, largest float64) int
- func Prefix(spec string, value float64, loc Locale) (func(float64) string, error)
- func Ticks(spec string, lo, hi, step float64, loc Locale) (func(float64) string, error)
- func Time(spec string, loc Locale) func(time.Time) string
- func TimeUnitSpecifier(units []string, specs map[string]string) string
- func WallTime(year, month, day, hour, minute, sec, ms int, loc *time.Location) time.Time
- type Error
- type Interval
- type Locale
- type Spec
Constants ¶
This section is empty.
Variables ¶
var EnUS = Locale{ Decimal: ".", Thousands: ",", Grouping: []int{3}, Currency: [2]string{"$", ""}, Minus: "−", Percent: "%", NaN: "NaN", DateTime: "%x, %X", Date: "%-m/%-d/%Y", Time: "%-I:%M:%S %p", Periods: [2]string{"AM", "PM"}, Days: [7]string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}, ShortDays: [7]string{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}, Months: [12]string{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}, ShortMonths: [12]string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}, }
EnUS is the locale of US English, the default of d3 and of zuzu (D27).
var Intervals = []Interval{Year, Quarter, Month, Week, Date, Day, DayOfYear, Hours, Minutes, Seconds, Milliseconds}
Intervals are the intervals, from the longest to the shortest.
var UnitSpecifiers = map[string]string{
"year": "%Y ",
"quarter": "Q%q ",
"month": "%b ",
"date": "%d ",
"week": "W%U ",
"day": "%a ",
"dayofyear": "%j ",
"hours": "%H:00",
"minutes": "00:%M",
"seconds": ":%S",
"milliseconds": ".%L",
"year-month": "%Y-%m ",
"year-month-date": "%Y-%m-%d ",
"hours-minutes": "%H:%M",
}
UnitSpecifiers are the default parts of the format spec that TimeUnitSpecifier joins, as Vega documents them for its function timeUnitSpecifier.
Functions ¶
func Number ¶
Number returns a function that formats a number with a format spec of d3-format, such as ",.2f" or "~s", in a locale.
func Parse ¶
Parse returns a function that parses a time with a format spec of d3-time-format. The function reads the time in zone, unless the text gives an offset with %Z. It reports false when the text does not match the spec, or has text after it. A field that the text does not give is the start of its range: January 1, 1900, at midnight. A field out of its range carries into the next, so the month 13 of a year is January of the next year.
func ParseDate ¶
ParseDate parses a date the way the Date of JavaScript parses a string, for the date formats that data and expressions give without a format spec.
The ISO form, such as 2024-02-29 or 2024-02-29T12:30:05.007+05:30, is first. A date with no time is in UTC, and a time with no offset is in zone. Other forms, such as 2024/02/29, 02/29/2024, Feb 29 2024 and February 29, 2024 1:30 PM, are in zone unless they give an offset. A year of two digits is in 1950 to 2049.
func PrecisionFixed ¶ added in v0.7.2
PrecisionFixed returns the number of decimals that fixed notation needs to tell apart two values that are step apart, such as 1 for 0.5 and 2 for 0.01, as precisionFixed of d3-format. It returns -1 for a step that is not a number.
func PrecisionPrefix ¶ added in v0.7.2
PrecisionPrefix returns the number of decimals that the notation of an SI prefix needs to tell apart two values that are step apart, where the prefix is the one of value, such as 1 for a step of 100000 and a value of 1300000 (1.3M), as precisionPrefix of d3-format. It returns -1 for a step or a value that is not a number.
func PrecisionRound ¶ added in v0.7.2
PrecisionRound returns the number of significant digits that tell apart two values that are step apart, up to the largest value, such as 3 for a step of 0.01 and a largest value of 1.01, as precisionRound of d3-format. It returns -1 for a step or a value that is not a number.
func Prefix ¶ added in v0.7.2
Prefix returns a function that formats a number in fixed notation, in the unit of the SI prefix of value, with that prefix after it, such as 1.5M for 1500000 when value is 4000000. Every number takes the same prefix, as formatPrefix of d3-format does. The type of the spec is ignored.
func Ticks ¶ added in v0.7.2
Ticks returns the function that writes the labels of the ticks of a scale of numbers from lo to hi, step apart, with a spec of d3-format, as tickFormat of d3-scale does. The spec ",f" is the default. If the spec gives no precision, the precision is the one that tells two ticks apart. The type s writes each label with the SI prefix of the largest end, such as 0M, 1M and 2M.
func Time ¶
Time returns a function that formats a time with a format spec of d3-time-format, such as "%b %d, %Y", in a locale. The function writes the time in its own location: pass t.UTC() for the UTC formats, and t.In(zone) for the local ones.
A directive is a % and a letter. A -, _ or 0 between them pads the number with nothing, spaces or zeros. A letter that is not a directive is written as it is, without the %.
func TimeUnitSpecifier ¶
TimeUnitSpecifier returns a format spec of d3-time-format for a set of time units, such as year and month. A part in specs takes the place of the default part of the same name in UnitSpecifiers. The parts are joined from the longest unit to the shortest. Year, month and date join as year-month-date, year and month as year-month, and hours and minutes as hours-minutes.
func WallTime ¶
WallTime returns the time that a clock in loc shows as the given fields, as the Date of JavaScript builds it. A field out of its range carries into the next. A time that the clock skips, when it moves forward, reads with the offset from before the move, so 2:30 on a day that skips from 2:00 to 3:00 is 3:30. A time that the clock shows twice, when it moves back, is the first of the two.
Types ¶
type Error ¶
type Error string
Error is an error of the package format.
const ErrInvalidSpec Error = "invalid format spec"
ErrInvalidSpec is the error of a format spec that does not parse.
type Interval ¶
type Interval string
Interval is a unit of time, such as a month or an hour, in the location of the times it takes. It has the names of the time units of Vega: year, quarter, month, week, date, day, dayofyear, hours, minutes, seconds and milliseconds. date, day and dayofyear are the same interval: a day. week starts on Sunday.
const ( Year Interval = "year" Quarter Interval = "quarter" Month Interval = "month" Week Interval = "week" Date Interval = "date" Day Interval = "day" DayOfYear Interval = "dayofyear" Hours Interval = "hours" Minutes Interval = "minutes" Seconds Interval = "seconds" Milliseconds Interval = "milliseconds" )
The intervals.
func (Interval) Offset ¶
Offset returns t moved by n intervals. A month, a quarter or a year keeps the day of the month, and a day past the end of the month carries into the next. A day or a week keeps the time of day.
type Locale ¶
type Locale struct {
Decimal string
Thousands string
Grouping []int
Currency [2]string
Minus string
Percent string
NaN string
DateTime string
Date string
Time string
Periods [2]string
Days [7]string
ShortDays [7]string
Months [12]string
ShortMonths [12]string
}
Locale holds the symbols of numbers and the names of times of a language.