Skip to content

Commit bd2f91d

Browse files
committed
Add some edit features
1 parent a500a16 commit bd2f91d

19 files changed

+1251
-4
lines changed

.Rbuildignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
^tooltip_tmp.R$
1010
^R/cjs_plot_gallery.R$
1111
_\.new\.png$
12+
^dev$

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ inst/doc
99
tooltip_tmp.R
1010
# {shinytest2}: Ignore new debug snapshots for `$expect_values()`
1111
*_.new.png
12+
^dev$

DESCRIPTION

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
Package: chartjs4r
22
Title: Chartjs For R
3-
Version: 0.0.6
3+
Version: 0.0.7
44
Authors@R:
55
person("Shaun", "Nielsen", , "[email protected]", role = c("aut", "cre"))
66
Description: Chartjs htmlwidgeet for R.
77
License: MIT + file LICENSE
88
Encoding: UTF-8
99
Roxygen: list(markdown = TRUE)
10-
RoxygenNote: 7.2.3
10+
RoxygenNote: 7.3.1
1111
Imports:
1212
base,
1313
crosstalk,
@@ -16,6 +16,7 @@ Imports:
1616
grDevices,
1717
htmltools,
1818
htmlwidgets,
19+
jsonlite,
1920
magrittr,
2021
rlang,
2122
rmarkdown,

NAMESPACE

+3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ export(chartjsOutput)
77
export(chartjsProxy)
88
export(cjs_add_bars)
99
export(cjs_add_doughnut_pie)
10+
export(cjs_add_options)
1011
export(cjs_add_points)
1112
export(cjs_bar_orientation)
1213
export(cjs_border)
14+
export(cjs_config_line)
15+
export(cjs_defaults)
1316
export(cjs_events)
1417
export(cjs_example_data)
1518
export(cjs_flexdashboard_example)

NEWS.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Version 0.0.7
2+
3+
* Add some customiser functions
4+
* `cjs_defaults()`, `cjs_add_options()`, `cjs_config_line()`
5+
16
# Version 0.0.6
27

38
* tool tips customisation with text literals `cjs_tooltip()`

R/cjs_add_options.R

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#' Add/edit options of a chartjs object
2+
#'
3+
#' A short cut to add options to the chart object. The options object is deeply
4+
#' nested and has many options. Rather than write a function capturing all the
5+
#' options (and their children), this function allows you to specify them using
6+
#' a . (dot) notation e.g. elements.line.tension = 0.25 expands to
7+
#' \{elements: \{line: \{tension: 0.25\}\}\}. Thus you must determine the options you
8+
#' desire from the [chartjs documention](https://www.chartjs.org/docs/latest/configuration/).
9+
#' Any previously set options should be preserved ...
10+
#'
11+
#' @param p chartjs object
12+
#' @param ... any chartjs option, using . notation
13+
#'
14+
#' @return An object of class `htmlwidget`
15+
#'
16+
#' @export
17+
cjs_add_options <- function(p, ...){
18+
19+
#chartjs_output$x$options$elements$line$tension = 0.25
20+
#chartjs_output$x$options$elements$line$borderJoinStyle = 'round'
21+
22+
# Capture arguments
23+
parameters <- list(...)
24+
parameters <- get_non_null_parameters(parameters)
25+
parameters <- relist_parameter_vector(parameters)
26+
27+
# Update data
28+
p$x$options <-
29+
modify_list(p$x$options, parameters)
30+
31+
# Return plot
32+
p
33+
}

R/cjs_config_line.R

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#' Line Configuration
2+
#'
3+
#' Line elements are used to represent the line in a line chart.
4+
5+
#' @param tension number 0 Bézier curve tension (0 for no Bézier curves).
6+
#' @param backgroundColor Color Chart.defaults.backgroundColor Line fill color.
7+
#' @param borderWidth number 3 Line stroke width.
8+
#' @param borderColor Color Chart.defaults.borderColor Line stroke color.
9+
#' @param borderCapStyle string 'butt' Line cap style. See MDN
10+
#' @param borderDash number[] [] Line dash. See MDN
11+
#' @param borderDashOffset number 0.0 Line dash offset. See MDN
12+
#' @param borderJoinStyle 'round'|'bevel'|'miter' 'miter' Line join style. See MDN
13+
#' @param capBezierPoints boolean true true to keep Bézier control inside the chart, false for no restriction.
14+
#' @param cubicInterpolationMode string 'default' Interpolation mode to apply. See more...
15+
#' @param fill boolean|string false How to fill the area under the line. See area charts.
16+
#' @param stepped boolean false true to show the line as a stepped line (tension will be ignored).
17+
#'
18+
#' @return a list
19+
#'
20+
#' @export
21+
cjs_config_line <- function(
22+
tension = 0,
23+
backgroundColor = NULL,
24+
borderWidth = 3,
25+
borderColor = NULL,
26+
borderCapStyle = 'butt',
27+
borderDash = NULL,
28+
borderDashOffset = NULL,
29+
borderJoinStyle = 'miter',
30+
capBezierPoints = TRUE,
31+
cubicInterpolationMode = 'default',
32+
fill = FALSE,
33+
stepped = FALSE) {
34+
35+
parameters <- as.list(environment())
36+
parameters <- get_non_null_parameters(parameters)
37+
parameters
38+
39+
}

R/cjs_defaults.R

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#' Set chartjs defaults
2+
#'
3+
#' Set global defaults for subsequent chartjs objects in a document. To check
4+
#' available options, create a chartjs plot, examine in the browser, go to the
5+
#' javascript console and print the object "Chart.defaults".
6+
#'
7+
#' @param ... default options.
8+
#'
9+
#' @export
10+
cjs_defaults <- function(...){
11+
12+
# Capture arguments
13+
parameters <- list(...)
14+
parameters <- get_non_null_parameters(parameters)
15+
parameters <- relist_parameter_vector(parameters)
16+
17+
update_obj = jsonlite::toJSON(parameters, auto_unbox = T)
18+
19+
20+
htmltools::singleton(
21+
htmltools::tags$script(
22+
htmltools::HTML(
23+
"updateObjectInPlace(Chart.defaults,", update_obj, ");"
24+
)
25+
)
26+
)
27+
28+
}
29+
30+
31+
32+

R/cjs_tooltip.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#' Modify tooltips
22
#'
33
#' Uses a callback function within chartjs. Reference elements using template literal
4-
#' notation e.g. 'x value: ${x}, y value: ${y}'.
4+
#' notation e.g. 'x value: $\{x\}, y value: $\{y\}'.
55
#' See `vignette('tooltips-and-interaction-modes', package = 'chartjs4r')`
66
#'
77
#' @param p chartjs object

0 commit comments

Comments
 (0)