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.26
Version: 0.0.28
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
6 changes: 5 additions & 1 deletion R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ build_config = function(chart) {

# Theme: merge global option with per-chart theme
theme = modifyList(as.list(getOption('gglite.theme')), as.list(chart$theme))
# For dark themes, auto-set the view background so facet subplots get a dark
# background (G2 uses transparent by default, making axis/grid hard to see).
if (isTRUE(theme$type %in% c('dark', 'classicDark')))
theme = modifyList(list(view = list(viewFill = '#141414')), theme)
if (length(theme)) config$theme = theme

# Faceting wraps the spec as a facet view
Expand Down Expand Up @@ -293,7 +297,7 @@ chart_html = function(chart, id = NULL, width = NULL, height = NULL) {
dark = isTRUE(chart$theme$type %in% c('dark', 'classicDark'))
w = if (!is.null(width)) paste0('width:', width, 'px;') else ''
h = if (!is.null(height)) paste0('height:', height, 'px;') else ''
bg = if (dark) 'background-color:#141414;' else ''
bg = if (dark) 'background-color:#141414;'

if (!is.null(threshold)) {
# Ensure container has min-height so IntersectionObserver can trigger
Expand Down
4 changes: 3 additions & 1 deletion examples/canvas.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ pushing axis tick labels inward. Each argument accepts a scalar (all sides) or
`c(top, right, bottom, left)` with `NA` to skip individual sides.

```{r}
p |> canvas(padding = c(0, 150, NA, 100), margin = 10, inset = c(10, 0, 20, -20))
p |>
canvas(margin = 10, padding = c(10, 150, NA, 100), inset = c(10, 0, 20, -20)) |>
theme_light(view = list(viewFill = '#fff3bf', plotFill = '#ffc9c9', mainFill = '#a5d8ff'))
```

## Renderer
Expand Down
18 changes: 18 additions & 0 deletions examples/themes.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ Themes control the overall visual appearance of the chart. Use helpers like
`theme_classic()`, `theme_dark()`, `theme_light()`, `theme_classic_dark()`,
and `theme_academy()` to set a built-in theme.

G2 provides five built-in themes in two pairs plus one standalone:

- **`classic` vs `light`**: both use a light background, but differ only in
their default color palette for categorical data. `light` uses the AntV Light
palette (e.g. `#1783FF`, `#00C9C9`, …), while `classic` (the default) uses
G2's `category10` palette (e.g. `#5B8FF9`, `#5AD8A6`, …). When there is only
a single data series, the two themes look identical because only one color is
used.

- **`dark` vs `classicDark`**: the same relationship as above but on a dark
background (`#141414`). `dark` uses the AntV Light palette colors on a dark
canvas; `classicDark` uses the `category10` palette colors on a dark canvas.
The only visible difference is the multi-series color palette.

- **`academy`**: a distinct style with custom fonts, color ramps, and decorative
grid filters; it is not related to the classic/light or dark/classicDark
pairs.

## Classic Theme (default)

```{r}
Expand Down
27 changes: 24 additions & 3 deletions tests/normalize-notebook.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
"""Normalize and copy an executed Jupyter notebook.

Strips execution metadata (execution counts, timestamps) that change between
runs and copies the normalized notebook to the target path. Used by CI to
produce a clean diff when deciding whether to open a PR.
runs and copies the normalized notebook to the target path. Also strips version
numbers from CDN resource URLs so that version bumps in upstream packages do
not trigger new PRs to update the .ipynb output.

Usage: python tests/normalize-notebook.py <source.ipynb> <target.ipynb>
"""
import json, sys
import json, re, sys


def strip_cdn_versions(s: str) -> str:
"""Remove version specifiers (e.g. @5, @v0.14.34) from CDN URLs in src/href attributes."""
return re.sub(r'((src|href)="https://[^"]*?)/@v?\d+(?:\.\d+)*', r'\1', s)


def normalize_value(v):
if isinstance(v, str):
return strip_cdn_versions(v)
if isinstance(v, list):
return [normalize_value(x) for x in v]
return v


def normalize(nb: dict) -> None:
Expand All @@ -15,6 +29,13 @@ def normalize(nb: dict) -> None:
cell.get('metadata', {}).pop('execution', None)
for out in cell.get('outputs', []):
out.pop('execution_count', None)
for key in ('text', 'data'):
if key in out:
val = out[key]
if isinstance(val, list):
out[key] = [normalize_value(x) for x in val]
elif isinstance(val, dict):
out[key] = {k: normalize_value(v) for k, v in val.items()}


if __name__ == '__main__':
Expand Down
18 changes: 18 additions & 0 deletions tests/testit/test-theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,21 @@ assert('theme_classic_dark() sets classicDark theme', {
chart = g2() |> mark_point() |> theme_classic_dark()
(chart$theme$type %==% 'classicDark')
})

assert('dark theme auto-sets viewFill in build_config', {
chart = g2(mtcars, hp ~ mpg) |> theme_dark()
config = build_config(chart)
(config$theme$view$viewFill %==% '#141414')
})

assert('classicDark theme auto-sets viewFill in build_config', {
chart = g2(mtcars, hp ~ mpg) |> theme_classic_dark()
config = build_config(chart)
(config$theme$view$viewFill %==% '#141414')
})

assert('light theme does not set viewFill', {
chart = g2(mtcars, hp ~ mpg) |> theme_light()
config = build_config(chart)
(is.null(config$theme$view))
})
Loading