Skip to content

Commit

Permalink
#919 fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Aug 24, 2024
1 parent 8823764 commit e7aa29a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
3 changes: 3 additions & 0 deletions R/step1_rearrange.R
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ impute_comp = function(a, o) {

ca = class(a)

call = names(a)

# special case: position, in case c("left", "top") is used
if (is.character(a$position)) a$position = str2pos(a$position)
if (is.numeric(a$position)) a$position = num2pos(a$position)
Expand All @@ -276,6 +278,7 @@ impute_comp = function(a, o) {


a = complete_options(a, ot)
a$call = call

class(a) = ca
a
Expand Down
20 changes: 12 additions & 8 deletions R/tmapGridComp.R
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,8 @@ tmapGridCompPrepare.tm_scalebar = function(comp, o) {
show.messages = o$show.messages
show.warnings = o$show.warnings
within(comp, {
if (is.function(call)) call = ""
if (all(c("breaks", "width") %in% call) && show.warnings) {
warning("For 'tm_scalebar()', 'breaks' and 'width' cannot be used together. The width is being ignored.", call. = FALSE)
message("For 'tm_scalebar()', 'breaks' and 'width' are not supposed to be used together; normally, setting the exact width is not needed when breaks have been specified.", call. = FALSE)
}
if ("breaks" %in% call) {
if (breaks[1] != 0) {
Expand Down Expand Up @@ -391,8 +390,7 @@ tmapGridCompWidth.tm_scalebar = function(comp, o) {
comp$WnativeID = 3
if (!is.null(comp$breaks)) {
comp$WnativeRange = tail(comp$breaks, 1) - comp$breaks[1]# + (comp$breaks[2] - comp$breaks[1]) * 2
comp$Wextra_line = textS * o$lin# for fitting break labels

#comp$Wextra_text_inch = text_width_inch(paste0(" ", cmp$units$unit)) + text_width_inch(paste0(tail(comp$breaks, 1), comp$breaks[1])) / 2
}

comp
Expand Down Expand Up @@ -486,8 +484,10 @@ tmapGridLegPlot.tm_scalebar = function(comp, o, fH, fW) {

ticks2Labels = format(ticks2, trim=TRUE)
ticksWidths = text_width_inch(ticks2Labels)
unitWidth = text_width_inch(unit)

labels = c(ticks2Labels, unit)
labelsW = c(ticksWidths, unitWidth)

n = length(ticks2)

Expand All @@ -501,10 +501,14 @@ tmapGridLegPlot.tm_scalebar = function(comp, o, fH, fW) {
#width = sum(widths[-n]) + .5*ticksWidths[1]*size + .5*ticksWidths[n]*size+ unitWidth #widths * n

xtext = x[1] + c(ticks3, ticks3[n] + .5*ticksWidths[n]*size + .5*unitWidth)# + widths*.5 + unitWidth*.5) #+ position[1]

#x = just-just*width+x
#xtext = just-just*width+xtext


# if "unit" text is clipped, remove last label and move unit to previous label
xright = xtext + labelsW / 2
if (tail(xright, 1) > W) {
labels = c(head(labels, -2), unit)
xtext = x[1] + c(head(ticks3, -1), ticks3[n-1] + .5*ticksWidths[n-1]*size + .5*unitWidth)# + widths*.5 + unitWidth*.5) #+ position[1]
}

grobBG = if (getOption("tmap.design.mode")) rectGrob(gp=gpar(fill="orange")) else NULL


Expand Down
6 changes: 2 additions & 4 deletions R/tmapGridLegend.R
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,13 @@ tmapGridLegend = function(comp, o, facet_row = NULL, facet_col = NULL, facet_pag

oldIn = as.numeric(cmp$wsu[cmp$WnativeID])

if (is.null(cmp$WnativeRange)) {
if (is.null(cmp$WnativeRange) || ("width" %in% cmp$call)) {
# in case breaks not defined: allow the width to be maximal fraction of facet width
#newIn = min(totW, bb_facet * cmp$width)
newIn = min(totW, cmp$Win)
} else {
# in case breaks are defined: compute width

# for fitting breaks (half of last 'breaks' word + width of units text)
Wextra = (ceiling(nchar(tail(cmp$breaks, 1)) / 2) + nchar(cmp$units$unit)/2) * cmp$Wextra_line # for fitting break labels (in theory, it should include a " ", and a bit for the left-hand side, but in practice this is a better fit)
Wextra = (text_width_inch(paste0(" ", cmp$units$unit)) + text_width_inch(paste0(tail(cmp$breaks, 1), cmp$breaks[1])) / 2 ) * cmp$text.size

# bbw are number of (CRS) units of map width, totalCoords translated to scalebar units
totalCoords = bbw * cmp$units$to
Expand Down

0 comments on commit e7aa29a

Please sign in to comment.