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+ }
0 commit comments