@@ -241,23 +241,26 @@ reactable(data, bordered = TRUE, columns = list(
241
241
Another way to create bar charts is to render them as background images.
242
242
This example creates bar images using the
243
243
[ ` linear-gradient() ` ] ( https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient() )
244
- CSS function, and was inspired by an example from https://rstudio.github.io/DT/010-style.html .
244
+ CSS function, and was inspired by an example from
245
+ [ DT] ( https://rstudio.github.io/DT/010-style.html ) .
245
246
246
247
``` {r}
247
248
# Render a bar chart in the background of the cell
248
- bar_style <- function(width = 1, fill = "#ddd", height = "75%", right = FALSE) {
249
- if (right) {
250
- position <- paste0(100 - width * 100, "%")
251
- image <- sprintf("linear-gradient(90deg, transparent %1$s, %2$s %1$s)", position, fill)
252
- } else {
249
+ bar_style <- function(width = 1, fill = "#e6e6e6", height = "75%", align = c("left", "right"), color = NULL) {
250
+ align <- match.arg(align)
251
+ if (align == "left") {
253
252
position <- paste0(width * 100, "%")
254
253
image <- sprintf("linear-gradient(90deg, %1$s %2$s, transparent %2$s)", fill, position)
254
+ } else {
255
+ position <- paste0(100 - width * 100, "%")
256
+ image <- sprintf("linear-gradient(90deg, transparent %1$s, %2$s %1$s)", position, fill)
255
257
}
256
258
list(
257
259
backgroundImage = image,
258
260
backgroundSize = paste("100%", height),
259
261
backgroundRepeat = "no-repeat",
260
- backgroundPosition = "center"
262
+ backgroundPosition = "center",
263
+ color = color
261
264
)
262
265
}
263
266
@@ -266,12 +269,14 @@ data <- mtcars[, 1:4]
266
269
reactable(data, columns = list(
267
270
mpg = colDef(
268
271
style = function(value) {
269
- bar_style(width = value / max(data$mpg), fill = "#a5d6f3")
270
- }
272
+ bar_style(width = value / max(data$mpg), fill = "#2c5e77", color = "#fff")
273
+ },
274
+ align = "left",
275
+ format = colFormat(digits = 1)
271
276
),
272
277
disp = colDef(
273
278
style = function(value) {
274
- bar_style(width = value / max(data$disp), height = "90%", right = TRUE )
279
+ bar_style(width = value / max(data$disp), height = "90%", align = "right" )
275
280
}
276
281
)
277
282
))
0 commit comments