Skip to content

Commit

Permalink
Update Chart Options 😍
Browse files Browse the repository at this point in the history
The chart-function now has the following options:

* scale: scales the font-size.
* static: a logical value FALSE by default. If TRUE it act as a regular static plot a la ggplot.

The chart function now asserts the size being between 0 and 1, and checks for valid hcl.palettes with ignore.case set to TRUE.

All subcharts have had annotations multiplied by args from the chart funciton.

NOTE: All checks have passed locally 🚀
  • Loading branch information
serkor1 committed Jun 28, 2024
1 parent d0c24e2 commit 20b68d3
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 68 deletions.
12 changes: 7 additions & 5 deletions NEWS.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ library(cryptoQuotes)

* The `chart()`-function are now exported as `.svg`-images in 4k resolution via the `modebar`.
* The `chart()`-function are now more interactive and supports drawing lines and rectangles via the `modebar`. It is also possible to interactively change the `title` and `subtitle` by double clicking these (Thank you @andreltr for the suggestion. See [Discussion](https://github.com/serkor1/cryptoQuotes/discussions/19).
* The `chart()`-function now has a new option `modebar` that is equal to `TRUE` by default. If `FALSE` no `modebar` will appear on the `chart`
* The `chart()`-function now has a new option `static` that is equal to `FALSE` by default. If `FALSE` the chart can be edited, annotated and explored interactively.
* The `chart()`-function now has a new option `palette` that is set to "hawaii" by default. See [hcl.pals()] for accepted values.
* The `chart()`-function now has a new option `scale` that is set to 1 by default. Scales
#' all fonts on the chart.

<details>
<summary>Without modebar (Default Palette) </summary>
<summary>Static set to FALSE (Default Palette) </summary>
```{r}
# modebar = FALSE
chart(
Expand All @@ -49,15 +51,15 @@ chart(
sma(n = 21)
),
options = list(
modebar = FALSE,
static = FALSE,
palette = "hawaii"
)
)
```
</details>

<details>
<summary>With modebar ("Set 3" palette)</summary>
<summary>Static set to TRUE ("Set 3" palette)</summary>
```{r}
# modebar = TRUE
chart(
Expand All @@ -69,7 +71,7 @@ chart(
sma(n = 21)
),
options = list(
modebar = TRUE,
static = TRUE,
palette = "Set 3"
)
)
Expand Down
43 changes: 23 additions & 20 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
interactively change the `title` and `subtitle` by double clicking
these (Thank you @andreltr for the suggestion. See
[Discussion](https://github.com/serkor1/cryptoQuotes/discussions/19).
- The `chart()`-function now has a new option `modebar` that is equal to
`TRUE` by default. If `FALSE` no `modebar` will appear on the `chart`
- The `chart()`-function now has a new option `static` that is equal to
`FALSE` by default. If `FALSE` the chart can be edited, annotated and
explored interactively.
- The `chart()`-function now has a new option `palette` that is set to
“hawaii” by default. See \[hcl.pals()\] for accepted values.
- The `chart()`-function now has a new option `scale` that is set to 1
by default. Scales \#’ all fonts on the chart.

<details>
<summary>
Without modebar (Default Palette)
Static set to FALSE (Default Palette)
</summary>

``` r
Expand All @@ -37,7 +40,7 @@ chart(
sma(n = 21)
),
options = list(
modebar = FALSE,
static = FALSE,
palette = "hawaii"
)
)
Expand All @@ -47,7 +50,7 @@ chart(
</details>
<details>
<summary>
With modebar (“Set 3” palette)
Static set to TRUE (“Set 3” palette)
</summary>

``` r
Expand All @@ -61,7 +64,7 @@ chart(
sma(n = 21)
),
options = list(
modebar = TRUE,
static = TRUE,
palette = "Set 3"
)
)
Expand Down Expand Up @@ -160,12 +163,12 @@ tail(
```

#> open_interest
#> 2024-06-26 08:00:00 2355.272
#> 2024-06-26 09:00:00 2414.858
#> 2024-06-26 10:00:00 2411.043
#> 2024-06-26 11:00:00 2390.814
#> 2024-06-26 12:00:00 2379.619
#> 2024-06-26 13:00:00 2363.744
#> 2024-06-28 18:00:00 2422.880
#> 2024-06-28 19:00:00 2419.634
#> 2024-06-28 20:00:00 2408.566
#> 2024-06-28 21:00:00 2409.375
#> 2024-06-28 22:00:00 2399.027
#> 2024-06-28 23:00:00 2396.589

</details>

Expand Down Expand Up @@ -346,12 +349,12 @@ tail(
```

#> funding_rate
#> 2024-06-24 18:00:00 7.488e-05
#> 2024-06-25 02:00:00 7.447e-05
#> 2024-06-25 10:00:00 5.949e-05
#> 2024-06-25 18:00:00 7.752e-05
#> 2024-06-26 02:00:00 1.000e-04
#> 2024-06-26 10:00:00 1.000e-04
#> 2024-06-27 02:00:00 1e-04
#> 2024-06-27 10:00:00 1e-04
#> 2024-06-27 18:00:00 1e-04
#> 2024-06-28 02:00:00 1e-04
#> 2024-06-28 10:00:00 1e-04
#> 2024-06-28 18:00:00 1e-04

</details>

Expand All @@ -373,12 +376,12 @@ tail(
```

#> open_interest
#> 2024-06-21 02:00:00 74377.63
#> 2024-06-22 02:00:00 76738.13
#> 2024-06-23 02:00:00 78036.86
#> 2024-06-24 02:00:00 77593.99
#> 2024-06-25 02:00:00 76111.90
#> 2024-06-26 02:00:00 75584.46
#> 2024-06-27 02:00:00 74876.90
#> 2024-06-28 02:00:00 74906.65

</details>

Expand Down
93 changes: 79 additions & 14 deletions R/chart.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,22 @@
#'
#' * \code{dark} A <[logical]>-value of [length] 1. [TRUE] by default.
#' Sets the overall theme of the [chart()]
#'
#' * \code{slider} A <[logical]>-value of [length] 1. [FALSE] by default.
#' If [TRUE], a [plotly::rangeslider()] is added
#' If [TRUE], a [plotly::rangeslider()] is added.
#'
#' * \code{deficiency} A <[logical]>-value of [length] 1. [FALSE] by default.
#' If [TRUE], all [chart()]-elements are colorblind friendly
#'
#' * \code{size} A <[numeric]>-value of [length] 1. The relative size of the
#' main chart. 0.6 by default. Must be between 0 and 1, non-inclusive.
#' * \code{modebar} A <[logical]>-value of [length] 1. [TRUE] by default. If [FALSE]
#' a [plotly] modebar will be displayed.
#'
#' * \code{scale} A <[numeric]>-value of [length] 1. 1 by default. Scales
#' all fonts on the chart.
#'
#' * \code{static} A <[logical]>-value of [length] 1. [FALSE] by default. If [FALSE]
#' the chart can be edited, annotated and explored interactively.
#'
#' * \code{palette} A <[character]>-vector of [length] 1. "hawaii" by default. See [hcl.pals()] for
#' all possible color palettes.
#'
Expand Down Expand Up @@ -135,11 +143,12 @@ chart <- function(
## 1) set chart options
## globally (locally)
default_options <- list(
static = FALSE,
dark = TRUE,
slider = FALSE,
deficiency = FALSE,
modebar = TRUE,
palette = "hawaii",
scale = 1,
size = 0.6
)

Expand All @@ -149,14 +158,67 @@ chart <- function(
keep.null = TRUE
)

dark <- options$dark
deficiency <- options$deficiency
slider <- options$slider
size <- options$size
modebar <- options$modebar
palette <- options$palette

dark <- options$dark
deficiency <- options$deficiency
slider <- options$slider
size <- options$size
palette <- options$palette
static <- options$static
candle_color <- movement_color(deficiency = deficiency)
scale <- options$scale

if (static) {

# if the plot is static
# then turn off modebar
# slider and editable

modebar <- slider <- editable <- FALSE

Check warning on line 176 in R/chart.R

View check run for this annotation

Codecov / codecov/patch

R/chart.R#L176

Added line #L176 was not covered by tests

} else {

# the modebar and editable
# part of the plot should
# always be set to true
# for "real" interactivitiy
modebar <- editable <- TRUE


}

# assert inputs and options
assert(
any(grepl(pattern = palette,x = grDevices::hcl.pals(),ignore.case = TRUE)),
error_message = c(
"x" = sprintf(
fmt = "Palette {.val %s} is not valid.",
palette
),
"i" = paste(
"Run",
cli::code_highlight(
code = "hcl.pals()",
code_theme = "chaos"
),
"for valid values."
)
)
)

assert(
size > 0 & size < 1,
error_message = c(
"x" = sprintf(
fmt = "Got {.arg size} %s.",
size
),
"i" = sprintf(
fmt = "{.arg size} has to be between 0 and 1, non-inclusive."
)
)
)



# 1) generate list
# of calls for lazy
Expand All @@ -177,6 +239,7 @@ chart <- function(
.f$interval <- interval
.f$candle_color <- candle_color
.f$deficiency <- deficiency
.f$scale <- scale
eval(.f)
},
flatten(list(call_list$main, call_list$sub))
Expand Down Expand Up @@ -285,10 +348,12 @@ chart <- function(
name = name,
market = market,
date_range = paste(range(zoo::index(ticker)), collapse = " - "),
modebar = modebar
modebar = modebar,
scale = scale
),
editable = TRUE,
responsive = TRUE,
staticPlot = static,
editable = editable,
responsive = TRUE,
displayModeBar = modebar,
modeBarButtonsToAdd = c(
"drawline",
Expand Down
2 changes: 1 addition & 1 deletion R/chart_fgi.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fgi <- function(
annotations = list(
text = "Fear and Greed Index",
font = list(
size = 16
size = 16 * args$scale
),
showarrow = FALSE,
x = 0,
Expand Down
2 changes: 1 addition & 1 deletion R/chart_lsr.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ lsr <- function(
annotations = list(
text = "Long-Short Ratio",
font = list(
size = 16
size = 16 * args$scale
),
showarrow = FALSE,
x = 0,
Expand Down
2 changes: 1 addition & 1 deletion R/chart_macd.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ macd <- function(
x = 0,
y = 1,
font = list(
size = 16
size = 16 * args$scale
),
xref = 'paper',
yref = 'paper',
Expand Down
2 changes: 1 addition & 1 deletion R/chart_rsi.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ rsi <- function(
list(
text = paste0("RSI(", n, ")"),
font = list(
size = 16
size = 16 * args$scale
),
x = 0,
y = 1,
Expand Down
2 changes: 1 addition & 1 deletion R/chart_smi.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ smi <- function(
x = 0,
y = 1,
font = list(
size = 16
size = 16 * args$scale
),
xref = 'paper',
yref = 'paper',
Expand Down
2 changes: 1 addition & 1 deletion R/chart_volume.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ volume <- function(
annotations = list(
text = "Volume",
font = list(
size = 16
size = 16 * args$scale
),
showarrow = FALSE,
x = 0,
Expand Down
7 changes: 4 additions & 3 deletions R/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,7 @@ bar <- function(
market,
date_range,
modebar,
scale,
...) {

# 0) chart theme
Expand All @@ -970,7 +971,7 @@ bar <- function(
paper_bgcolor = theme$paper_bgcolor,
plot_bgcolor = theme$plot_bgcolor,
font = list(
size = 14,
size = 14 * scale,
color = theme$font_color
),
showlegend = TRUE,
Expand All @@ -982,14 +983,14 @@ bar <- function(
title = list(
text = "<b>Indicators:</b>",
font = list(
size = 16
size = 16 * scale
)
)
),
title = list(
text = title_text,
font = list(
size = 20
size = 20 * scale
),
x = 1,
xref = "paper",
Expand Down
Loading

0 comments on commit 20b68d3

Please sign in to comment.