Skip to content

Commit

Permalink
fix: % in labels are not generating an error
Browse files Browse the repository at this point in the history
fix #80
  • Loading branch information
davidgohel committed Nov 26, 2022
1 parent f7514a4 commit e63e3e3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 41 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: mschart
Type: Package
Title: Chart Generation for 'Microsoft Word' and 'Microsoft PowerPoint' Documents
Version: 0.4.0.003
Version: 0.4.0.004
Authors@R: c(
person("David", "Gohel", role = c("aut", "cre"),
email = "[email protected]"),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* Support for openxlsx2 by Jan Marvin Garbuszus
* option to add table of data below corresponding levels by Marlon Molina

## Issues

* fix issue with % in labels of the graphic

# mschart 0.3.1

## New features
Expand Down
78 changes: 42 additions & 36 deletions R/axis_codes.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ get_axis_tag <- function(x){
#' @param is_x if TRUE, generate xml for x axis, else for y axis
#' @param lab label for the axis
#' @param rot rotation of title
axis_content_xml <- function(x, id, cross_id, theme, is_x = TRUE, lab = NULL, rot = 0 ){

axis_content_xml <- function(x, id, cross_id, theme, is_x = TRUE, lab = NULL, rot = 0) {
x_title_id <- paste0("axis_title_", ifelse(is_x, "x", "y"))

if( is.null(lab)) {
if (is.null(lab)) {
title_ <- ""
} else {
title_ <- "<c:title><c:tx><c:rich><a:bodyPr rot=\"%.0f\" vert=\"horz\" anchor=\"ctr\"/><a:lstStyle/><a:p><a:pPr><a:defRPr/></a:pPr><a:r>%s<a:t>%s</a:t></a:r></a:p></c:rich></c:tx><c:layout/><c:overlay val=\"0\"/></c:title>"
title_ <- sprintf(title_, rot * 60000 , format(theme[[x_title_id]], type = "pml" ), lab )
title_ <- sprintf(title_, rot * 60000, format(theme[[x_title_id]], type = "pml"), lab)
}

major_tm <- "<c:majorTickMark val=\"%s\"/>"
Expand All @@ -46,38 +45,44 @@ axis_content_xml <- function(x, id, cross_id, theme, is_x = TRUE, lab = NULL, ro

grid_major_id <- paste0("grid_major_line_", ifelse(is_x, "x", "y"))
major_gl <- ooxml_fp_border(theme[[grid_major_id]],
in_tags = c("c:majorGridlines", "c:spPr"))
in_tags = c("c:majorGridlines", "c:spPr")
)

grid_minor_id <- paste0("grid_minor_line_", ifelse(is_x, "x", "y"))
minor_gl <- ooxml_fp_border(theme[[grid_minor_id]],
in_tags = c("c:minorGridlines", "c:spPr"))
in_tags = c("c:minorGridlines", "c:spPr")
)

lim_max <- ""
if( !is.null(x$limit_max) )
lim_max <- sprintf("<c:max val=\"%.02f\"/>", x$limit_max )
if (!is.null(x$limit_max)) {
lim_max <- sprintf("<c:max val=\"%.02f\"/>", x$limit_max)
}
lim_min <- ""
if( !is.null(x$limit_min) )
lim_min <- sprintf("<c:min val=\"%.02f\"/>", x$limit_min )
if (!is.null(x$limit_min)) {
lim_min <- sprintf("<c:min val=\"%.02f\"/>", x$limit_min)
}

scaling_str <- sprintf("<c:scaling><c:orientation val=\"%s\"/>%s%s</c:scaling>", x$orientation, lim_max, lim_min )
delete <- sprintf("<c:delete val=\"%.0f\"/>", x$delete )
position <- sprintf("<c:axPos val=\"%s\"/>", x$axis_position )
crosses <- sprintf("<c:crosses val=\"%s\"/>", x$crosses )
scaling_str <- sprintf("<c:scaling><c:orientation val=\"%s\"/>%s%s</c:scaling>", x$orientation, lim_max, lim_min)
delete <- sprintf("<c:delete val=\"%.0f\"/>", x$delete)
position <- sprintf("<c:axPos val=\"%s\"/>", x$axis_position)
crosses <- sprintf("<c:crosses val=\"%s\"/>", x$crosses)

lim_max <- ""
if( !is.null(x$limit_max) )
lim_max <- sprintf("<c:max val=\"%.02f\"/>", x$limit_max )
if (!is.null(x$limit_max)) {
lim_max <- sprintf("<c:max val=\"%.02f\"/>", x$limit_max)
}
lim_min <- ""
if( !is.null(x$limit_min) )
lim_min <- sprintf("<c:min val=\"%.02f\"/>", x$limit_min )
if (!is.null(x$limit_min)) {
lim_min <- sprintf("<c:min val=\"%.02f\"/>", x$limit_min)
}
cross_at <- ""
if( !is.null(x$position) ){
cross_at <- sprintf("<c:crossesAt val=\"%.02f\"/>", x$position )
if (!is.null(x$position)) {
cross_at <- sprintf("<c:crossesAt val=\"%.02f\"/>", x$position)
crosses <- ""
}

num_fmt <- ""
if( !is.null(x$num_fmt) ){
if (!is.null(x$num_fmt)) {
num_fmt <- sprintf("<c:numFmt formatCode=\"%s\" sourceLinked=\"0\"/>", x$num_fmt)
}

Expand All @@ -88,27 +93,28 @@ axis_content_xml <- function(x, id, cross_id, theme, is_x = TRUE, lab = NULL, ro

axis_major_ticks_id <- paste0("axis_ticks_", ifelse(is_x, "x", "y"))
axis_ticks <- ooxml_fp_border(theme[[axis_major_ticks_id]],
in_tags = c("c:spPr"))
in_tags = c("c:spPr")
)


labels_text_id <- paste0("axis_text_", ifelse(is_x, "x", "y"))
rpr <- format(theme[[labels_text_id]], type = "pml")
rpr <- gsub("a:rPr", "a:defRPr", rpr)
labels_text_pr <- "<c:txPr><a:bodyPr rot=\"%.0f\" vert=\"horz\"/><a:lstStyle/><a:p><a:pPr>%s</a:pPr></a:p></c:txPr>"
labels_text_pr <- sprintf(labels_text_pr, x$rotation * 60000, rpr )

str_ <- paste0( "<c:axId val=\"%s\"/>",
scaling_str, delete, position,
major_gl, minor_gl,
title_,
major_tm, minor_tm, tl_pos,
labels_text_pr,
axis_ticks, num_fmt,
"<c:crossAx val=\"%s\"/>",
cross_at,
crosses)
str_ <- sprintf(str_, id, cross_id)
labels_text_pr <- sprintf(labels_text_pr, x$rotation * 60000, rpr)

str_ <- paste0(
sprintf("<c:axId val=\"%s\"/>", id),
scaling_str, delete, position,
major_gl, minor_gl,
title_,
major_tm, minor_tm, tl_pos,
labels_text_pr,
axis_ticks, num_fmt,
sprintf("<c:crossAx val=\"%s\"/>", cross_id),
cross_at,
crosses
)
str_

}

6 changes: 3 additions & 3 deletions R/chart_labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
#' mylc <- chart_labels(mylc, title = "my title", xlab = "my x label",
#' ylab = "my y label")
chart_labels <- function( x, title = NULL, xlab = NULL, ylab = NULL){
if( !is.null(title) ) x$labels[["title"]] <- htmlEscape(title)
if( !is.null(title) ) x$labels[["title"]] <- title
else x$labels[["title"]] <- NULL

if( !is.null(xlab) ) x$labels[["x"]] <- htmlEscape(xlab)
if( !is.null(xlab) ) x$labels[["x"]] <- xlab
else x$labels[["x"]] <- NULL

if( !is.null(ylab) ) x$labels[["y"]] <- htmlEscape(ylab)
if( !is.null(ylab) ) x$labels[["y"]] <- ylab
else x$labels[["y"]] <- NULL
x
}
2 changes: 1 addition & 1 deletion R/ms_chart.R
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ format.ms_chart <- function(x, id_x, id_y, sheetname = "sheet1", drop_ext_data =
if (!is.null(x$labels[["title"]])) {
chartnode <- xml_find_first(xml_doc, "//c:chart")
title_ <- "<c:title %s><c:tx><c:rich><a:bodyPr/><a:lstStyle/><a:p><a:pPr><a:defRPr/></a:pPr><a:r>%s<a:t>%s</a:t></a:r></a:p></c:rich></c:tx><c:layout/><c:overlay val=\"0\"/></c:title>"
title_ <- sprintf(title_, ns, format(x$theme[["main_title"]], type = "pml"), x$labels[["title"]])
title_ <- sprintf(title_, ns, format(x$theme[["main_title"]], type = "pml"), htmlEscape(x$labels[["title"]]))
xml_add_child(chartnode, as_xml_document(title_), .where = 0)
} else { # null is not enough
atd_node <- xml_find_first(xml_doc, "//c:chart/c:autoTitleDeleted")
Expand Down

0 comments on commit e63e3e3

Please sign in to comment.