Visualize.Format (Visualize v0.2.25)

Copy Markdown View Source

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

currency(value, opts \\ [])

@spec currency(number(), keyword()) :: String.t()

Formats currency.

Options

  • :symbol - currency symbol (default: "$")
  • :precision - decimal places (default: 2)

duration(seconds)

@spec duration(number()) :: String.t()

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"]

exponential(value, opts \\ [])

@spec exponential(number(), keyword()) :: String.t()

Formats a number in exponential notation.

fixed(value, precision)

@spec fixed(number(), integer()) :: String.t()

Formats a number with fixed decimal places.

formatter(specifier)

@spec formatter(String.t()) :: (number() -> String.t())

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]
  • fill is any character, used with align: > 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 the 0b/0o/0x prefix of the b/o/x/X types.
  • 0 pads with zeros after the sign and symbol; width is the minimum output width; , groups thousands; .precision is the number of digits after the point (e, f, %) or of significant digits (g, r, s, p); ~ trims insignificant trailing zeros.
  • type: e exponential, f fixed, g general (either, by magnitude), r rounded to significant digits, s SI prefix, % percentage fixed, p percentage rounded, d integer, b/o/x/X integer in base 2/8/16, c the value as a string, n for ,g; no type is ~g with 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.

number(value, opts \\ [])

@spec number(number(), keyword()) :: String.t()

Formats a number with thousands separators.

Options

  • :precision - decimal places (default: auto)
  • :separator - thousands separator (default: ",")
  • :decimal - decimal separator (default: ".")

percent(value, opts \\ [])

@spec percent(number(), keyword()) :: String.t()

Formats a number as a percentage.

Options

  • :precision - decimal places (default: 1)
  • :multiply - multiply by 100 (default: true)

si(value, opts \\ [])

@spec si(number(), keyword()) :: String.t()

Formats a number with SI prefix (k, M, G, etc.).

Options

  • :precision - significant digits (default: 3)

time(datetime, format \\ "%Y-%m-%d")

@spec time(DateTime.t() | Date.t() | NaiveDateTime.t(), String.t()) :: String.t()

Formats a date/time value with strftime-style directives.

DirectiveValue
%Yfour-digit year
%ytwo-digit year
%mmonth 01–12
%dday of month 01–31
%eday of month, space-padded 1–31
%jday of year 001–366
%Hhour 00–23
%Ihour 01–12
%Mminute 00–59
%Ssecond 00–60
%Lmilliseconds 000–999
%pAM or PM
%aabbreviated weekday name
%Afull weekday name
%babbreviated month name
%Bfull month name
%Uweek of the year, Sunday first, 00–53
%wweekday as a number, Sunday 0
%Ztime zone abbreviation (DateTime only; otherwise empty)
%ztime zone offset +hhmm (DateTime only; otherwise empty)
%%a literal %

A Date has the time fields at zero. An unknown directive passes through verbatim.