Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: gglite
Title: Lightweight Data Visualization via the Grammar of Graphics
Version: 0.0.5
Version: 0.0.6
Authors@R: person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name",
comment = c(ORCID = "0000-0003-0645-5666"))
Description: A lightweight R interface to the AntV G2 JavaScript visualization
Expand Down
2 changes: 1 addition & 1 deletion R/gglite.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ g2_cdn = function() {
getOption('gglite.g2_cdn', 'https://unpkg.com/@antv/g2@5/dist/g2.min.js')
}

g2_col_cdn = 'https://cdn.jsdelivr.net/npm/@xiee/utils/js/g2-column.min.js'
g2_patches_cdn = 'https://cdn.jsdelivr.net/npm/@xiee/utils/js/g2-patches.min.js'

#' Process a layout argument (padding, margin, or inset)
#'
Expand Down
5 changes: 4 additions & 1 deletion R/mark.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ mark_line = function(chart, ...) mark_(chart, 'line', ...)
#' @export
#' @examples
#' g2(mtcars, x = 'mpg', y = 'hp', color = 'cyl') |> mark_point()
mark_point = function(chart, ...) mark_(chart, 'point', ...)
mark_point = function(chart, ...) {
opts = modifyList(list(style = list(shape = 'point')), list(...))
do.call(mark_, c(list(chart, 'point'), opts))
}

#' Add an Area Mark
#'
Expand Down
7 changes: 5 additions & 2 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ build_config = function(chart) {
if (length(chart$scales)) config$scale = chart$scales
if (!is.null(chart$coords)) config$coordinate = chart$coords
if (length(chart$interactions)) config$interaction = chart$interactions
if (!is.null(chart$theme)) config$theme = chart$theme
if (length(chart$axes)) config$axis = chart$axes
if (length(chart$legends)) config$legend = chart$legends
if (!is.null(chart$chart_title)) config$title = chart$chart_title
Expand All @@ -42,6 +41,10 @@ build_config = function(chart) {
if (!is.null(chart$scrollbars)) config$scrollbar = chart$scrollbars
if (length(chart$layout)) config = modifyList(config, chart$layout)

# Theme: merge global option with per-chart theme
theme = modifyList(as.list(getOption('gglite.theme')), as.list(chart$theme))
if (length(theme)) config$theme = theme

# Faceting wraps the spec as a facet view
if (!is.null(chart$facet)) {
config$type = chart$facet$type
Expand Down Expand Up @@ -129,7 +132,7 @@ chart_html = function(chart, id = NULL, width = NULL, height = NULL) {
}

cdn_scripts = function() {
sprintf('<script src="%s" defer></script>', c(g2_cdn(), g2_col_cdn))
sprintf('<script src="%s" defer></script>', c(g2_cdn(), g2_patches_cdn))
}

#' Preview a Chart in the Viewer or Browser
Expand Down
15 changes: 15 additions & 0 deletions R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@
#' G2 built-in themes: `'classic'` (default), `'classicDark'`, `'light'`,
#' `'dark'`, `'academy'`.
#'
#' To set global theme options for all charts, use `options(gglite.theme =
#' list(...))`. This is useful for changing default font sizes, grid line
#' visibility, and other theme properties without modifying each chart
#' individually. For example:
#'
#' ```r
#' options(gglite.theme = list(
#' title = list(titleFontSize = 20),
#' axis = list(labelFontSize = 16, gridStrokeOpacity = 0.3),
#' legendCategory = list(itemLabelFontSize = 14)
#' ))
#' ```
#'
#' Per-chart `theme_()` settings are merged on top of the global option.
#'
#' @param chart A `g2` object.
#' @param type Theme name string, or a list of custom theme options.
#' @param ... Additional theme options merged with the type.
Expand Down
14 changes: 14 additions & 0 deletions man/theme_.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions tests/testit/test-gglite.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ assert('mark_point() and mark_line() add layers', {
(chart$layers[[2]]$type %==% 'line')
})

# mark_point() defaults to solid shape; user can override
assert('mark_point() defaults to solid shape and respects user override', {
chart = g2(mtcars, x = 'mpg', y = 'hp') |> mark_point()
(chart$layers[[1]]$style$shape %==% 'point')
chart2 = g2(mtcars, x = 'mpg', y = 'hp') |>
mark_point(style = list(shape = 'hollow'))
(chart2$layers[[1]]$style$shape %==% 'hollow')
chart3 = g2(mtcars, x = 'mpg', y = 'hp') |>
mark_point(style = list(opacity = 0.5))
(chart3$layers[[1]]$style$shape %==% 'point')
(chart3$layers[[1]]$style$opacity %==% 0.5)
})

# build_config() produces correct spec with column-major data
assert('build_config() produces correct spec', {
df = data.frame(x = c('A', 'B'), y = c(3, 7))
Expand Down Expand Up @@ -100,6 +113,28 @@ assert('animate() sets animation options', {
(chart$layers[[1]]$animate$enter$type %==% 'fadeIn')
})

# build_config() respects global gglite.theme option
assert('build_config() merges gglite.theme option', {
old = options(gglite.theme = list(axis = list(labelFontSize = 20)))
chart = g2(mtcars, x = 'mpg', y = 'hp') |> mark_point()
config = build_config(chart)
options(old)
(config$theme$axis$labelFontSize %==% 20)
})

assert('gglite.theme merges with per-chart theme_()', {
old = options(gglite.theme = list(axis = list(labelFontSize = 20)))
chart = g2(mtcars, x = 'mpg', y = 'hp') |>
mark_point() |>
theme_('dark', axis = list(titleFontSize = 18))
config = build_config(chart)
options(old)
# per-chart type + titleFontSize win; labelFontSize from option preserved
(config$theme$type %==% 'dark')
(config$theme$axis$titleFontSize %==% 18)
(config$theme$axis$labelFontSize %==% 20)
})

# theme_(), axis_(), legend_(), title_() set chart options
assert('component functions set chart options', {
chart = g2(mtcars, x = 'mpg', y = 'hp') |>
Expand Down
Loading