Skip to content

Commit

Permalink
fixed #809
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Jan 12, 2024
1 parent 9863372 commit fc8428b
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions R/tmapGridComp.R
Original file line number Diff line number Diff line change
Expand Up @@ -424,14 +424,19 @@ tmapGridLegPlot.tm_scalebar = function(comp, o, fH, fW) {
widths = wsu,
heights = hsu))

#fW_fact = 1


unit = comp$units$unit
unit.size = 1/comp$units$to
#xrange = (comp$bbox[3] - comp$bbox[1]) * fW_fact

xrange = fW * comp$cpi


# xrange is the range of the viewport in terms of coordinates
# xrange2 is the same but with units (e.g. km instead of m)
# W is the targeted space for the scalebar


W = as.numeric(wsu[3])

crop_factor = W / fW
Expand All @@ -445,23 +450,15 @@ tmapGridLegPlot.tm_scalebar = function(comp, o, fH, fW) {

xrange2 = xrange/unit.size

# to find the label width of first and last item, only used as proxy


if (is.null(comp$breaks)) {

# determine resolution
# determine resolution only (unselect steps that do not fit later (with 'sel'))
for (i in 10:1) {
tcks = pretty(c(0, xrange2*crop_factor), i)

tcks3 = (tcks / xrange2) * fW

tcksL = format(tcks, trim=TRUE)

labW = text_width_inch(tcksL) * comp$text.size

tickW = tcks3[-1] - head(tcks3, -1)

if (all(tickW > labW[-1])) {
sbW = W - labW
break
Expand All @@ -470,9 +467,13 @@ tmapGridLegPlot.tm_scalebar = function(comp, o, fH, fW) {
ticks2 = tcks
} else {
ticks2 = comp$breaks
tcksL = format(ticks2, trim=TRUE)

labW = text_width_inch(tcksL) * comp$text.size
sbW = W - labW
}

ticks3 = ticks2 / xrange2 * fW # (ticks2*unit.size / xrange) * as.numeric(wsu[3])
ticks3 = ticks2 / xrange2 * fW
sel = which(ticks3 <= sbW)

if (!is.null(comp$breaks) && length(sel) != length(ticks3)) {
Expand Down

0 comments on commit fc8428b

Please sign in to comment.