Skip to content

Commit e0d1d31

Browse files
committed
configure axis
1 parent 0b62217 commit e0d1d31

6 files changed

Lines changed: 136 additions & 6 deletions

File tree

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ export(JS)
44
export(aes)
55
export(chartgpu)
66
export(chartgpuOutput)
7+
export(chartgpu_axis_x)
8+
export(chartgpu_axis_y)
79
export(chartgpu_options)
810
export(chartgpu_serie)
911
export(chartgpu_theme)

R/options.R

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#'
1010
#' @example examples/chartgpu_options.R
1111
chartgpu_options <- function(chart, ...) {
12-
stopifnot("`chart` must have been created with `chartgpu`" = inherits(chart, "chartgpu"))
12+
stopifnot("`chart` must have been created with `chartgpu()`" = inherits(chart, "chartgpu"))
1313
chart$x$options <- modifyList(
1414
x = chart$x$options,
1515
val = dropNulls(list(...))
@@ -33,7 +33,7 @@ chartgpu_options <- function(chart, ...) {
3333
#'
3434
#' @example examples/chartgpu_serie.R
3535
chartgpu_serie <- function(chart, name, label = NULL, color = NULL, ...) {
36-
stopifnot("`chart` must have been created with `chartgpu`" = inherits(chart, "chartgpu"))
36+
stopifnot("`chart` must have been created with `chartgpu()`" = inherits(chart, "chartgpu"))
3737
stopifnot("`name` must be of length 1" = length(name) == 1)
3838
serie <- dropNulls(list(name = label, color = color, ...))
3939
if (!is.null(serie$data))
@@ -68,7 +68,7 @@ chartgpu_serie <- function(chart, name, label = NULL, color = NULL, ...) {
6868
#'
6969
#' @example examples/chartgpu_theme.R
7070
chartgpu_theme <- function(chart, palette = NULL, ...) {
71-
stopifnot("`chart` must have been created with `chartgpu`" = inherits(chart, "chartgpu"))
71+
stopifnot("`chart` must have been created with `chartgpu()`" = inherits(chart, "chartgpu"))
7272
chart$x$options$theme <- list(...)
7373
chart$x$options$palette <- list1(palette)
7474
return(chart)
@@ -78,7 +78,7 @@ chartgpu_theme <- function(chart, palette = NULL, ...) {
7878
#'
7979
#' @rdname chartgpu-theme
8080
chartgpu_theme_light <- function(chart, palette = NULL) {
81-
stopifnot("`chart` must have been created with `chartgpu`" = inherits(chart, "chartgpu"))
81+
stopifnot("`chart` must have been created with `chartgpu()`" = inherits(chart, "chartgpu"))
8282
chart$x$options$theme <- "light"
8383
chart$x$options$palette <- list1(palette)
8484
return(chart)
@@ -88,7 +88,7 @@ chartgpu_theme_light <- function(chart, palette = NULL) {
8888
#'
8989
#' @rdname chartgpu-theme
9090
chartgpu_theme_dark <- function(chart, palette = NULL) {
91-
stopifnot("`chart` must have been created with `chartgpu`" = inherits(chart, "chartgpu"))
91+
stopifnot("`chart` must have been created with `chartgpu()`" = inherits(chart, "chartgpu"))
9292
chart$x$options$theme <- "dark"
9393
chart$x$options$palette <- list1(palette)
9494
return(chart)
@@ -112,7 +112,7 @@ chartgpu_theme_dark <- function(chart, palette = NULL) {
112112
#' @example examples/chartgpu_zoom.R
113113
chartgpu_zoom <- function(chart, type = c("inside", "slider"), ...) {
114114
type <- match.arg(type, several.ok = TRUE)
115-
stopifnot("`chart` must have been created with `chartgpu`" = inherits(chart, "chartgpu"))
115+
stopifnot("`chart` must have been created with `chartgpu()`" = inherits(chart, "chartgpu"))
116116
dataZoom <- chart$x$options$dataZoom
117117
for (type_i in type) {
118118
if (length(dataZoom) < 1) {
@@ -139,5 +139,43 @@ chartgpu_zoom <- function(chart, type = c("inside", "slider"), ...) {
139139

140140

141141

142+
#' X-Axis Options
143+
#'
144+
#' @param chart A chart created with [chartgpu()].
145+
#' @param ... Options for x-axis,
146+
#' see [https://github.com/ChartGPU/ChartGPU/blob/main/docs/api/options.md#axis-configuration](https://github.com/ChartGPU/ChartGPU/blob/main/docs/api/options.md#axis-configuration).
147+
#'
148+
#' @returns A [chartgpu()] `htmlwidget` object.
149+
#' @export
150+
#'
151+
#' @example examples/chartgpu_axis_x.R
152+
chartgpu_axis_x <- function(chart, ...) {
153+
stopifnot("`chart` must have been created with `chartgpu()`" = inherits(chart, "chartgpu"))
154+
chart$x$options$xAxis <- modifyList(
155+
x = chart$x$options$xAxis %||% list(),
156+
val = dropNulls(list(...))
157+
)
158+
return(chart)
159+
}
160+
161+
162+
#' Y-Axis Options
163+
#'
164+
#' @param chart A chart created with [chartgpu()].
165+
#' @param ... Options for y-axis,
166+
#' see [https://github.com/ChartGPU/ChartGPU/blob/main/docs/api/options.md#axis-configuration](https://github.com/ChartGPU/ChartGPU/blob/main/docs/api/options.md#axis-configuration).
167+
#'
168+
#' @returns A [chartgpu()] `htmlwidget` object.
169+
#' @export
170+
#'
171+
#' @example examples/chartgpu_axis_y.R
172+
chartgpu_axis_y <- function(chart, ...) {
173+
stopifnot("`chart` must have been created with `chartgpu()`" = inherits(chart, "chartgpu"))
174+
chart$x$options$yAxis <- modifyList(
175+
x = chart$x$options$yAxis %||% list(),
176+
val = dropNulls(list(...))
177+
)
178+
return(chart)
179+
}
142180

143181

examples/chartgpu_axis_x.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
# Provide name for axis
3+
chartgpu(ggplot2::economics, aes(date, psavert)) |>
4+
chartgpu_axis_x(name = "Date")
5+
6+
# Custom date locale for axis labels
7+
chartgpu(ggplot2::economics, aes(date, psavert)) |>
8+
chartgpu_axis_x(
9+
tickFormatter = JS("(ms) => new Date(ms).toLocaleDateString('de-DE')")
10+
)
11+

examples/chartgpu_axis_y.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
# Provide name for axis
3+
chartgpu(ggplot2::economics, aes(date, psavert)) |>
4+
chartgpu_axis_y(name = "personal savings rate")
5+
6+
# Custom labels for ticks + axis minimum/maximum
7+
chartgpu(ggplot2::economics, aes(date, psavert)) |>
8+
chartgpu_axis_y(
9+
min = 0,
10+
max = 20,
11+
tickFormatter = JS("(v) => `${v.toFixed(0)} %`")
12+
)

man/chartgpu_axis_x.Rd

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/chartgpu_axis_y.Rd

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)