Number and time formatting utilities for chart labels.
formatter/1 is a d3-format specifier parser; time/2 a strftime-style directive
formatter. The simpler functions (number/2, si/2, percent/2, …) are presets with
keyword options.
Examples
Visualize.Format.number(1234567)
# => "1,234,567"
Visualize.Format.si(1234567)
# => "1.23M"
Visualize.Format.percent(0.1234)
# => "12.3%"
Visualize.Format.formatter("($.2f").(-1234.5)
# => "($1,234.50)"
Summary
Functions
Formats currency (spec/05 §2.6): by a symbol, or by an ISO 4217 code.
A length of time in seconds, in the unit its magnitude chooses (spec/05 §2.9, #191): the
largest of d, h, m, s, ms the value reaches — a whole count as it is, one that
is whole in the next finer unit as both, any other to one decimal.
A length of time in seconds, in a style (spec/05 §2.9): :magnitude, the default, is
duration/1; :compound is the label and tooltip form — the two largest non-zero
whole units of d, h, m, s from a second up (1h 24m), milliseconds below a
second and nanoseconds below a microsecond, each to :precision decimals (default 2)
with trailing zeros trimmed (840ms). An axis keeps a single scaled unit.
Formats a number in exponential notation.
Formats a number with fixed decimal places.
Creates a number formatter function from a d3-format specifier.
Formats a number with thousands separators.
Formats a number as a percentage.
Formats a number with SI prefix (k, M, G, etc.).
Formats a date/time value with strftime-style directives.
Functions
Formats currency (spec/05 §2.6): by a symbol, or by an ISO 4217 code.
With :code, the code writes itself: its minor units are the precision, the locale's
separators group it, and the locale's symbol for the code goes before the number after
the sign — or the code after the number, for a code the locale has no symbol for.
Options
:symbol- currency symbol (default: "$"); not with:code:precision- decimal places (default: 2, or the code's minor units):code- an ISO 4217 code ofVisualize.Unit's table (spec/05 §4.1):locale- with:code, default"en"
Examples
iex> Visualize.Format.currency(1240, code: "USD")
"$1,240.00"
iex> Visualize.Format.currency(1240, code: "JPY")
"¥1,240"
iex> Visualize.Format.currency(-1240.5, code: "CHF")
"-1,240.50 CHF"
A length of time in seconds, in the unit its magnitude chooses (spec/05 §2.9, #191): the
largest of d, h, m, s, ms the value reaches — a whole count as it is, one that
is whole in the next finer unit as both, any other to one decimal.
Examples
iex> Enum.map([45, 150, 5400, 5430, 3660, 2.5, 0.25, 0, -90], &Visualize.Format.duration/1)
["45s", "2m30s", "1h30m", "1.5h", "1h1m", "2.5s", "250ms", "0s", "−1m30s"]
A length of time in seconds, in a style (spec/05 §2.9): :magnitude, the default, is
duration/1; :compound is the label and tooltip form — the two largest non-zero
whole units of d, h, m, s from a second up (1h 24m), milliseconds below a
second and nanoseconds below a microsecond, each to :precision decimals (default 2)
with trailing zeros trimmed (840ms). An axis keeps a single scaled unit.
Options
:style-:magnitude(default) or:compound:precision- the compound style's decimals below a second (default2):locale- default"en"
Examples
iex> Enum.map([5040, 0.84, 3603, 0, -90], &Visualize.Format.duration(&1, style: :compound))
["1h 24m", "840ms", "1h 3s", "0s", "-1m 30s"]
Formats a number in exponential notation.
Formats a number with fixed decimal places.
Creates a number formatter function from a d3-format specifier.
The specifier grammar is d3-format's:
[[fill]align][sign][symbol][0][width][,][.precision][~][type]fillis any character, used withalign:>right (default),<left,^center,=after the sign and symbol.sign:-(minus for negatives only, default),+(plus or minus),((parentheses for negatives), space (space for positives).symbol:$for a currency prefix,#for the0b/0o/0xprefix of theb/o/x/Xtypes.0pads with zeros after the sign and symbol;widthis the minimum output width;,groups thousands;.precisionis the number of digits after the point (e,f,%) or of significant digits (g,r,s,p);~trims insignificant trailing zeros.type:eexponential,ffixed,ggeneral (either, by magnitude),rrounded to significant digits,sSI prefix,%percentage fixed,ppercentage rounded,dinteger,b/o/x/Xinteger in base 2/8/16,cthe value as a string,nfor,g; no type is~gwith a default precision of 12.
Examples
Visualize.Format.formatter(".2s").(1234567) # => "1.2M"
Visualize.Format.formatter(",.0f").(1234567) # => "1,234,567"
Visualize.Format.formatter("+.1%").(0.123) # => "+12.3%"
Visualize.Format.formatter("($.2f").(-12.5) # => "($12.50)"
Visualize.Format.formatter("08.2f").(3.14159) # => "00003.14"
Visualize.Format.formatter("#x").(255) # => "0xff"An invalid specifier raises ArgumentError.
Formats a number with thousands separators.
Options
:precision- decimal places (default: auto):separator- thousands separator (default: ","):decimal- decimal separator (default: ".")
Formats a number as a percentage.
Options
:precision- decimal places (default: 1):multiply- multiply by 100 (default: true)
Formats a number with SI prefix (k, M, G, etc.).
Options
:precision- significant digits (default: 3)
@spec time(DateTime.t() | Date.t() | NaiveDateTime.t(), String.t()) :: String.t()
Formats a date/time value with strftime-style directives.
| Directive | Value |
|---|---|
%Y | four-digit year |
%y | two-digit year |
%m | month 01–12 |
%d | day of month 01–31 |
%e | day of month, space-padded 1–31 |
%j | day of year 001–366 |
%H | hour 00–23 |
%I | hour 01–12 |
%M | minute 00–59 |
%S | second 00–60 |
%L | milliseconds 000–999 |
%p | AM or PM |
%a | abbreviated weekday name |
%A | full weekday name |
%b | abbreviated month name |
%B | full month name |
%U | week of the year, Sunday first, 00–53 |
%w | weekday as a number, Sunday 0 |
%Z | time zone abbreviation (DateTime only; otherwise empty) |
%z | time zone offset +hhmm (DateTime only; otherwise empty) |
%% | a literal % |
A Date has the time fields at zero. An unknown directive passes through verbatim.