Skip to content

Commit 944c6d5

Browse files
authored
Add support to set chart and plot area color and border
* Add functionality to set background fill color and border of chart and plot area. * Add theme_ggplot2() and chart_fill_ggplot2().
1 parent da9fb75 commit 944c6d5

10 files changed

+251
-6
lines changed

DESCRIPTION

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
Package: mschart
22
Type: Package
33
Title: Chart Generation for 'Microsoft Word' and 'Microsoft PowerPoint' Documents
4-
Version: 0.4.1.001
4+
Version: 0.4.1.002
55
Authors@R: c(
66
person("David", "Gohel", role = c("aut", "cre"),
77
email = "[email protected]"),
88
person(given = "ArData", role = "cph"),
99
person("YouGov", role = "fnd"),
1010
person("Jan Marvin", "Garbuszus", role = "ctb", comment = "support for openxls2"),
11+
person("Stefan", "Moog", role = "ctb", email = '[email protected]', comment = "support to set chart and plot area color and border"),
12+
person("Eli", "Daniels", role = "ctb", email = '[email protected]'),
1113
person("Marlon", "Molina", role = "ctb", comment = "added table feature"),
1214
person("Rokas", "Klydzia", role = "ctb", comment = "custom labels"),
1315
person("David", "Camposeco", role = "ctb",
@@ -25,10 +27,17 @@ License: MIT + file LICENSE
2527
Encoding: UTF-8
2628
LazyData: true
2729
Depends: R (>= 2.10)
28-
Imports: stats, data.table,
29-
officer (>= 0.3.6), cellranger,
30-
writexl, grDevices, xml2 (>= 1.1.0),
31-
htmltools, utils
30+
Imports:
31+
stats,
32+
data.table,
33+
officer (>= 0.3.6),
34+
cellranger,
35+
writexl,
36+
grDevices,
37+
xml2 (>= 1.1.0),
38+
htmltools,
39+
utils,
40+
scales
3241
URL: https://ardata-fr.github.io/officeverse/, https://ardata-fr.github.io/mschart/
3342
BugReports: https://github.com/ardata-fr/mschart/issues
3443
RoxygenNote: 7.3.1

NAMESPACE

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export(chart_data_size)
1919
export(chart_data_smooth)
2020
export(chart_data_stroke)
2121
export(chart_data_symbol)
22+
export(chart_fill_ggplot2)
2223
export(chart_labels)
2324
export(chart_labels_text)
2425
export(chart_settings)
@@ -30,6 +31,7 @@ export(ms_linechart)
3031
export(ms_scatterchart)
3132
export(mschart_theme)
3233
export(set_theme)
34+
export(theme_ggplot2)
3335
importFrom(cellranger,as.cell_limits)
3436
importFrom(cellranger,as.range)
3537
importFrom(cellranger,cell_limits)

R/ms_chart.R

+8-1
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,10 @@ format.ms_chart <- function(x, id_x, id_y, sheetname = "sheet1", drop_ext_data =
405405

406406
table_str <- table_content_xml(x)
407407

408+
sppr_str <- sppr_content_xml(x$theme, "plot")
409+
408410
ns <- "xmlns:c=\"http://schemas.openxmlformats.org/drawingml/2006/chart\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\""
409-
xml_elt <- paste0("<c:plotArea ", ns, "><c:layout/>", str_, x_axis_str, y_axis_str, table_str, "</c:plotArea>")
411+
xml_elt <- paste0("<c:plotArea ", ns, "><c:layout/>", str_, x_axis_str, y_axis_str, table_str, sppr_str, "</c:plotArea>")
410412
xml_doc <- read_xml(system.file(package = "mschart", "template", "chart.xml"))
411413

412414
node <- xml_find_first(xml_doc, "//c:plotArea")
@@ -436,6 +438,11 @@ format.ms_chart <- function(x, id_x, id_y, sheetname = "sheet1", drop_ext_data =
436438
legend_ <- xml_find_first(xml_doc, "//c:chart/c:legend")
437439
xml_add_child(legend_, as_xml_document(labels_text_pr))
438440
}
441+
442+
chart_area_node <- xml_find_first(xml_doc, "//c:chartSpace")
443+
chart_area_properties <- sppr_content_xml(x$theme, what = "chart", ns = ns)
444+
xml_add_child(chart_area_node, as_xml_document(chart_area_properties))
445+
439446
if (drop_ext_data) {
440447
xml_remove(xml_find_first(xml_doc, "//c:externalData"))
441448
}

R/sppr_codes.R

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#' @title xml code for chart and plot area properties
2+
#' @param theme an object of class mschart_theme
3+
#' @param what a character. One of `"plot"` or `"chart"`
4+
#' @noRd
5+
sppr_content_xml <- function(theme = NULL, what = "chart", ns = NULL) {
6+
if (!what %in% c("plot", "chart")) {
7+
stop("what sould be one of \"plot\" or \"chart\"")
8+
}
9+
10+
fill <- theme[[paste0(what, "_background")]]
11+
border_properties <- theme[[paste0(what, "_border")]]
12+
13+
if (!is.null(fill)) {
14+
fill_elts <- col2rgb(fill, alpha = TRUE)[, 1]
15+
fill_hex <- sprintf("%02X%02X%02X", fill_elts[1], fill_elts[2], fill_elts[3])
16+
fill_str <- sprintf(
17+
"<a:solidFill><a:srgbClr val=\"%s\"><a:alpha val=\"%.0f\"/></a:srgbClr></a:solidFill>",
18+
fill_hex, fill_elts[4] / 255.0 * 100000
19+
)
20+
} else {
21+
fill_str <- NULL
22+
}
23+
24+
border_str <- ooxml_fp_border(border_properties)
25+
26+
if (!is.null(ns)) ns <- paste0(" ", ns)
27+
28+
sppr_str <- paste0(
29+
"<c:spPr", ns, ">",
30+
fill_str,
31+
border_str,
32+
"</c:spPr>"
33+
)
34+
35+
sppr_str
36+
}

R/theme.R

+26
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ set_theme <- function(x, value) {
4848
#' @param axis_ticks,axis_ticks_x,axis_ticks_y axis ticks formatting properties (see [fp_border()])
4949
#' @param grid_major_line,grid_major_line_x,grid_major_line_y major grid lines formatting properties (see [fp_border()])
5050
#' @param grid_minor_line,grid_minor_line_x,grid_minor_line_y minor grid lines formatting properties (see [fp_border()])
51+
#' @param plot_border plot area border lines formatting properties (see [fp_border()])
52+
#' @param chart_border chart area border lines formatting properties (see [fp_border()])
53+
#' @param plot_background plot area background fill color - single character value (e.g. "#000000" or "black")
54+
#' @param chart_background chart area background fill color - single character value (e.g. "#000000" or "black")
5155
#' @param date_fmt date format
5256
#' @param str_fmt string or factor format
5357
#' @param double_fmt double format
@@ -65,6 +69,8 @@ mschart_theme <- function(axis_title = fp_text(bold = TRUE, font.size = 16), axi
6569
axis_ticks = fp_border(color = "#99999999"), axis_ticks_x = axis_ticks, axis_ticks_y = axis_ticks,
6670
grid_major_line = fp_border(color = "#99999999", style = "dashed"), grid_major_line_x = grid_major_line, grid_major_line_y = grid_major_line,
6771
grid_minor_line = fp_border(width = 0), grid_minor_line_x = grid_minor_line, grid_minor_line_y = grid_minor_line,
72+
chart_background = NULL, chart_border = fp_border(color = "transparent"),
73+
plot_background = NULL, plot_border = fp_border(color = "transparent"),
6874
date_fmt = "yyyy/mm/dd", str_fmt = "General", double_fmt = "#,##0.00", integer_fmt = "0", legend_position = "b") {
6975
stopifnot(inherits(main_title, "fp_text"))
7076
stopifnot(inherits(table_text, "fp_text"))
@@ -84,6 +90,8 @@ mschart_theme <- function(axis_title = fp_text(bold = TRUE, font.size = 16), axi
8490
stopifnot(inherits(grid_minor_line, "fp_border"))
8591
stopifnot(inherits(grid_minor_line_x, "fp_border"))
8692
stopifnot(inherits(grid_minor_line_y, "fp_border"))
93+
stopifnot(inherits(chart_border, "fp_border"))
94+
stopifnot(inherits(plot_border, "fp_border"))
8795

8896
if (title_rot < 0 && title_rot > 359) {
8997
stop("title_rot must be between 0 and 359")
@@ -110,6 +118,8 @@ mschart_theme <- function(axis_title = fp_text(bold = TRUE, font.size = 16), axi
110118
axis_ticks_x = axis_ticks_x, axis_ticks_y = axis_ticks_y,
111119
grid_major_line_x = grid_major_line_x, grid_major_line_y = grid_major_line_y,
112120
grid_minor_line_x = grid_minor_line_x, grid_minor_line_y = grid_minor_line_y,
121+
chart_background = chart_background, chart_border = chart_border,
122+
plot_background = plot_background, plot_border = plot_border,
113123
legend_position = legend_position
114124
)
115125
class(out) <- "mschart_theme"
@@ -125,6 +135,8 @@ chart_theme <- function(x, axis_title_x, axis_title_y, main_title, legend_text,
125135
axis_ticks_x, axis_ticks_y,
126136
grid_major_line_x, grid_major_line_y,
127137
grid_minor_line_x, grid_minor_line_y,
138+
chart_background, chart_border,
139+
plot_background, plot_border,
128140
date_fmt, str_fmt, double_fmt, integer_fmt, legend_position) {
129141
if (!missing(axis_title_x)) {
130142
if (!all(class(axis_title_x) %in% class(x$theme$axis_title_x))) {
@@ -210,6 +222,20 @@ chart_theme <- function(x, axis_title_x, axis_title_y, main_title, legend_text,
210222
x$theme$grid_minor_line_y <- grid_minor_line_y
211223
}
212224

225+
if (!missing(chart_border)) {
226+
if (!all(class(chart_border) %in% class(x$theme$chart_border))) {
227+
stop("chart_border should be of class ", class(x$theme$chart_border))
228+
}
229+
x$theme$chart_border <- chart_border
230+
}
231+
232+
if (!missing(plot_border)) {
233+
if (!all(class(plot_border) %in% class(x$theme$plot_border))) {
234+
stop("plot_border should be of class ", class(x$theme$plot_border))
235+
}
236+
x$theme$plot_border <- plot_border
237+
}
238+
213239
if (!missing(date_fmt)) {
214240
if (!all(class(date_fmt) %in% class(x$theme$date_fmt))) {
215241
stop("date_fmt should be of class ", class(x$theme$date_fmt))

R/themes.R

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#' Apply ggplot2 theme
2+
#'
3+
#' A theme that approximates the style of ggplot2::theme_grey.
4+
#'
5+
#' @param x a mschart object
6+
#' @param base_size base font size
7+
#' @param base_family font family
8+
#'
9+
#' @return a mschart object
10+
#'
11+
#' @export
12+
#'
13+
#' @section theme_ggplot2():
14+
#'
15+
#' \if{html}{\figure{fig_theme_ggplot2.png}{options: width="500"}}
16+
#'
17+
#' @examples
18+
#' p <- ms_scatterchart(
19+
#' data = iris, x = "Sepal.Length",
20+
#' y = "Sepal.Width", group = "Species"
21+
#' )
22+
#'
23+
#' p <- theme_ggplot2(p)
24+
#' p <- chart_fill_ggplot2(p)
25+
theme_ggplot2 <- function(x, base_size = 11, base_family = "Arial") {
26+
t <- mschart_theme(
27+
main_title = fp_text(color = "black", font.size = 1.2 * base_size, font.family = base_family),
28+
axis_title = fp_text(color = "black", font.size = base_size, font.family = base_family),
29+
axis_text = fp_text(color = "grey30", font.size = .8 * base_size, font.family = base_family),
30+
axis_ticks = fp_border(color = "grey20", width = 1, style = "solid"),
31+
grid_major_line_x = fp_border(color = "white", width = 1, style = "solid"),
32+
grid_major_line_y = fp_border(color = "white", width = 1, style = "solid"),
33+
grid_minor_line_x = fp_border(color = "white", width = .5, style = "solid"),
34+
grid_minor_line_y = fp_border(color = "white", width = .5, style = "solid"),
35+
chart_background = "white",
36+
plot_background = "grey92",
37+
legend_text = fp_text(color = "black", font.size = base_size, font.family = base_family),
38+
legend_position = "r"
39+
)
40+
set_theme(x, t)
41+
}
42+
43+
#' Apply ggplot2 color scale
44+
#'
45+
#' The default hue color scale from ggplot2.
46+
#'
47+
#' @param x a mschart object
48+
#' @param stroke a boolean. Apply the color scale to stroke? Defaults to `TRUE`.
49+
#'
50+
#' @return a mschart object
51+
#'
52+
#' @export
53+
#'
54+
#' @section chart_fill_ggplot2():
55+
#'
56+
#' \if{html}{\figure{fig_theme_ggplot2.png}{options: width="500"}}
57+
#'
58+
#' @examples
59+
#' p <- ms_scatterchart(
60+
#' data = iris, x = "Sepal.Length",
61+
#' y = "Sepal.Width", group = "Species"
62+
#' )
63+
#'
64+
#' p <- theme_ggplot2(p)
65+
#' p <- chart_fill_ggplot2(p)
66+
chart_fill_ggplot2 <- function(x, stroke = TRUE) {
67+
if (!is.null(x$group)) {
68+
groups <- unique(x$data[[x$group]])
69+
ngroups <- length(groups)
70+
pal <- scales::hue_pal()(ngroups)
71+
names(pal) <- groups
72+
} else {
73+
pal <- scales::hue_pal()(1)
74+
}
75+
76+
x <- chart_data_fill(x, values = pal)
77+
if (stroke) x <- chart_data_stroke(x, values = pal)
78+
x
79+
}

man/chart_fill_ggplot2.Rd

+34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/figures/fig_theme_ggplot2.png

241 KB
Loading

man/set_theme.Rd

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/theme_ggplot2.Rd

+36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)