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.
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.
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.
Options
:symbol- currency symbol (default: "$"):precision- decimal places (default: 2)
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"]
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,^centre,=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.