#library(chartjs4r)
devtools::load_all()
#>  Loading chartjs4r

Introduction

We can set the defaults for all chartjs charts in a document using cjs_defaults(). The list of chartjs defaults can be found in the javascript object Chart.defaults.

This function alters the Chartjs.defaults inplace, and creating incorrect settings can have undesired side-effects. So be careful!

For example, we can see the borderColor is a single value, while font is a list of options

// In browser console (javascript)
Chart.defaults
...
borderColor: "pink"
...
font: {
    "family": "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
    "size": 16,
    "style": "normal",
    "lineHeight": 1.2
}
...

Thus we need to pass a similar object if we wish to change them. cjs_defaults() will cycle through the parameters provided and update the Chart.defaults object.

# font is a list
cjs_defaults(borderColor = 'pink', font = list(size = 16))

Now all of the remaining plots will have pink borderColor

chartjs() |>
  cjs_add_points(x = 1:10,
                 y = rnorm(10),
                 show_line = T)
chartjs() |>
  cjs_add_bars(x = letters[1:3],
               y = rpois(3, 10))