Skip to content

Commit ee9aa54

Browse files
committed
Use darker background for bar chart example
1 parent c8af872 commit ee9aa54

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

vignettes/cookbook/cookbook.Rmd

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,23 +241,26 @@ reactable(data, bordered = TRUE, columns = list(
241241
Another way to create bar charts is to render them as background images.
242242
This example creates bar images using the
243243
[`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).
245246

246247
```{r}
247248
# 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") {
253252
position <- paste0(width * 100, "%")
254253
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)
255257
}
256258
list(
257259
backgroundImage = image,
258260
backgroundSize = paste("100%", height),
259261
backgroundRepeat = "no-repeat",
260-
backgroundPosition = "center"
262+
backgroundPosition = "center",
263+
color = color
261264
)
262265
}
263266
@@ -266,12 +269,14 @@ data <- mtcars[, 1:4]
266269
reactable(data, columns = list(
267270
mpg = colDef(
268271
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)
271276
),
272277
disp = colDef(
273278
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")
275280
}
276281
)
277282
))

0 commit comments

Comments
 (0)