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

Automatic scaling in e_bar #459

Open
svenb78 opened this issue Sep 8, 2022 · 2 comments
Open

Automatic scaling in e_bar #459

svenb78 opened this issue Sep 8, 2022 · 2 comments

Comments

@svenb78
Copy link

svenb78 commented Sep 8, 2022

Hi,

given the example below:

df_01 <- data.frame(
  date = c("2022", "2021")
  , value = c(11000000, 15000000)
)

plot_e <- df_01 %>%
  echarts4r::e_chart(x = date) %>%
  echarts4r::e_bar(serie = value)

plot_e

Is there a simple way for an "automatic" scaling of the y-axis, without using something like dplyr::mutate(value = value / 1e6)?

Background: I use e_charts within a shiny app with multiple filters on hierarchical data. At the top level with few filters, the values are about millions, and the y-axis labels become quite long. Scaling them using the mutate example leads to the problem, that on a low level with many filters, the values get small and are printed with a lot of decimal places.

@helgasoft
Copy link
Contributor

The simple way is Y-axis scale

data.frame(
	date = c("2022", "2021")
	,value = c(11000000, 15000000)
) |>
	e_chart(x = date) |>
	e_bar(serie = value) |>
	e_y_axis(scale= TRUE, min= 10000000) |> 
	e_grid(containLabel= TRUE)

image

@svenb78
Copy link
Author

svenb78 commented Sep 8, 2022

Hi,

thanks, but your proposal does not solve the problem which is the large number of zeros / digits at the y-axis ticks / labels. If I manually "down scale" the values, for example by using dplyr::mutate(value = value / 1e6), I get rid of the zeros in that case, but if the values become small as a result of filtering (-> shiny), I get axis ticks like 0.00001 and again too many digits.

So, what I'm looking for is an option which automatically recognizes the number of digits and scales the labels accordingly. Means: If value = 15000000 the label should be something like 15 Mio.. If value = 10 it should be 10.

What I found so far is that function (-> Link ):

comprss <- function(tx) { 
  div <- findInterval(as.numeric(gsub("\\,", "", tx)), 
                      c(0, 1e3, 1e6) )  # modify this if negative numbers are possible
  paste(round( as.numeric(gsub("\\,","",tx))/10^(3*(div-1)), 2), 
        c("","Tsd.","Mio.")[div] )}

With the help of comprss , I can modify df_01 as follows:

df_01$scaled_value <- comprss(df_01$value)

Now, I have a column with nicely scaled values, but they are character and I can use them only like that:

plot_e <- df_01 %>%
  echarts4r::e_charts(x = date) %>%
  echarts4r::e_bar(serie = value, bind = scaled_value, label = list(show=TRUE, formatter = "{b}"))

plot_e

The y-axis labels stay unaffected and my problem is not solved.

Ann.: So far, I helped myself by hiding the y-axis labels, but that is not what I actually want. :-)

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

2 participants