Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion R/pictographchart.R
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ PictographChart <- function(x,
}
x <- RemoveRowsAndOrColumns(x, row.names.to.remove, column.names.to.remove)

# Cleaned data matrix the pictograph represents, captured before x is
# rescaled for icon rendering, so it can be exposed as the chart's data.
chart.data <- x

# Data labels
label.data.values <- unlist(x) * (1+(99*label.data.100prc))
if (!customize.label.data && max(label.data.values) <= 1)
Expand Down Expand Up @@ -598,5 +602,7 @@ PictographChart <- function(x,
if(is.numeric(json))
f.mspace <- f.mspace + json
}
return(graphic(json))
g <- graphic(json)
attr(g, "ChartData") <- chart.data
return(g)
}
15 changes: 13 additions & 2 deletions R/visualizenumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ VisualizeNumber <- function(x,
margin.bottom = 0,
...)
{
# Snapshot the input value before any normalisation (e.g. the percentage
# rescaling of x below) so it can be exposed unchanged as the chart's data.
chart.data <- x
# Title to expose as the chart's data label, or NULL when no title is shown.
chart.labels <- if (any(nzchar(text.above))) list(ChartTitle = text.above) else NULL

display <- switch(tolower(display), oval = "circle", circle = "circle", "number in an oval" = "circle",
rectangle = "rectangle", square = "rectangle", "number in a rectangle" = "rectangle",
number = "number",
Expand Down Expand Up @@ -299,7 +305,7 @@ VisualizeNumber <- function(x,
}
if (label.data.position == "None")
label.str <- ""
return(iconsWithText(value, fill.icon.color = fill.color,
picto <- iconsWithText(value, fill.icon.color = fill.color,
base.icon.color = base.color, maximum.value = maximum.value,
total.icons = total.icons, ..., # other icon parameters?
text.overlay = label.str, text.overlay.halign = tolower(label.data.halign),
Expand All @@ -322,7 +328,10 @@ VisualizeNumber <- function(x,
text.above.font.weight = tolower(text.above.font.weight),
background.color = if (background.opacity > 0) background.color else "transparent",
margin.top = margin.top, margin.right = margin.right,
margin.bottom = margin.bottom, margin.left = margin.left))
margin.bottom = margin.bottom, margin.left = margin.left)
attr(picto, "ChartData") <- chart.data
attr(picto, "ChartLabels") <- chart.labels
return(picto)
}

if (display %in% c("donut", "gauge", "bar"))
Expand Down Expand Up @@ -490,6 +499,8 @@ VisualizeNumber <- function(x,
p$sizingPolicy$browser$padding <- 0
class(p) <- c(class(p), "visualization-selector")
attr(p, "can-run-in-root-dom") <- TRUE
attr(p, "ChartData") <- chart.data
attr(p, "ChartLabels") <- chart.labels
return(p)
}

Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-pictographchart.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ test_that("Wide pictograph", {
# expect_error(suppressWarnings(PictographChart(x2, mode="bar", hide.base.image=T, graphic.width.inch=244/72, graphic.height.inch=583/72,
# show.label.data=T, label.data.position="Next to bar")), "Window is too narrow.")
})

test_that("ChartData exposed, no ChartTitle", {
res <- PictographChart(x1, show.legend = TRUE)
expect_equal(as.numeric(attr(res, "ChartData")), as.numeric(x1))
expect_null(attr(res, "ChartLabels"))
})
19 changes: 19 additions & 0 deletions tests/testthat/test-visualizenumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,22 @@ test_that("VisualizeNumber with SelectEntry",
expect_equal(attr(value, "format"), "%")
expect_equal(value, 0.122449, check.attributes = FALSE, tol = 1e-3)
})

test_that("ChartData and ChartTitle exposed on output",
{
# Main (non-icon) return path
res <- VisualizeNumber(0.4, maximum.value = 1.0, text.above = "My title")
expect_equal(as.numeric(attr(res, "ChartData")), 0.4)
expect_equal(attr(res, "ChartLabels")$ChartTitle, "My title")

# Icon/pictograph return path
res.icon <- VisualizeNumber(ParseText("40%"), display = "Pictograph (single icon)",
label.data.number.type = "Percentage",
maximum.value = ParseText("100%"), text.above = "Icon title")
expect_false(is.null(attr(res.icon, "ChartData")))
expect_equal(attr(res.icon, "ChartLabels")$ChartTitle, "Icon title")

# No ChartTitle exposed when text.above is empty (the default)
res.no.title <- VisualizeNumber(0.4, maximum.value = 1.0)
expect_null(attr(res.no.title, "ChartLabels")$ChartTitle)
})
Loading