-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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().
- Loading branch information
Showing
10 changed files
with
251 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
Package: mschart | ||
Type: Package | ||
Title: Chart Generation for 'Microsoft Word' and 'Microsoft PowerPoint' Documents | ||
Version: 0.4.1.001 | ||
Version: 0.4.1.002 | ||
Authors@R: c( | ||
person("David", "Gohel", role = c("aut", "cre"), | ||
email = "[email protected]"), | ||
person(given = "ArData", role = "cph"), | ||
person("YouGov", role = "fnd"), | ||
person("Jan Marvin", "Garbuszus", role = "ctb", comment = "support for openxls2"), | ||
person("Stefan", "Moog", role = "ctb", email = '[email protected]', comment = "support to set chart and plot area color and border"), | ||
person("Eli", "Daniels", role = "ctb", email = '[email protected]'), | ||
person("Marlon", "Molina", role = "ctb", comment = "added table feature"), | ||
person("Rokas", "Klydzia", role = "ctb", comment = "custom labels"), | ||
person("David", "Camposeco", role = "ctb", | ||
|
@@ -25,10 +27,17 @@ License: MIT + file LICENSE | |
Encoding: UTF-8 | ||
LazyData: true | ||
Depends: R (>= 2.10) | ||
Imports: stats, data.table, | ||
officer (>= 0.3.6), cellranger, | ||
writexl, grDevices, xml2 (>= 1.1.0), | ||
htmltools, utils | ||
Imports: | ||
stats, | ||
data.table, | ||
officer (>= 0.3.6), | ||
cellranger, | ||
writexl, | ||
grDevices, | ||
xml2 (>= 1.1.0), | ||
htmltools, | ||
utils, | ||
scales | ||
URL: https://ardata-fr.github.io/officeverse/, https://ardata-fr.github.io/mschart/ | ||
BugReports: https://github.com/ardata-fr/mschart/issues | ||
RoxygenNote: 7.3.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#' @title xml code for chart and plot area properties | ||
#' @param theme an object of class mschart_theme | ||
#' @param what a character. One of `"plot"` or `"chart"` | ||
#' @noRd | ||
sppr_content_xml <- function(theme = NULL, what = "chart", ns = NULL) { | ||
if (!what %in% c("plot", "chart")) { | ||
stop("what sould be one of \"plot\" or \"chart\"") | ||
} | ||
|
||
fill <- theme[[paste0(what, "_background")]] | ||
border_properties <- theme[[paste0(what, "_border")]] | ||
|
||
if (!is.null(fill)) { | ||
fill_elts <- col2rgb(fill, alpha = TRUE)[, 1] | ||
fill_hex <- sprintf("%02X%02X%02X", fill_elts[1], fill_elts[2], fill_elts[3]) | ||
fill_str <- sprintf( | ||
"<a:solidFill><a:srgbClr val=\"%s\"><a:alpha val=\"%.0f\"/></a:srgbClr></a:solidFill>", | ||
fill_hex, fill_elts[4] / 255.0 * 100000 | ||
) | ||
} else { | ||
fill_str <- NULL | ||
} | ||
|
||
border_str <- ooxml_fp_border(border_properties) | ||
|
||
if (!is.null(ns)) ns <- paste0(" ", ns) | ||
|
||
sppr_str <- paste0( | ||
"<c:spPr", ns, ">", | ||
fill_str, | ||
border_str, | ||
"</c:spPr>" | ||
) | ||
|
||
sppr_str | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#' Apply ggplot2 theme | ||
#' | ||
#' A theme that approximates the style of ggplot2::theme_grey. | ||
#' | ||
#' @param x a mschart object | ||
#' @param base_size base font size | ||
#' @param base_family font family | ||
#' | ||
#' @return a mschart object | ||
#' | ||
#' @export | ||
#' | ||
#' @section theme_ggplot2(): | ||
#' | ||
#' \if{html}{\figure{fig_theme_ggplot2.png}{options: width="500"}} | ||
#' | ||
#' @examples | ||
#' p <- ms_scatterchart( | ||
#' data = iris, x = "Sepal.Length", | ||
#' y = "Sepal.Width", group = "Species" | ||
#' ) | ||
#' | ||
#' p <- theme_ggplot2(p) | ||
#' p <- chart_fill_ggplot2(p) | ||
theme_ggplot2 <- function(x, base_size = 11, base_family = "Arial") { | ||
t <- mschart_theme( | ||
main_title = fp_text(color = "black", font.size = 1.2 * base_size, font.family = base_family), | ||
axis_title = fp_text(color = "black", font.size = base_size, font.family = base_family), | ||
axis_text = fp_text(color = "grey30", font.size = .8 * base_size, font.family = base_family), | ||
axis_ticks = fp_border(color = "grey20", width = 1, style = "solid"), | ||
grid_major_line_x = fp_border(color = "white", width = 1, style = "solid"), | ||
grid_major_line_y = fp_border(color = "white", width = 1, style = "solid"), | ||
grid_minor_line_x = fp_border(color = "white", width = .5, style = "solid"), | ||
grid_minor_line_y = fp_border(color = "white", width = .5, style = "solid"), | ||
chart_background = "white", | ||
plot_background = "grey92", | ||
legend_text = fp_text(color = "black", font.size = base_size, font.family = base_family), | ||
legend_position = "r" | ||
) | ||
set_theme(x, t) | ||
} | ||
|
||
#' Apply ggplot2 color scale | ||
#' | ||
#' The default hue color scale from ggplot2. | ||
#' | ||
#' @param x a mschart object | ||
#' @param stroke a boolean. Apply the color scale to stroke? Defaults to `TRUE`. | ||
#' | ||
#' @return a mschart object | ||
#' | ||
#' @export | ||
#' | ||
#' @section chart_fill_ggplot2(): | ||
#' | ||
#' \if{html}{\figure{fig_theme_ggplot2.png}{options: width="500"}} | ||
#' | ||
#' @examples | ||
#' p <- ms_scatterchart( | ||
#' data = iris, x = "Sepal.Length", | ||
#' y = "Sepal.Width", group = "Species" | ||
#' ) | ||
#' | ||
#' p <- theme_ggplot2(p) | ||
#' p <- chart_fill_ggplot2(p) | ||
chart_fill_ggplot2 <- function(x, stroke = TRUE) { | ||
if (!is.null(x$group)) { | ||
groups <- unique(x$data[[x$group]]) | ||
ngroups <- length(groups) | ||
pal <- scales::hue_pal()(ngroups) | ||
names(pal) <- groups | ||
} else { | ||
pal <- scales::hue_pal()(1) | ||
} | ||
|
||
x <- chart_data_fill(x, values = pal) | ||
if (stroke) x <- chart_data_stroke(x, values = pal) | ||
x | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.