Skip to content

Commit

Permalink
Create function for setting chart legend options
Browse files Browse the repository at this point in the history
  • Loading branch information
katieb1 committed Apr 30, 2024
1 parent 9c7d008 commit f53d4df
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Collate:
'chartInclude.R'
'chartInfo.R'
'chartOptionsFont.R'
'chartOptionsLegend.R'
'chartXAxis.R'
'chartYAxis.R'
'command.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export(chartId)
export(chartInclude)
export(chartInfo)
export(chartOptionsFont)
export(chartOptionsLegend)
export(chartXAxis)
export(chartYAxis)
export(command)
Expand Down
121 changes: 121 additions & 0 deletions R/chartOptionsLegend.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Copyright (c) 2024 Apex Resource Management Solution Ltd. (ApexRMS). All rights reserved.
# MIT License
#' @include AAAClassDefinitions.R
NULL

#' Modifies the legend settings for a \code{\link{Chart}}
#'
#' Modifies the legend settings for a \code{\link{Chart}}.
#'
#' @param chart \code{\link{Chart}} object
#' @param show logical. Whether to show the chart legend. Default is \code{NULL}.
#' @param showScenarioName logical. Whether to show the scenario name in the
#' legend. Default is \code{NULL}.
#' @param showScenarioID logical. Whether to show the scenario ID in the legend.
#' Default is \code{NULL}.
#' @param showStageName logical. Determines whether to show the stage name
#' (i.e., transformer name) in the legend. Default is \code{NULL}.
#' @param showTimestamp logical. Whether to show the timestamp of the scenario
#' run in the legend. Default is \code{NULL}. Default is \code{NULL}.
#'
#' @return
#' A \code{Chart} object representing a SyncroSim chart or, if no arguments
#' other than the chart are provided, a data.frame of the current chart legend
#' settings.
#'
#' @examples
#' \dontrun{
#' # Open a chart object
#' myChart <- chart(myProject, chart = "My Chart")
#'
#' # Remove the scenario ID and the timestamp from the chart
#' myChart <- chartOptionsLegend(myChart, showScenarioID = FALSE,
#' showTimestamp = FALSE)
#'
#' # Hide the chart legend
#' myChart <- chartOptionsLegend(myChart, show = FALSE)
#' }
#'
#' @export
setGeneric("chartOptionsLegend", function(
chart, show = NULL, showScenarioName = NULL, showScenarioID = NULL,
showStageName = NULL, showTimestamp = NULL) standardGeneric("chartOptionsLegend"))

#' @rdname chartOptionsLegend
setMethod("chartOptionsLegend", signature(chart = "Chart"),
function(chart, show, showScenarioName, showScenarioID,
showStageName, showTimestamp) {

# Grab project and chart ID from chart
proj <- .project(chart)
chartCID <- .chartId(chart)
chartDSName <- "core_Chart"

# Load chart configuration datasheet
ds <- .datasheet(proj, name = chartDSName, optional = T,
returnInvisible = T, includeKey = T, verbose = F)

# Set variable for checking if we should just return a dataframe of settings
allNULL <- TRUE

# Set legend options
if (!is.null(show)){
allNULL <- FALSE
if (is.logical(show)){
ds[ds$ChartId == chartCID,]$ChartShowLegend <- show
} else {
stop("show should be logical.")
}
}

if (!is.null(showScenarioName)){
allNULL <- FALSE
if (is.logical(showScenarioName)){
ds[ds$ChartId == chartCID,]$ChartLegendShowScenarioName <- showScenarioName
} else {
stop("showScenarioName should be logical.")
}
}

if (!is.null(showScenarioID)){
allNULL <- FALSE
if (is.logical(showScenarioID)){
ds[ds$ChartId == chartCID,]$ChartLegendShowScenarioId <- showScenarioID
} else {
stop("showScenarioID should be logical.")
}
}

if (!is.null(showStageName)){
allNULL <- FALSE
if (is.logical(showStageName)){
ds[ds$ChartId == chartCID,]$ChartLegendShowStageName <- showStageName
} else {
stop("showStageName should be logical.")
}
}

if (!is.null(showTimestamp)){
allNULL <- FALSE
if (is.logical(showTimestamp)){
ds[ds$ChartId == chartCID,]$ChartLegendShowTimestamp <- showTimestamp
} else {
stop("showTimestamp should be logical.")
}
}

if (allNULL){
ds <- ds[ds$ChartId == chartCID,]
legendOptions <- data.frame(show = ds$ChartShowLegend,
showScenarioName = ds$ChartLegendShowScenarioName,
showScenarioID = ds$ChartLegendShowScenarioId,
showStageName = ds$ChartLegendShowStageName,
showTimestamp = ds$ChartLegendShowTimestamp)

return(legendOptions)
}

saveDatasheet(proj, ds, name = chartDSName, append = FALSE, force = TRUE)

return(chart)
})
64 changes: 64 additions & 0 deletions man/chartOptionsLegend.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f53d4df

Please sign in to comment.