|
| 1 | + |
| 2 | +#' Options for uPlot |
| 3 | +#' |
| 4 | +#' @param uplot Chart created with [uPlot()]. |
| 5 | +#' @param ... Options for the chart. |
| 6 | +#' |
| 7 | +#' @return An `htmlwidget` object of class `"uPlot"`. |
| 8 | +#' @export |
| 9 | +#' |
| 10 | +#' @examples |
| 11 | +#' uPlot(eco2mix[1:24, c(1, 2)]) %>% |
| 12 | +#' uOptions( |
| 13 | +#' title = "Title for the chart" |
| 14 | +#' ) |
| 15 | +uOptions <- function(uplot, ...) { |
| 16 | + check_uplot(uplot) |
| 17 | + if (is.null(uplot$x$config$options)) |
| 18 | + uplot$x$config$options <- list() |
| 19 | + uplot$x$config$options <- modifyList( |
| 20 | + x = uplot$x$config$options, |
| 21 | + val = drop_nulls(list(...)) |
| 22 | + ) |
| 23 | + return(uplot) |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +#' Series options |
| 29 | +#' |
| 30 | +#' @param uplot Chart created with [uPlot()]. |
| 31 | +#' @param serie Name (column's name in data) or index of the serie. |
| 32 | +#' @param label Label to display in legend. |
| 33 | +#' @param stroke Stroke color of the line. |
| 34 | +#' @param ... Other options. |
| 35 | +#' |
| 36 | +#' @return An `htmlwidget` object of class `"uPlot"`. |
| 37 | +#' @export |
| 38 | +#' |
| 39 | +#' @importFrom utils modifyList |
| 40 | +#' |
| 41 | +#' @example examples/ex-uSeries.R |
| 42 | +uSeries <- function(uplot, serie, label = NULL, stroke = NULL, ...) { |
| 43 | + check_uplot(uplot) |
| 44 | + stopifnot( |
| 45 | + "'name' must be a character of length 1" = identical(length(serie), 1L) |
| 46 | + ) |
| 47 | + if (is.character(serie)) { |
| 48 | + serie_index <- find_serie_index(uplot, serie) |
| 49 | + } else { |
| 50 | + serie_index <- as.numeric(serie) |
| 51 | + } |
| 52 | + uplot$x$config$options$series[[serie_index]] <- modifyList( |
| 53 | + x = uplot$x$config$options$series[[serie_index]], |
| 54 | + val = drop_nulls(list( |
| 55 | + label = label, |
| 56 | + stroke = stroke, |
| 57 | + ... |
| 58 | + )) |
| 59 | + ) |
| 60 | + return(uplot) |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +find_serie_index <- function(uplot, name) { |
| 65 | + index <- which(uplot$x$series_nms == name) |
| 66 | + if (length(index) < 1) |
| 67 | + stop("uSeries: serie '", name, "' not found.") |
| 68 | + return(index) |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +#' Series colors |
| 75 | +#' |
| 76 | +#' Allow to specify series colors in one call. |
| 77 | +#' |
| 78 | +#' @param uplot Chart created with [uPlot()]. |
| 79 | +#' @param ... Colors to attribute to each series. |
| 80 | +#' |
| 81 | +#' @return An `htmlwidget` object of class `"uPlot"`. |
| 82 | +#' @export |
| 83 | +#' |
| 84 | +#' @examples |
| 85 | +#' uPlot(temperatures[, c("date", "temperature")]) %>% |
| 86 | +#' uColors(temperature = "black") |
| 87 | +#' |
| 88 | +#' uPlot(temperatures[, c("date", "low", "high")]) %>% |
| 89 | +#' uColors( |
| 90 | +#' low = "blue", |
| 91 | +#' high = "red" |
| 92 | +#' ) |
| 93 | +uColors <- function(uplot, ...) { |
| 94 | + args <- list(...) |
| 95 | + for (i in seq_along(args)) { |
| 96 | + uplot <- uSeries(uplot, serie = names(args)[i], stroke = args[[i]]) |
| 97 | + } |
| 98 | + uplot |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +#' Cursor configuration |
| 105 | +#' |
| 106 | +#' @param uplot Chart created with [uPlot()]. |
| 107 | +#' @param ... Cursor options, see examples. |
| 108 | +#' |
| 109 | +#' @return An `htmlwidget` object of class `"uPlot"`. |
| 110 | +#' @export |
| 111 | +#' |
| 112 | +#' @example examples/ex-uCursor.R |
| 113 | +uCursor <- function(uplot, ...) { |
| 114 | + check_uplot(uplot) |
| 115 | + uplot$x$config$options$cursor <- list(...) |
| 116 | + return(uplot) |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +#' Draw bands between series |
| 123 | +#' |
| 124 | +#' @param uplot Chart created with [uPlot()]. |
| 125 | +#' @param lower Name of the lower serie. |
| 126 | +#' @param upper Name of the upper serie. |
| 127 | +#' @param fill Fill color. |
| 128 | +#' @param ... Other options. |
| 129 | +#' |
| 130 | +#' @return An `htmlwidget` object of class `"uPlot"`. |
| 131 | +#' @export |
| 132 | +#' |
| 133 | +#' @example examples/ex-uBands.R |
| 134 | +uBands <- function(uplot, lower, upper, fill, ...) { |
| 135 | + check_uplot(uplot) |
| 136 | + stopifnot( |
| 137 | + "'lower' must be a character of length 1" = is.character(lower) & identical(length(lower), 1L) |
| 138 | + ) |
| 139 | + stopifnot( |
| 140 | + "'upper' must be a character of length 1" = is.character(upper) & identical(length(upper), 1L) |
| 141 | + ) |
| 142 | + lower_index <- find_serie_index(uplot, lower) |
| 143 | + upper_index <- find_serie_index(uplot, upper) |
| 144 | + uplot$x$config$options$bands <- append( |
| 145 | + uplot$x$config$options$bands, |
| 146 | + list(drop_nulls(list( |
| 147 | + series = c(upper_index, lower_index) - 1, |
| 148 | + fill = fill, |
| 149 | + ... |
| 150 | + ))) |
| 151 | + ) |
| 152 | + return(uplot) |
| 153 | +} |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | +#' X axis configuration |
| 159 | +#' |
| 160 | +#' @param uplot Chart created with [uPlot()]. |
| 161 | +#' @param ... Axis options. |
| 162 | +#' |
| 163 | +#' @return An `htmlwidget` object of class `"uPlot"`. |
| 164 | +#' @export |
| 165 | +#' |
| 166 | +#' @example examples/ex-uAxesX.R |
| 167 | +uAxesX <- function(uplot, ...) { |
| 168 | + check_uplot(uplot) |
| 169 | + uplot$x$config$options$axes[[1]] <- list(...) |
| 170 | + return(uplot) |
| 171 | +} |
| 172 | + |
| 173 | + |
| 174 | +#' Y axis configuration |
| 175 | +#' |
| 176 | +#' @param uplot Chart created with [uPlot()]. |
| 177 | +#' @param ... Axis options. |
| 178 | +#' |
| 179 | +#' @return An `htmlwidget` object of class `"uPlot"`. |
| 180 | +#' @export |
| 181 | +#' |
| 182 | +#' @example examples/ex-uAxesY.R |
| 183 | +uAxesY <- function(uplot, ...) { |
| 184 | + check_uplot(uplot) |
| 185 | + uplot$x$config$options$axes[[2]] <- list(...) |
| 186 | + return(uplot) |
| 187 | +} |
| 188 | + |
0 commit comments