A chart template is a zip package holding a chart part without data
references. Excel writes them from "Save as Template"; other applications
read them as well. encharter_to_crtx() saves a Chart as a template,
encharter_from_crtx() reads a template into a Chart without series,
and Chart$apply_crtx() copies the styling of a template onto an existing
chart.
Usage
encharter_to_crtx(chart, path)
ec_to_crtx(chart, path)
encharter_from_crtx(path)
ec_from_crtx(path)Details
A template read with encharter_from_crtx() keeps the per-series styling
(color, line, marker) of the template. Series added to it with
$add_series() take that styling in order, for the arguments not given
explicitly; when the template has fewer series than added, its styles are
reused from the start.
Templates cover standard charts only; extended charts (ChartEx) are not
supported. Templates written by Excel also carry a theme override and a
preview image, which are not read; a template written here holds the
chart part only.
Examples
wb <- openxlsx2::wb_workbook()$add_worksheet("Data")$add_data(x = data.frame(
Month = month.abb[1:6], Sales = c(120, 135, 128, 160, 175, 190)
))
chart <- ec("bar")$set_chart_title("Sales", bold = TRUE)$
set_y_axis(grid_lines = TRUE, grid_color = "EEEEEE")$
add_series(name = Sales, data = openxlsx2::wb_data(wb), label = Month, color = "2E4057")
tmp <- tempfile(fileext = ".crtx")
encharter_to_crtx(chart, tmp)
chart2 <- encharter_from_crtx(tmp)
chart2$add_series(name = "Data!$B$1", data = "Data!$B$2:$B$7", label = "Data!$A$2:$A$7")
chart2$series_data[[1]]$line$color
#> [1] "2E4057"
