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

Remove comma in thousands #173

Closed
etiennebacher opened this issue Jul 16, 2020 · 6 comments
Closed

Remove comma in thousands #173

etiennebacher opened this issue Jul 16, 2020 · 6 comments

Comments

@etiennebacher
Copy link
Contributor

etiennebacher commented Jul 16, 2020

I have a plot where the x-axis scale is in thousands, but it corresponds to years so I would like to remove the comma, in order to have 1900 instead of 1,900 for example.

library(tibble)
library(echarts4r)

data_test <- tibble(
  year = seq(1900, 1920, 1),
  variable = seq(200, 400, 10)
)

data_test %>%
  e_charts(year) %>%
  e_x_axis(year) %>%
  e_y_axis(variable) %>%
  e_line(variable)

I don't think that e_axis_formatter can do that. Is there a way to remove the comma?

I have already posted this question on StackOverflow.

@JohnCoene
Copy link
Owner

If your data is not missing any year then this could be treated as categorical variable.

library(tibble)
library(echarts4r)

data_test <- tibble(
    year = seq(1900, 1920, 1),
    variable = seq(200, 400, 10)
)

data_test %>%
    dplyr::mutate(year = as.factor(year)) %>% 
    e_charts(year) %>%
    e_line(variable)

If you are missing years let me know there are other ways that involve some JavaScript.

@etiennebacher
Copy link
Contributor Author

Perfect, it works. I just had to remove e_x_axis(year), otherwise I have the following error:

Error in Summary.factor(1:113, na.rm = FALSE) :  ‘range’ not meaningful for factors`

I don't have missing years but maybe you should also detail the solution when there are missing years, for future reference.

Let me know if you want to answer on SO as well or if I copy-paste your solution.

@JohnCoene
Copy link
Owner

Thanks for pointing this out, I fixed that error with *_axis functions.

To change the label in a more robust way you have to change the formatter, a JavaScript function, in this case it's simply a matter of returning the value as-is but at least it keeps echarts.js from formatting it.

library(echarts4r)

data_test <- tibble(
    year = c(1900, 1901, 1905),
    variable = 1:3
)

label <- list(
    formatter = htmlwidgets::JS(
        'function(value, index){
            return value;
        }'
    )
)

data_test %>%
    e_charts(year) %>%
    e_y_axis(variable) %>% 
    e_line(variable) %>% 
    e_x_axis(serie = year, axisLabel = label)

@etiennebacher
Copy link
Contributor Author

I am reopening this because the solution using JavaScript does not work when I hover the graph whereas the first one (using factor) works.

library(tibble)
library(echarts4r)

### First solution: works
data_test <- tibble(
  year = seq(1900, 1920, 1),
  variable = seq(200, 400, 10)
)

data_test %>%
  dplyr::mutate(year = as.factor(year)) %>% 
  e_charts(year) %>%
  e_line(variable) %>%
  e_tooltip(trigger = "axis") # I added this

### Second solution: does not work 
data_test <- tibble(
  year = c(1900, 1901, 1905),
  variable = 1:3
)

label <- list(
  formatter = htmlwidgets::JS(
    'function(value, index){
            return value;
        }'
  )
)

data_test %>%
  e_charts(year) %>%
  e_y_axis(variable) %>% 
  e_line(variable) %>% 
  e_x_axis(serie = year, axisLabel = label) %>%
  e_tooltip(trigger = "axis") # I added this

Is there a way to keep the second solution, which is more robust, and to remove the comma when hovering the points?

@etiennebacher etiennebacher reopened this Jul 17, 2020
@JohnCoene
Copy link
Owner

Yes, there is some documentation on tooltips on the website

@etiennebacher
Copy link
Contributor Author

Okay, I put here what I did, just for the record:

data_test %>%
  e_charts(year) %>%
  e_y_axis(variable) %>% 
  e_line(variable) %>% 
  e_x_axis(serie = year, axisLabel = label) %>%
  e_tooltip(
    formatter = htmlwidgets::JS(
      "function(params){
            return (params.value[0] + ' <br/> variable: ' + params.value[1] )
        }"
    )
  ) 

However, this doesn't work if I add trigger = "axis" in e_tooltip, but this is not the point of this issue, so maybe this will be developed in another issue.

By the way, there are some errors in the code of some of the examples in the help page. From "In order to customize the tooltip, ...", some chunks of code are repeated and therefore copy-paste does not work.

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