英文:
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
-
Stack Overflow - Remove comma in thousands in echarts4r
But I'd rather change the symbols than remove it. -
Stack Overflow - Set global thousands separator for echarts
Wasn't answered with an example.
英文:
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
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论