diff --git a/R/format.R b/R/format.R index ec10eb9..8093479 100644 --- a/R/format.R +++ b/R/format.R @@ -3,6 +3,8 @@ #' #' @param format Format for numbers, currency, percentage, e.g. \code{".0\%"} for rounded percentage. #' See online documentation : \url{https://github.com/d3/d3-format}. +#' @param prefix Character string to append before formatd value. +#' @param suffix Character string to append after formatd value. #' @param locale Localization to use, for exemple \code{"fr-FR"} for french, #' see possible values here: \url{https://github.com/d3/d3-format/tree/master/locale}. #' @@ -12,15 +14,15 @@ #' @importFrom htmlwidgets JS #' #' @example examples/format.R -format_num <- function(format, locale = "en-US") { +format_num <- function(format, prefix = "", suffix = "", locale = "en-US") { check_locale(locale) path <- system.file(file.path("htmlwidgets/lib/d3-format/locale", paste0(locale, ".json")), package = "apexcharter") if (path != "") { locale <- paste(readLines(con = path, encoding = "UTF-8"), collapse = "") } JS(sprintf( - "function(value) {var locale = d3.formatLocale(JSON.parse('%s')); return locale.format('%s')(value);}", - locale, format + "function(value) {var locale = d3.formatLocale(JSON.parse('%s')); return '%s' + locale.format('%s')(value) + '%s';}", + locale, prefix, format, suffix )) } diff --git a/examples/format.R b/examples/format.R index ddecf20..70a9d8a 100644 --- a/examples/format.R +++ b/examples/format.R @@ -38,15 +38,23 @@ apex(dat, aes(labels, values), "column") %>% formatter = format_num("$,.2f") )) - # Change locale apex(dat, aes(labels, values), "column") %>% ax_yaxis(labels = list( - formatter = format_num("$,.2f", "fr-FR") + 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") + )) diff --git a/man/format_num.Rd b/man/format_num.Rd index 90ce8b8..0344ed4 100644 --- a/man/format_num.Rd +++ b/man/format_num.Rd @@ -4,12 +4,16 @@ \alias{format_num} \title{Format numbers (with D3)} \usage{ -format_num(format, locale = "en-US") +format_num(format, prefix = "", suffix = "", locale = "en-US") } \arguments{ \item{format}{Format for numbers, currency, percentage, e.g. \code{".0\%"} for rounded percentage. See online documentation : \url{https://github.com/d3/d3-format}.} +\item{prefix}{Character string to append before formatd value.} + +\item{suffix}{Character string to append after formatd value.} + \item{locale}{Localization to use, for exemple \code{"fr-FR"} for french, see possible values here: \url{https://github.com/d3/d3-format/tree/master/locale}.} } @@ -60,16 +64,24 @@ apex(dat, aes(labels, values), "column") \%>\% formatter = format_num("$,.2f") )) - # Change locale apex(dat, aes(labels, values), "column") \%>\% ax_yaxis(labels = list( - formatter = format_num("$,.2f", "fr-FR") + 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") + )) }