diff --git a/R/pricesensitivitymeter.R b/R/pricesensitivitymeter.R index 365d8d2..6adf1de 100644 --- a/R/pricesensitivitymeter.R +++ b/R/pricesensitivitymeter.R @@ -367,6 +367,17 @@ PriceSensitivityMeter <- function(x, edits = list(annotationPosition = FALSE, annotationText = FALSE, axisTitleText = FALSE, titleText = FALSE, legendText = FALSE)) attr(pp, "ChartData") <- psm.dat + # Expose the chart title (forwarded to Line via ...) as a ChartLabels$ChartTitle + # attribute so downstream consumers can label the chart's data. Merge into any + # ChartLabels already set by Line rather than overwriting it. + chart.title <- list(...)[["title"]] + if (!is.null(chart.title) && any(nzchar(chart.title))) { + chart.labels <- attr(pp, "ChartLabels") + if (is.null(chart.labels)) + chart.labels <- list() + chart.labels$ChartTitle <- chart.title + attr(pp, "ChartLabels") <- chart.labels + } return(pp) } diff --git a/tests/testthat/test-pricesensitivitymeter.R b/tests/testthat/test-pricesensitivitymeter.R index b78cd8e..52dbf93 100644 --- a/tests/testthat/test-pricesensitivitymeter.R +++ b/tests/testthat/test-pricesensitivitymeter.R @@ -231,3 +231,15 @@ test_that("PSM with two axes", y2.title.font.family = "Courier New", y2.title.font.color = "#0000FF", y2.bounds.minimum = 0.5, y2.bounds.maximum = 5, y2.title = "R"), NA) }) + +test_that("Chart title exposed as ChartLabels$ChartTitle", +{ + psm <- PriceSensitivityMeter(dat, title = "My price sensitivity meter") + expect_equal(attr(psm, "ChartLabels")$ChartTitle, "My price sensitivity meter") + # ChartData is still attached alongside the title + expect_false(is.null(attr(psm, "ChartData"))) + + # No ChartTitle is exposed when the title is empty + psm.no.title <- PriceSensitivityMeter(dat) + expect_null(attr(psm.no.title, "ChartLabels")$ChartTitle) +})