更改轴和工具提示中的小数点和千位分隔符。

huangapple go评论72阅读模式
英文:

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

英文:

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 &lt;- data.frame(x=1:3, y=c(1000,2000,2500))

e_charts(my_data, x = x) %&gt;% e_bar(y) %&gt;% 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

https://stackoverflow.com/questions/62932480/remove-comma-in-thousands-in-echarts4r
But I'd rather change the symbols than remove it.
https://stackoverflow.com/questions/61934422/set-global-thousands-separator-for-echarts
Wasn't answered with an example.

答案1

得分: 1

这里是调整 formatter 以获得所需输出的唯一方法:

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][1]][1]


[1]: https://i.stack.imgur.com/H7k3E.gif

<details>
<summary>英文:</summary>

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][1]][1]


  [1]: https://i.stack.imgur.com/H7k3E.gif

</details>



huangapple
  • 本文由 发表于 2023年5月29日 15:35:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定