Skip to content

Commit

Permalink
bugfix geom_point stroke param
Browse files Browse the repository at this point in the history
  • Loading branch information
tdhock committed Nov 13, 2023
1 parent b4eed26 commit 6f91b76
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion inst/htmljs/animint.js
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@ var animint = function (to_select, json_file) {
if (g_info.geom == "point") {
g_info.style_list = [
"opacity","stroke","stroke-width","stroke-dasharray","fill"];
style_on_funs["stroke-width"] = get_attr("stroke");
style_on_funs["stroke-width"] = get_stroke_width;
eActions = function (e) {
e.attr("cx", toXY("x", "x"))
.attr("cy", toXY("y", "y"))
Expand Down
48 changes: 23 additions & 25 deletions tests/testthat/test-renderer4-geom-point-stroke.R
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
acontext("geom_point_stroke")

stroke_in_R <- 5
p1 <- ggplot(mtcars, aes(wt, mpg)) +
geom_point(shape = 21, colour = "black", fill = "white",
size = 5, stroke = stroke_in_R)
viz <- animint(
param_stroke=ggplot(mtcars, aes(
wt, mpg)) +
geom_point(
shape = 21, colour = "black", fill = "white",
size = 5, stroke = stroke_in_R),
aes_stroke=ggplot(mtcars, aes(
wt, mpg, stroke=cyl)) +
geom_point(
shape = 21, colour = "black", fill = "white", size = 5))

p2 <- ggplot(mtcars, aes(wt, mpg, stroke=cyl)) +
geom_point(shape = 21, colour = "black", fill = "white", size = 5)

viz <- list(p1=p1, p2=p2)
info <- animint2HTML(viz)

test_that("points are rendered with stroke-width", {
stroke_vals <-
getStyleValue(info$html, '//g[@class="geom1_point_p1"]//circle',
"stroke-width")
# stroke-width is rendered for every point
test_that("geom_point stroke param rendered with stroke-width", {
stroke_vals <- getStyleValue(
info$html,
'//g[@class="geom1_point_param_stroke"]//circle',
"stroke-width")
expect_equal(length(stroke_vals), length(mtcars$wt))

stroke_vals_unique <- unique(stroke_vals)
expect_equal(length(stroke_vals_unique), 1)

stroke_width_val <- as.numeric(gsub("[^0-9]", "", stroke_vals_unique))
expect_equal(stroke_width_val, stroke_in_R)
})

test_that("aes(stroke) works", {
stroke_vals_2 <-
getStyleValue(info$html, '//g[@class="geom2_point_p2"]//circle',
"stroke-width")

expect_equal(length(stroke_vals_2), length(mtcars$wt))

stroke_vals_unique_2 <- unique(stroke_vals_2)
expect_equal(length(stroke_vals_unique_2), length(unique(mtcars$cyl)))

# Check that the values of the stroke are taken from mtcars$cyl
stroke_width_vals <- as.numeric(gsub("[^0-9]", "", stroke_vals_unique_2))
stroke_vals_aes <- getStyleValue(
info$html,
'//g[@class="geom2_point_aes_stroke"]//circle',
"stroke-width")
expect_equal(length(stroke_vals_aes), length(mtcars$wt))
stroke_vals_unique_aes <- unique(stroke_vals_aes)
expect_equal(length(stroke_vals_unique_aes), length(unique(mtcars$cyl)))
stroke_width_vals <- as.numeric(gsub("[^0-9]", "", stroke_vals_unique_aes))
expect_identical(sort(stroke_width_vals), sort(unique(mtcars$cyl)))
})

0 comments on commit 6f91b76

Please sign in to comment.