Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

color_bar text formatting #106

Open
ixodid198 opened this issue Apr 2, 2018 · 5 comments
Open

color_bar text formatting #106

ixodid198 opened this issue Apr 2, 2018 · 5 comments

Comments

@ixodid198
Copy link

When the value for color_bar is small relative to the length the bar, the text seems misplaced. In the image see:

Nova Scotia, Increasing
The "2" is placed to the left of the bar.

Newfoundland and Labrador, Increasing
The "70" is misaligned with all the other numbers in its column.

Is this the desired behaviour?
It may well be. I'm new at most of this.
If this is correct, how can I stop it from happening?

screen shot 2018-04-02 at 5 30 16 pm

@pdelboca
Copy link

This issue happens everytime data "grow exponentially" (Bigger differencies between max and min values). This can be reproduced with this code:

data.frame(
  key = LETTERS[1:4],
  value = c(1000,100,10,1)
) %>%
  mutate(value = color_bar()(value)) %>%
  kable("html", escape = F) %>%
  kable_styling("hover", full_width = F)

I'm not a css expert but I found removing the 'display:rtl' implemented in #49 solves this issue. I think focing text direction from right-to-left solves some issues but creates another. I did a quick workarround redefining the function in my code without that parameter.

@mikekaminsky
Copy link

@pdelboca how did you redefine the function? I'm running into this issue and would like to implement.

@ixodid198
Copy link
Author

ixodid198 commented Jul 25, 2019 via email

@psychemedia
Copy link

psychemedia commented Mar 8, 2021

I've noticed that if the bar is narrow, text formatting can become an issue when rendering as HTML when using bookdown:

image

Using CSS linear gradient seems to get round the issue:

bg = function(start, end, color, ...) {
  paste("linear-gradient(90deg,transparent ",percent(start),",",
        color, percent(start), ",", color, percent(end),
        ", transparent", percent(end),")")
} 

color_bar2 =  function (color = "lightgray", fun = "proportion", ...) 
{
    fun <- match.fun(fun)
    formatter("span", style = function(x) style(display = "inline-block",
                `unicode-bidi` = "plaintext", 
                "background" = bg(1-fun(as.numeric(x), ...), 1, color), "width"="100%" ))
}

image

@nvelden
Copy link

nvelden commented May 17, 2021

When the function is defined outside the package don't forget to add a name space before the style function:

{
    fun <- match.fun(fun)
    formatter("span", style = function(x) formattable::style(display = "inline-block",
                `unicode-bidi` = "plaintext", 
                "background" = bg(1-fun(as.numeric(x), ...), 1, color), "width"="100%" ))
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants