blob: 76fb7dd7b1cb13ab3197f89afe6ef241fb7ceb0d [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 Zhu6f362bb2017-10-23 23:21:38 -040032 colors <- 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) != "#") {
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}
45latex_color <- function(colors) {
Hao Zhu6f362bb2017-10-23 23:21:38 -040046 colors <- as.character(colors)
Hao Zhu457acb42017-10-14 17:37:02 -040047 sapply(colors, latex_color_)
48}
49
Hao Zhu9ce317e2017-10-12 18:19:55 -040050#' 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 Zhu4e557c22017-12-19 12:49:48 -050056#' @param scale_from input range (vector of length two). If not given,
57#' is calculated from the range of x
Hao Zhu9ce317e2017-10-12 18:19:55 -040058#' @export
Hao Zhu4e557c22017-12-19 12:49:48 -050059spec_font_size <- function(x, begin = 8, end = 16, na_font_size = 12,
Hao Zhu3edc85a2017-12-19 12:55:44 -050060 scale_from = NULL) {
61 if (is.null(scale_from)) {
Hao Zhu4e557c22017-12-19 12:49:48 -050062 x <- round(rescale(x, c(begin, end)))
63 } else {
64 x <- round(rescale(x, to = c(begin, end),
65 from = scale_from))
66 }
Hao Zhu9ce317e2017-10-12 18:19:55 -040067 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 Zhu4e557c22017-12-19 12:49:48 -050076#' @param scale_from input range (vector of length two). If not given,
77#' is calculated from the range of x
Hao Zhu9ce317e2017-10-12 18:19:55 -040078#' @export
Hao Zhu3edc85a2017-12-19 12:55:44 -050079spec_angle <- function(x, begin, end, scale_from = NULL) {
80 if (is.null(scale_from)) {
Hao Zhu4e557c22017-12-19 12:49:48 -050081 x <- round(rescale(x, c(begin, end)))
82 } else {
83 x <- round(rescale(x, to = c(begin, end),
84 from = scale_from))
85 }
Hao Zhu9ce317e2017-10-12 18:19:55 -040086 x[is.na(x)] <- 0
87 return(x)
88}
Hao Zhu6f362bb2017-10-23 23:21:38 -040089
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
97spec_tooltip <- function(title, position = "right") {
Hao Zhu8a69ad12018-01-10 18:01:41 -050098 position <- match.arg(position, c("right", "bottom", "top", "left", "auto"),
99 several.ok = TRUE)
Hao Zhu6f362bb2017-10-23 23:21:38 -0400100 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
119spec_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}