added format_num() and d3-format

This commit is contained in:
pvictor 2019-11-26 12:49:03 +01:00
parent 00fe467dcc
commit 267793ff5a
65 changed files with 542 additions and 13 deletions

View File

@ -1,5 +1,5 @@
Package: apexcharter
Version: 0.1.2.910
Version: 0.1.2.920
Title: Create Interactive Chart with the JavaScript 'ApexCharts' Library
Description: Provides an 'htmlwidgets' interface to 'apexcharts.js'.
'Apexcharts' is a modern JavaScript charting library to build interactive charts and visualizations with simple API.

View File

@ -35,6 +35,7 @@ export(ax_yaxis)
export(ax_yaxis2)
export(bar_opts)
export(events_opts)
export(format_num)
export(heatmap_opts)
export(parse_df)
export(pie_opts)

View File

@ -1,3 +1,11 @@
apexcharter 0.1.3
==================
* Upgraded ApexCharts.js to 3.10.1
* New function `format_num()` to format labels on y-axis for example.
apexcharter 0.1.2
==================

38
R/format.R Normal file
View File

@ -0,0 +1,38 @@
#' Format numbers (with D3)
#'
#' @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 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}.
#'
#' @return a \code{JS} function
#' @export
#'
#' @importFrom htmlwidgets JS
#'
#' @example examples/format.R
format_num <- function(format, 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
))
}
check_locale <- function(x) {
json <- list.files(system.file("htmlwidgets/lib/d3-format/locale", package = "apexcharter"))
njson <- gsub("\\.json", "", json)
if (!x %in% njson) {
stop(paste(
"Invalid D3 locale, must be one of:",
paste(njson, collapse = ", ")
), call. = FALSE)
}
}

52
examples/format.R Normal file
View File

@ -0,0 +1,52 @@
# 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", "fr-FR")
))

View File

@ -3,7 +3,7 @@ dependencies:
version: 3.10.1
src: htmlwidgets/lib/apexcharts-3.10
script: apexcharts.min.js
- name: numeral
version: 2.0.6
src: htmlwidgets/lib/numeral
script: numeral.min.js
- name: d3-format
version: 1.4.2
src: htmlwidgets/lib/d3-format
script: d3-format.min.js

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["", ""],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["", " \u062f\u002e\u0625\u002e"],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["", " \u062f\u002e\u0628\u002e"],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["\u200f\u0046\u0064\u006a ", ""],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,6 @@
{
"decimal": "\u002c",
"thousands": "\u002e",
"grouping": [3],
"currency": ["\u062f\u002e\u062c\u002e ", ""]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["", " \u062c\u002e\u0645\u002e"],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,6 @@
{
"decimal": "\u002e",
"thousands": "\u002c",
"grouping": [3],
"currency": ["\u062f\u002e\u0645\u002e ", ""]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["\u004e\u0066\u006b ", ""],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["\u20aa ", ""],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["", " \u062f\u002e\u0639\u002e"],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["", " \u062f\u002e\u0623\u002e"],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["", " \u0641\u002e\u062c\u002e\u0642\u002e"],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["", " \u062f\u002e\u0643\u002e"],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["", " \u0644\u002e\u0644\u002e"],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,6 @@
{
"decimal": "\u002c",
"thousands": "\u002e",
"grouping": [3],
"currency": ["\u062f\u002e\u0644\u002e ", ""]
}

View File

@ -0,0 +1,6 @@
{
"decimal": "\u002c",
"thousands": "\u002e",
"grouping": [3],
"currency": ["\u062f\u002e\u0645\u002e ", ""]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["", " \u0623\u002e\u0645\u002e"],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["", " \u0631\u002e\u0639\u002e"],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["\u20aa ", ""],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["", " \u0631\u002e\u0642\u002e"],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["", " \u0631\u002e\u0633\u002e"],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["", " \u062c\u002e\u0633\u002e"],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["\u200f\u0053 ", ""],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["\u00a3 ", ""],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["", " \u0644\u002e\u0633\u002e"],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["\u200f\u0046\u0043\u0046\u0041 ", ""],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,6 @@
{
"decimal": "\u002c",
"thousands": "\u002e",
"grouping": [3],
"currency": ["\u062f\u002e\u062a\u002e ", ""]
}

View File

@ -0,0 +1,7 @@
{
"decimal": "\u066b",
"thousands": "\u066c",
"grouping": [3],
"currency": ["", " \u0631\u002e\u0649\u002e"],
"numerals" : ["\u0660", "\u0661", "\u0662", "\u0663", "\u0664", "\u0665", "\u0666", "\u0667", "\u0668", "\u0669"]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["", "\u00a0€"]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ",",
"thousands": "\u00a0",
"grouping": [3],
"currency": ["", "\u00a0Kč"]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ",",
"thousands": "'",
"grouping": [3],
"currency": ["", "\u00a0CHF"]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["", "\u00a0€"]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["$", ""]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["£", ""]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["€", ""]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ".",
"thousands": ",",
"grouping": [3, 2, 2, 2, 2, 2, 2, 2, 2, 2],
"currency": ["₹", ""]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["$", ""]
}

View File

@ -0,0 +1,7 @@
{
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["Bs\u00a0", ""],
"percent": "\u202f%"
}

View File

@ -0,0 +1,6 @@
{
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["", "\u00a0€"]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["$", ""]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ",",
"thousands": "\u00a0",
"grouping": [3],
"currency": ["", "\u00a0€"]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ",",
"thousands": "\u00a0",
"grouping": [3],
"currency": ["", "$"]
}

View File

@ -0,0 +1,7 @@
{
"decimal": ",",
"thousands": "\u00a0",
"grouping": [3],
"currency": ["", "\u00a0€"],
"percent": "\u202f%"
}

View File

@ -0,0 +1,6 @@
{
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["₪", ""]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ",",
"thousands": "\u00a0",
"grouping": [3],
"currency": ["", "\u00a0Ft"]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["€", ""]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["", "円"]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["₩", ""]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["", "\u00a0ден."]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["€\u00a0", ""]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["", "zł"]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ",",
"thousands": ".",
"grouping": [3],
"currency": ["R$", ""]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ",",
"thousands": "\u00a0",
"grouping": [3],
"currency": ["", "\u00a0руб."]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ",",
"thousands": "\u00a0",
"grouping": [3],
"currency": ["", " kr"]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ",",
"thousands": "\u00a0",
"grouping": [3],
"currency": ["", "\u00a0₴."]
}

View File

@ -0,0 +1,6 @@
{
"decimal": ".",
"thousands": ",",
"grouping": [3],
"currency": ["¥", ""]
}

File diff suppressed because one or more lines are too long

75
man/format_num.Rd Normal file
View File

@ -0,0 +1,75 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/format.R
\name{format_num}
\alias{format_num}
\title{Format numbers (with D3)}
\usage{
format_num(format, 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{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}.}
}
\value{
a \code{JS} function
}
\description{
Format numbers (with D3)
}
\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", "fr-FR")
))
}