Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 1 | #' Generate viridis Color code for continuous values |
| 2 | #' |
| 3 | #' @inheritParams viridisLite::viridis |
| 4 | #' @param x continuous vectors of values |
| 5 | #' @param na_color color code for NA values |
Hao Zhu | 4e557c2 | 2017-12-19 12:49:48 -0500 | [diff] [blame] | 6 | #' @param scale_from input range (vector of length two). If not given, |
| 7 | #' is calculated from the range of x |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 8 | #' @export |
| 9 | spec_color <- function(x, alpha = 1, begin = 0, end = 1, |
| 10 | direction = 1, option = "D", |
Hao Zhu | 3edc85a | 2017-12-19 12:55:44 -0500 | [diff] [blame] | 11 | na_color = "#BBBBBB", scale_from = NULL) { |
| 12 | if (is.null(scale_from)) { |
Hao Zhu | 4e557c2 | 2017-12-19 12:49:48 -0500 | [diff] [blame] | 13 | x <- round(rescale(x, c(1, 256))) |
| 14 | } else { |
| 15 | x <- round(rescale(x, to = c(1, 256), |
| 16 | from = scale_from)) |
| 17 | } |
| 18 | |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 19 | color_code <- viridisLite::viridis(256, alpha, begin, end, direction, option)[x] |
| 20 | color_code[is.na(color_code)] <- na_color |
| 21 | return(color_code) |
| 22 | } |
| 23 | |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 24 | html_color_ <- function(color) { |
| 25 | if (substr(color, 1, 1) != "#" | nchar(color) != 9) return(color) |
| 26 | rgba_code <- col2rgb(color, alpha = TRUE) |
| 27 | rgba_code[4] <- round(rgba_code[4] / 255, 2) |
| 28 | return(paste0("rgba(", paste(rgba_code, collapse = ", "), ")")) |
| 29 | } |
| 30 | |
| 31 | html_color <- function(colors) { |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 32 | colors <- as.character(colors) |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 33 | sapply(colors, html_color_) |
| 34 | } |
| 35 | |
| 36 | latex_color_ <- function(color) { |
| 37 | if (substr(color, 1, 1) != "#") { |
| 38 | return(paste0("{", color, "}")) |
| 39 | } else { |
| 40 | color <- sub("#", "", color) |
| 41 | if (nchar(color) == 8) color <- substr(color, 1, 6) |
| 42 | return(paste0("[HTML]{", color, "}")) |
| 43 | } |
| 44 | } |
| 45 | latex_color <- function(colors) { |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 46 | colors <- as.character(colors) |
Hao Zhu | 457acb4 | 2017-10-14 17:37:02 -0400 | [diff] [blame] | 47 | sapply(colors, latex_color_) |
| 48 | } |
| 49 | |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 50 | #' Generate common font size for continuous values |
| 51 | #' |
| 52 | #' @param x continuous vectors of values |
| 53 | #' @param begin Smalles font size to be used. Default is 10. |
| 54 | #' @param end Largest font size. Default is 20. |
| 55 | #' @param na_font_size font size for NA values |
Hao Zhu | 4e557c2 | 2017-12-19 12:49:48 -0500 | [diff] [blame] | 56 | #' @param scale_from input range (vector of length two). If not given, |
| 57 | #' is calculated from the range of x |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 58 | #' @export |
Hao Zhu | 4e557c2 | 2017-12-19 12:49:48 -0500 | [diff] [blame] | 59 | spec_font_size <- function(x, begin = 8, end = 16, na_font_size = 12, |
Hao Zhu | 3edc85a | 2017-12-19 12:55:44 -0500 | [diff] [blame] | 60 | scale_from = NULL) { |
| 61 | if (is.null(scale_from)) { |
Hao Zhu | 4e557c2 | 2017-12-19 12:49:48 -0500 | [diff] [blame] | 62 | x <- round(rescale(x, c(begin, end))) |
| 63 | } else { |
| 64 | x <- round(rescale(x, to = c(begin, end), |
| 65 | from = scale_from)) |
| 66 | } |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 67 | x[is.na(x)] <- na_font_size |
| 68 | return(x) |
| 69 | } |
| 70 | |
| 71 | #' Generate rotation angle for continuous values |
| 72 | #' |
| 73 | #' @param x continuous vectors of values |
| 74 | #' @param begin Smallest degree to rotate. Default is 0 |
| 75 | #' @param end Largest degree to rotate. Default is 359. |
Hao Zhu | 4e557c2 | 2017-12-19 12:49:48 -0500 | [diff] [blame] | 76 | #' @param scale_from input range (vector of length two). If not given, |
| 77 | #' is calculated from the range of x |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 78 | #' @export |
Hao Zhu | 3edc85a | 2017-12-19 12:55:44 -0500 | [diff] [blame] | 79 | spec_angle <- function(x, begin, end, scale_from = NULL) { |
| 80 | if (is.null(scale_from)) { |
Hao Zhu | 4e557c2 | 2017-12-19 12:49:48 -0500 | [diff] [blame] | 81 | x <- round(rescale(x, c(begin, end))) |
| 82 | } else { |
| 83 | x <- round(rescale(x, to = c(begin, end), |
| 84 | from = scale_from)) |
| 85 | } |
Hao Zhu | 9ce317e | 2017-10-12 18:19:55 -0400 | [diff] [blame] | 86 | x[is.na(x)] <- 0 |
| 87 | return(x) |
| 88 | } |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 89 | |
| 90 | #' Setup bootstrap tooltip |
| 91 | #' |
| 92 | #' @param title text for hovering message |
| 93 | #' @param position How the tooltip should be positioned. Possible values are |
| 94 | #' `right`(default), `top`, `bottom`, `left` & `auto`. |
| 95 | #' |
| 96 | #' @export |
| 97 | spec_tooltip <- function(title, position = "right") { |
Hao Zhu | 8a69ad1 | 2018-01-10 18:01:41 -0500 | [diff] [blame^] | 98 | position <- match.arg(position, c("right", "bottom", "top", "left", "auto"), |
| 99 | several.ok = TRUE) |
Hao Zhu | 6f362bb | 2017-10-23 23:21:38 -0400 | [diff] [blame] | 100 | tooltip_options <- paste( |
| 101 | 'data-toggle="tooltip"', |
| 102 | paste0('data-placement="', position, '"'), |
| 103 | # ifelse(as_html, 'data-html="true"', NULL), |
| 104 | paste0('title="', title, '"')) |
| 105 | class(tooltip_options) <- "ke_tooltip" |
| 106 | return(tooltip_options) |
| 107 | } |
| 108 | |
| 109 | #' Setup bootstrap popover |
| 110 | #' |
| 111 | #' @param content content for pop-over message |
| 112 | #' @param title title for pop-over message. |
| 113 | #' @param trigger Controls how the pop-over message should be triggered. |
| 114 | #' Possible values include `hover` (default), `click`, `focus` and `manual`. |
| 115 | #' @param position How the tooltip should be positioned. Possible values are |
| 116 | #' `right`(default), `top`, `bottom`, `left` & `auto`. |
| 117 | #' |
| 118 | #' @export |
| 119 | spec_popover <- function(content = NULL, title = NULL, |
| 120 | trigger = "hover", position = "right") { |
| 121 | trigger <- match.arg(trigger, c("hover", "click", "focus", "manual"), |
| 122 | several.ok = TRUE) |
| 123 | position <- match.arg(position, c("bottom", "top", "left", "right", "auto"), |
| 124 | several.ok = TRUE) |
| 125 | popover_options <- paste( |
| 126 | 'data-toggle="popover"', |
| 127 | paste0('data-trigger="', trigger, '"'), |
| 128 | paste0('data-placement="', position, '"'), |
| 129 | ifelse(!is.null(title), paste0('title="', title, '"'), ""), |
| 130 | paste0('data-content="', content, '"')) |
| 131 | class(popover_options) <- "ke_popover" |
| 132 | return(popover_options) |
| 133 | } |