Skip to content

Commit 87e49d1

Browse files
authored
Merge pull request #2 from Othman-Beqqal/master
added connectCharts, pie/donut and bar
2 parents e0d1d31 + 8bcf553 commit 87e49d1

22 files changed

Lines changed: 459 additions & 34 deletions

.DS_Store

10 KB
Binary file not shown.

.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@
1111
^data-raw$
1212
^examples$
1313
^\.github$
14+
^\.positai$
15+
^\.claude$

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
.Ruserdata
55
node_modules
66
*.Rproj
7+
.positai

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
export(JS)
44
export(aes)
5+
export(bargpu)
56
export(chartgpu)
67
export(chartgpuOutput)
78
export(chartgpu_axis_x)
@@ -12,6 +13,8 @@ export(chartgpu_theme)
1213
export(chartgpu_theme_dark)
1314
export(chartgpu_theme_light)
1415
export(chartgpu_zoom)
16+
export(donutgpu)
17+
export(piegpu)
1518
export(renderChartgpu)
1619
export(scattergpu)
1720
importFrom(ggplot2,aes)

R/bar.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#' Bar Chart with ChartGPU
2+
#'
3+
#' @param data A `data.frame`.
4+
#' @inheritParams chartgpu
5+
#' @inheritParams htmlwidgets::createWidget
6+
#'
7+
#' @returns A [chartgpu()] `htmlwidget` object.
8+
#' @export
9+
#'
10+
#' @example examples/chartgpu_bar.R
11+
bargpu <- function(data, mapping = NULL, width = NULL, height = NULL, elementId = NULL) {
12+
chartgpu(
13+
data = data,
14+
mapping = mapping,
15+
type = "bar",
16+
width = width,
17+
height = height,
18+
elementId = elementId
19+
)
20+
}

R/chartgpu.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#' @param mapping Optional mapping of variables to use if `data` is a `data.frame`
99
#' @param type Type of chart, if `data` is a `data.frame`.
1010
#' @param ... Additional parameters.
11+
#' @param group Group ID to link multiple charts together.
12+
#' @param sync_zoom Whether to sync zoom/pan across charts in the same group.
1113
#' @inheritParams htmlwidgets::createWidget
1214
#'
1315
#' @return A [chartgpu()] `htmlwidget` object.
@@ -16,7 +18,7 @@
1618
#'
1719
#' @export
1820
#' @example examples/chartgpu.R
19-
chartgpu <- function(data = NULL, mapping = NULL, type = NULL, ..., width = NULL, height = NULL, elementId = NULL) {
21+
chartgpu <- function(data = NULL, mapping = NULL, type = NULL, ..., group = NULL, sync_zoom = FALSE, width = NULL, height = NULL, elementId = NULL) {
2022

2123
if (inherits(data, "data.frame")) {
2224
x <- list(
@@ -35,7 +37,12 @@ chartgpu <- function(data = NULL, mapping = NULL, type = NULL, ..., width = NULL
3537
options = c(data, list(...))
3638
)
3739
}
38-
40+
41+
if (!is.null(group)) {
42+
x$syncGroup <- group
43+
x$syncZoom <- isTRUE(sync_zoom)
44+
}
45+
3946
attr(x, "TOJSON_ARGS") <- list(dataframe = "rows")
4047

4148
createWidget(

R/pie.R

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#' Pie Chart with ChartGPU
2+
#'
3+
#' @param data A `data.frame`.
4+
#' @param name Series name shown in the tooltip.
5+
#' @param radius Length-2 vector `c(inner, outer)`, e.g. `c(0, "72%")`.
6+
#' @param center Length-2 vector for the chart centre, e.g. `c("50%", "50%")`.
7+
#' @param start_angle Starting angle in degrees (default 90 = top).
8+
#' @inheritParams chartgpu
9+
#' @inheritParams htmlwidgets::createWidget
10+
#'
11+
#' @returns A [chartgpu()] `htmlwidget` object.
12+
#' @export
13+
#'
14+
#' @example examples/chartgpu_pie.R
15+
piegpu <- function(data,
16+
mapping = NULL,
17+
name = NULL,
18+
radius = c(0, "72%"),
19+
center = c("50%", "50%"),
20+
start_angle = 90,
21+
width = NULL,
22+
height = NULL,
23+
elementId = NULL) {
24+
25+
if (is.null(mapping)) {
26+
nms <- colnames(data)
27+
mapping <- list(
28+
label = nms[1],
29+
value = nms[2],
30+
color = if (length(nms) >= 3) nms[3] else NULL
31+
)
32+
}
33+
34+
slices <- lapply(seq_len(nrow(data)), function(i) {
35+
s <- list(name = as.character(data[[mapping$label]][i]),
36+
value = data[[mapping$value]][i])
37+
if (!is.null(mapping$color) && !is.na(data[[mapping$color]][i]))
38+
s$color <- data[[mapping$color]][i]
39+
s
40+
})
41+
42+
serie <- dropNulls(list(
43+
type = "pie",
44+
name = name,
45+
data = slices,
46+
radius = as.list(radius),
47+
center = as.list(center),
48+
startAngle = start_angle
49+
))
50+
51+
chartgpu(
52+
data = list(
53+
series = list(serie),
54+
xAxis = list(type = "value", min = 0, max = 1, tickLength = 0, name = ""),
55+
yAxis = list(type = "value", min = 0, max = 1, tickLength = 0, name = "")
56+
),
57+
width = width,
58+
height = height,
59+
elementId = elementId
60+
)
61+
}
62+
63+
#' Donut Chart with ChartGPU
64+
#'
65+
#' @param data A `data.frame`.
66+
#' @param name Series name shown in the tooltip.
67+
#' @param radius Length-2 vector `c(inner, outer)`, e.g. `c("40%", "72%")`.
68+
#' @param center Length-2 vector for the chart centre, e.g. `c("50%", "50%")`.
69+
#' @param start_angle Starting angle in degrees (default 90 = top).
70+
#' @inheritParams chartgpu
71+
#' @inheritParams htmlwidgets::createWidget
72+
#'
73+
#' @returns A [chartgpu()] `htmlwidget` object.
74+
#' @export
75+
#'
76+
#' @example examples/chartgpu_pie.R
77+
donutgpu <- function(data,
78+
mapping = NULL,
79+
name = NULL,
80+
radius = c("40%", "72%"),
81+
center = c("50%", "50%"),
82+
start_angle = 90,
83+
width = NULL,
84+
height = NULL,
85+
elementId = NULL) {
86+
piegpu(
87+
data = data,
88+
mapping = mapping,
89+
name = name,
90+
radius = radius,
91+
center = center,
92+
start_angle = start_angle,
93+
width = width,
94+
height = height,
95+
elementId = elementId
96+
)
97+
}

examples/.DS_Store

6 KB
Binary file not shown.

examples/chartgpu.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@ chartgpu(list(
4949
dataZoom = list(list(type = "inside"), list(type = "slider")),
5050
theme = "light"
5151
))
52+

examples/chartgpu_bar.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
library(rchartgpu)
2+
3+
bargpu(
4+
ggplot2::economics[, c("date", "unemploy")],
5+
height = "400px"
6+
)

0 commit comments

Comments
 (0)