Search code examples
rechartsecharts4r

Change decimal and thousands separator in axis and tooltip


I'm making plots using echarts4r. In my language, the separators for decimals and thousands are reversed from english, i.e. commas for decimals, dots for thousands. How can I accomplish this for my charts? Here's an example:

library(dplyr)
library(echarts4r)
my_data <- data.frame(x=1:3, y=c(1000,2000,2500))

e_charts(my_data, x = x) %>% e_bar(y) %>% e_tooltip()

I'd like both the y axis and tooltip to have dots instead of commas in numbers.

I'd prefer doing it via Javascript in formatter arguments since I'm doing some other manipulations for detailed tooltips, but I'm not sure what to write for this purpose.

Similar questions

Remove comma in thousands in echarts4r
But I'd rather change the symbols than remove it.
Set global thousands separator for echarts
Wasn't answered with an example.


Solution

  • Here is ony way how to tweak formatter to get the desired output:

    library(dplyr)
    library(echarts4r)
    my_data <- data.frame(x = 1:3, y = c(100.23, 2000.21, 2500.12))
    
    
    
    e_charts(my_data, x = x) %>% 
      e_bar(y) %>% 
      e_tooltip(
        formatter = htmlwidgets::JS(
          "function(params) {
             var value = params.value[1].toFixed(2).replace('.', ',');
             return value.toString().replace(/(\\d)(?=(\\d{3})+(?!\\d))/g, '$1.');
           }"
        )
      ) 
    

    enter image description here