format_num: Format numbers (with D3)

View source: R/format.R

format_numR Documentation

Format numbers (with D3)

Description

Format numbers (with D3)

Usage

format_num(format, prefix = "", suffix = "", locale = "en-US")

Arguments

format

Format for numbers, currency, percentage, e.g. ".0%" for rounded percentage. See online documentation : https://github.com/d3/d3-format.

prefix

Character string to append before formatted value.

suffix

Character string to append after formatted value.

locale

Localization to use, for example "fr-FR" for french, see possible values here: https://github.com/d3/d3-format/tree/master/locale.

Value

a JS function

Examples

# Use SI prefix
dat <- data.frame(
  labels = c("apex", "charts"),
  values = c(1e4, 2e4)
)

apex(dat, aes(labels, values), "column") %>% 
  ax_yaxis(labels = list(
    formatter = format_num("~s")
  ))

apex(dat, aes(labels, values * 100), "column") %>% 
  ax_yaxis(labels = list(
    formatter = format_num("~s")
  ))


# Percentage
dat <- data.frame(
  labels = c("apex", "charts"),
  values = c(0.45, 0.55)
)

apex(dat, aes(labels, values), "column") %>% 
  ax_yaxis(labels = list(
    formatter = format_num(".0%")
  ))


# Currency
dat <- data.frame(
  labels = c("apex", "charts"),
  values = c(570, 1170)
)

apex(dat, aes(labels, values), "column") %>% 
  ax_yaxis(labels = list(
    formatter = format_num("$,.2f")
  ))

# Change locale
apex(dat, aes(labels, values), "column") %>% 
  ax_yaxis(labels = list(
    formatter = format_num("$,.2f", locale = "fr-FR")
  ))



# Customize tooltip value
# Use SI prefix
dat <- data.frame(
  labels = c("apex", "charts"),
  values = c(1e4, 2e4)
)

apex(dat, aes(labels, values), "column") %>% 
  ax_tooltip(y = list(
    formatter = format_num(",", suffix = " GW/h")
  ))


apexcharter documentation built on July 9, 2023, 7:55 p.m.