Skip to content

Commit

Permalink
Accessibility upgrade of chart()-function (#31)
Browse files Browse the repository at this point in the history
* Accessibility upgrade of chart()-function

* The chart()-function now has a new option  which controls the overall width of the chart.

NEWS has been updated

* MEXC Bugfix 🔧

PR #31 failed the checks. The source of the error:

* MEXC has changed the response-object on available tickers. This has been fixed.

All checks have passed.
  • Loading branch information
serkor1 authored Aug 16, 2024
1 parent e08df82 commit 31923a1
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 22 deletions.
1 change: 1 addition & 0 deletions NEWS.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ library(cryptoQuotes)
* 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.
* The `chart()`-function now has a new option `width` that is set to 0.9 by default. Sets the overall `linewidth` of the chart. (Thank you @andreltr for the suggestion. See [Discussion](https://github.com/serkor1/cryptoQuotes/discussions/30))

<details>
<summary>Static set to FALSE (Default Palette) </summary>
Expand Down
40 changes: 22 additions & 18 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
“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.
- The `chart()`-function now has a new option `width` that is set to 0.9
by default. Sets the overall `linewidth` of the chart. (Thank you
@andreltr for the suggestion. See
[Discussion](https://github.com/serkor1/cryptoQuotes/discussions/30))

<details>
<summary>
Expand Down Expand Up @@ -180,12 +184,12 @@ tail(
```

#> open_interest
#> 2024-07-07 10:00:00 2474.917
#> 2024-07-07 11:00:00 2522.876
#> 2024-07-07 12:00:00 2522.405
#> 2024-07-07 13:00:00 2520.337
#> 2024-07-07 14:00:00 2514.162
#> 2024-07-07 15:00:00 2512.482
#> 2024-08-16 10:00:00 1840.775
#> 2024-08-16 11:00:00 1845.837
#> 2024-08-16 12:00:00 1837.801
#> 2024-08-16 13:00:00 1836.295
#> 2024-08-16 14:00:00 1836.202
#> 2024-08-16 15:00:00 1838.612

</details>

Expand Down Expand Up @@ -367,12 +371,12 @@ tail(
```

#> funding_rate
#> 2024-07-05 18:00:00 1.762e-05
#> 2024-07-06 02:00:00 5.729e-05
#> 2024-07-06 10:00:00 7.512e-05
#> 2024-07-06 18:00:00 3.250e-05
#> 2024-07-07 02:00:00 8.164e-05
#> 2024-07-07 10:00:00 1.000e-04
#> 2024-08-14 18:00:00 -0.00007811
#> 2024-08-15 02:00:00 -0.00008488
#> 2024-08-15 10:00:00 0.00006339
#> 2024-08-15 18:00:00 -0.00006181
#> 2024-08-16 02:00:00 -0.00011235
#> 2024-08-16 10:00:00 -0.00007387

</details>

Expand All @@ -394,12 +398,12 @@ tail(
```

#> open_interest
#> 2024-07-02 02:00:00 81856.18
#> 2024-07-03 02:00:00 84473.52
#> 2024-07-04 02:00:00 88025.51
#> 2024-07-05 02:00:00 88552.55
#> 2024-07-06 02:00:00 83117.83
#> 2024-07-07 02:00:00 80177.38
#> 2024-08-11 02:00:00 71072.43
#> 2024-08-12 02:00:00 70092.78
#> 2024-08-13 02:00:00 68490.56
#> 2024-08-14 02:00:00 72127.82
#> 2024-08-15 02:00:00 78050.79
#> 2024-08-16 02:00:00 83767.54

</details>

Expand Down
2 changes: 1 addition & 1 deletion R/api_mexc.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ mexcResponse <- function(
} else {
subset(
x = response$symbols,
response$symbols$status == "ENABLED"
response$symbols$status == "1"
)$symbol


Expand Down
29 changes: 26 additions & 3 deletions R/chart.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
#' * \code{scale} A <[numeric]>-value of [length] 1. 1 by default. Scales
#' all fonts on the chart.
#'
#' * \code{width} A <[numeric]>-value of [length] 1. 0.9 by default. Sets
#' the width of all line elements 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.
#'
Expand Down Expand Up @@ -149,7 +152,8 @@ chart <- function(
deficiency = FALSE,
palette = "hawaii",
scale = 1,
size = 0.6
size = 0.6,
width = 0.9
)

options <- utils::modifyList(
Expand All @@ -166,6 +170,7 @@ chart <- function(
static <- options$static
candle_color <- movement_color(deficiency = deficiency)
scale <- options$scale
width <- options$width

if (static) {

Expand Down Expand Up @@ -253,7 +258,7 @@ chart <- function(

if (!identical(call_list$indicator, list())) {

plot_list[1] <- list(Reduce(
plot_list[1] <- list(Reduce(
f = function(plot, .f) {
# Modify the call list
.f$data <- ticker
Expand Down Expand Up @@ -339,9 +344,26 @@ chart <- function(

}
)

)

plotly::config(
scatter_indices <- which(
sapply(
X = plot$x$data,
FUN = function(x) {
x$type == "scatter"
}
)
)

plot <- plotly::style(
p = plot,
line.width = width,
traces = scatter_indices
)


plot <- plotly::config(
p = bar(
dark = dark,
plot = plot,
Expand Down Expand Up @@ -369,6 +391,7 @@ chart <- function(
)
)

plot

}

Expand Down
2 changes: 2 additions & 0 deletions man/chart.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified man/figures/NEWS-unnamed-chunk-2-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/NEWS-unnamed-chunk-3-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/NEWS-unnamed-chunk-4-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/NEWS-unnamed-chunk-6-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/NEWS-unnamed-chunk-7-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 59 additions & 0 deletions playground/playground.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,42 @@
# setup;
rm(list = ls()); gc(); devtools::load_all()

chart(
ticker = BTC,
indicator = list(
sma(),
wma()
),
sub = list(
rsi(),
macd()
),
options = list(
width = 2
)
)

print.chart <- function(x) {

suppressWarnings(
print(x)
)

}



library(plotly)
sloop::s3_dispatch(
call = print(plot_ly())
)

my_chart

class(my_chart)
my_chart



# lapply vs for loop
foo <- function() {
Expand Down Expand Up @@ -73,3 +109,26 @@ idx <- vapply(


# script end;



library(plotly)

# Create a sample subplot with various types of traces
x <- c(1, 2, 3, 4)
y1 <- c(10, 11, 12, 13)
y2 <- c(20, 21, 22, 23)
y3 <- c(30, 31, 32, 33)

p <- subplot(
plot_ly(x = x, y = y1, type = 'scatter', mode = 'lines', name = 'Line 1'),
plot_ly(x = x, y = y2, type = 'scatter', mode = 'lines+markers', name = 'Line 2'),
plot_ly(x = x, y = y3, type = 'bar', name = 'Bar')
)

# Vectorized approach to change linewidth for scatter traces only
scatter_indices <- which(sapply(p$x$data, function(trace) trace$type == "scatter"))

p <- style(p, line = list(width = 4), traces = scatter_indices)

p

0 comments on commit 31923a1

Please sign in to comment.