blob: 350925b5141d108dc59803600db846f0d3ec351b [file] [log] [blame]
Hao Zhu9ce317e2017-10-12 18:19:55 -04001#' 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 Zhu4e557c22017-12-19 12:49:48 -05006#' @param scale_from input range (vector of length two). If not given,
7#' is calculated from the range of x
Hao Zhu9ce317e2017-10-12 18:19:55 -04008#' @export
9spec_color <- function(x, alpha = 1, begin = 0, end = 1,
10 direction = 1, option = "D",
Hao Zhu3edc85a2017-12-19 12:55:44 -050011 na_color = "#BBBBBB", scale_from = NULL) {
12 if (is.null(scale_from)) {
Hao Zhu4e557c22017-12-19 12:49:48 -050013 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 Zhu9ce317e2017-10-12 18:19:55 -040019 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 Zhu457acb42017-10-14 17:37:02 -040024html_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
31html_color <- function(colors) {
Hao Zhu72917f92019-03-15 18:41:42 -040032 colors <- trimws(gsub("\\!important", "", as.character(colors)))
Hao Zhu457acb42017-10-14 17:37:02 -040033 sapply(colors, html_color_)
34}
35
36latex_color_ <- function(color) {
37 if (substr(color, 1, 1) != "#") {
Hao Zhu33b865f2020-08-18 02:10:43 -040038 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
46latex_color__ <- function(color) {
47 if (substr(color, 1, 1) != "#") {
Hao Zhu457acb42017-10-14 17:37:02 -040048 return(paste0("{", color, "}"))
49 } else {
50 color <- sub("#", "", color)
51 if (nchar(color) == 8) color <- substr(color, 1, 6)
52 return(paste0("[HTML]{", color, "}"))
53 }
54}
Hao Zhu33b865f2020-08-18 02:10:43 -040055latex_color <- function(colors, escape = TRUE) {
Hao Zhu6f362bb2017-10-23 23:21:38 -040056 colors <- as.character(colors)
Hao Zhu33b865f2020-08-18 02:10:43 -040057 if (escape) {
58 return(sapply(colors, latex_color_))
59 } else {
60 return(sapply(colors, latex_color__))
61 }
62
Hao Zhu457acb42017-10-14 17:37:02 -040063}
64
Hao Zhu9ce317e2017-10-12 18:19:55 -040065#' Generate common font size for continuous values
66#'
67#' @param x continuous vectors of values
68#' @param begin Smalles font size to be used. Default is 10.
69#' @param end Largest font size. Default is 20.
70#' @param na_font_size font size for NA values
Hao Zhu4e557c22017-12-19 12:49:48 -050071#' @param scale_from input range (vector of length two). If not given,
72#' is calculated from the range of x
Hao Zhu9ce317e2017-10-12 18:19:55 -040073#' @export
Hao Zhu4e557c22017-12-19 12:49:48 -050074spec_font_size <- function(x, begin = 8, end = 16, na_font_size = 12,
Hao Zhu3edc85a2017-12-19 12:55:44 -050075 scale_from = NULL) {
76 if (is.null(scale_from)) {
Hao Zhu4e557c22017-12-19 12:49:48 -050077 x <- round(rescale(x, c(begin, end)))
78 } else {
79 x <- round(rescale(x, to = c(begin, end),
80 from = scale_from))
81 }
Hao Zhu9ce317e2017-10-12 18:19:55 -040082 x[is.na(x)] <- na_font_size
83 return(x)
84}
85
86#' Generate rotation angle for continuous values
87#'
88#' @param x continuous vectors of values
89#' @param begin Smallest degree to rotate. Default is 0
90#' @param end Largest degree to rotate. Default is 359.
Hao Zhu4e557c22017-12-19 12:49:48 -050091#' @param scale_from input range (vector of length two). If not given,
92#' is calculated from the range of x
Hao Zhu9ce317e2017-10-12 18:19:55 -040093#' @export
Hao Zhu3edc85a2017-12-19 12:55:44 -050094spec_angle <- function(x, begin, end, scale_from = NULL) {
95 if (is.null(scale_from)) {
Hao Zhu4e557c22017-12-19 12:49:48 -050096 x <- round(rescale(x, c(begin, end)))
97 } else {
98 x <- round(rescale(x, to = c(begin, end),
99 from = scale_from))
100 }
Hao Zhu9ce317e2017-10-12 18:19:55 -0400101 x[is.na(x)] <- 0
102 return(x)
103}
Hao Zhu6f362bb2017-10-23 23:21:38 -0400104
105#' Setup bootstrap tooltip
106#'
107#' @param title text for hovering message
108#' @param position How the tooltip should be positioned. Possible values are
109#' `right`(default), `top`, `bottom`, `left` & `auto`.
110#'
111#' @export
112spec_tooltip <- function(title, position = "right") {
Hao Zhu8a69ad12018-01-10 18:01:41 -0500113 position <- match.arg(position, c("right", "bottom", "top", "left", "auto"),
114 several.ok = TRUE)
Hao Zhu6f362bb2017-10-23 23:21:38 -0400115 tooltip_options <- paste(
Hao Zhu33b865f2020-08-18 02:10:43 -0400116 'data-toggle="tooltip" data-container="body"',
Hao Zhu6f362bb2017-10-23 23:21:38 -0400117 paste0('data-placement="', position, '"'),
118 # ifelse(as_html, 'data-html="true"', NULL),
119 paste0('title="', title, '"'))
Hao Zhu33b865f2020-08-18 02:10:43 -0400120 tooltip_options_list <- list(
121 'data-toggle' = 'tooltip',
122 'data-container' = 'body',
123 'data-placement' = position,
124 'title' = if(is.null(title)) '' else title
125 )
Hao Zhu6f362bb2017-10-23 23:21:38 -0400126 class(tooltip_options) <- "ke_tooltip"
Hao Zhu33b865f2020-08-18 02:10:43 -0400127 attr(tooltip_options, 'list') <- tooltip_options_list
Hao Zhu6f362bb2017-10-23 23:21:38 -0400128 return(tooltip_options)
129}
130
131#' Setup bootstrap popover
132#'
133#' @param content content for pop-over message
134#' @param title title for pop-over message.
135#' @param trigger Controls how the pop-over message should be triggered.
136#' Possible values include `hover` (default), `click`, `focus` and `manual`.
137#' @param position How the tooltip should be positioned. Possible values are
138#' `right`(default), `top`, `bottom`, `left` & `auto`.
139#'
140#' @export
141spec_popover <- function(content = NULL, title = NULL,
142 trigger = "hover", position = "right") {
143 trigger <- match.arg(trigger, c("hover", "click", "focus", "manual"),
144 several.ok = TRUE)
145 position <- match.arg(position, c("bottom", "top", "left", "right", "auto"),
146 several.ok = TRUE)
147 popover_options <- paste(
Hao Zhu33b865f2020-08-18 02:10:43 -0400148 'data-toggle="popover" data-container="body"',
Hao Zhu6f362bb2017-10-23 23:21:38 -0400149 paste0('data-trigger="', trigger, '"'),
150 paste0('data-placement="', position, '"'),
151 ifelse(!is.null(title), paste0('title="', title, '"'), ""),
152 paste0('data-content="', content, '"'))
Hao Zhu33b865f2020-08-18 02:10:43 -0400153 popover_options_list <- list(
154 'data-toggle' = 'popover',
155 'data-container' = 'body',
156 'data-trigger' = trigger,
157 'data-placement' = position,
158 'data-content' = content
159 )
160 if (!is.null(title)) {
161 popover_options_list['title'] <- title
162 }
Hao Zhu6f362bb2017-10-23 23:21:38 -0400163 class(popover_options) <- "ke_popover"
Hao Zhu33b865f2020-08-18 02:10:43 -0400164 attr(popover_options, 'list') <- popover_options_list
Hao Zhu6f362bb2017-10-23 23:21:38 -0400165 return(popover_options)
166}