format_num() : added prefix and suffix args

This commit is contained in:
pvictor 2019-11-26 13:09:47 +01:00
parent 267793ff5a
commit 4f2e8af93e
3 changed files with 32 additions and 10 deletions

View File

@ -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
))
}

View File

@ -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")
))

View File

@ -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")
))
}