From f53d4dffe70c7d6a2b4be4b961337539759621ec Mon Sep 17 00:00:00 2001 From: katieb1 Date: Mon, 29 Apr 2024 17:47:47 -0700 Subject: [PATCH] Create function for setting chart legend options --- DESCRIPTION | 1 + NAMESPACE | 1 + R/chartOptionsLegend.R | 121 ++++++++++++++++++++++++++++++++++++++ man/chartOptionsLegend.Rd | 64 ++++++++++++++++++++ 4 files changed, 187 insertions(+) create mode 100644 R/chartOptionsLegend.R create mode 100644 man/chartOptionsLegend.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 8e1ec039..bc0b6ba7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -58,6 +58,7 @@ Collate: 'chartInclude.R' 'chartInfo.R' 'chartOptionsFont.R' + 'chartOptionsLegend.R' 'chartXAxis.R' 'chartYAxis.R' 'command.R' diff --git a/NAMESPACE b/NAMESPACE index 432f183b..cf14d308 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -31,6 +31,7 @@ export(chartId) export(chartInclude) export(chartInfo) export(chartOptionsFont) +export(chartOptionsLegend) export(chartXAxis) export(chartYAxis) export(command) diff --git a/R/chartOptionsLegend.R b/R/chartOptionsLegend.R new file mode 100644 index 00000000..0568e9c5 --- /dev/null +++ b/R/chartOptionsLegend.R @@ -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) +}) \ No newline at end of file diff --git a/man/chartOptionsLegend.Rd b/man/chartOptionsLegend.Rd new file mode 100644 index 00000000..5c515473 --- /dev/null +++ b/man/chartOptionsLegend.Rd @@ -0,0 +1,64 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/chartOptionsLegend.R +\name{chartOptionsLegend} +\alias{chartOptionsLegend} +\alias{chartOptionsLegend,Chart-method} +\title{Modifies the legend settings for a \code{\link{Chart}}} +\usage{ +chartOptionsLegend( + chart, + show = NULL, + showScenarioName = NULL, + showScenarioID = NULL, + showStageName = NULL, + showTimestamp = NULL +) + +\S4method{chartOptionsLegend}{Chart}( + chart, + show = NULL, + showScenarioName = NULL, + showScenarioID = NULL, + showStageName = NULL, + showTimestamp = NULL +) +} +\arguments{ +\item{chart}{\code{\link{Chart}} object} + +\item{show}{logical. Whether to show the chart legend. Default is \code{NULL}.} + +\item{showScenarioName}{logical. Whether to show the scenario name in the +legend. Default is \code{NULL}.} + +\item{showScenarioID}{logical. Whether to show the scenario ID in the legend. +Default is \code{NULL}.} + +\item{showStageName}{logical. Determines whether to show the stage name +(i.e., transformer name) in the legend. Default is \code{NULL}.} + +\item{showTimestamp}{logical. Whether to show the timestamp of the scenario +run in the legend. Default is \code{NULL}. Default is \code{NULL}.} +} +\value{ +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. +} +\description{ +Modifies the legend settings for a \code{\link{Chart}}. +} +\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) +} + +}