英文:
echarts4r use valueFormatter in tooltip
问题
valueFormatter
选项在echarts4r的tooltip中使用。根据e_tooltip的文档,我应该能够使用传递给echarts的...
参数。
例如,我希望以下代码能够正常工作:
library(dplyr)
library(echarts4r)
mtcars |>
group_by(cyl, gear) |>
summarise(mpg = mean(mpg)) |>
mutate(gear = as.character(gear)) |>
e_charts(gear) |>
e_bar(mpg) |>
e_tooltip(
trigger = "axis",
valueFormatter = htmlwidgets::JS("(value) => 'MPG is ' + value.toFixed(2)")
)
如果省略valueFormatter
,则会看到以下图片。目标是在工具提示中显示彩色点,然后在工具提示的右侧显示MPG is 21.50
(注意,它精确到2位小数,即显示尾随的0),MPG is 17.75
和MPG is 15.05
。
不幸的是,工具提示无法正常工作,而且会崩溃/不显示任何内容。
感谢任何帮助。
英文:
I want to use the valueFormatter
option in an echarts4r tooltip.
Following the documentation of e_tooltip
I should be able to use the ...
args as they are passed to echarts.
For example I would expect this to work
library(dplyr)
library(echarts4r)
mtcars |>
group_by(cyl, gear) |>
summarise(mpg = mean(mpg)) |>
mutate(gear = as.character(gear)) |>
e_charts(gear) |>
e_bar(mpg) |>
e_tooltip(
trigger = "axis",
valueFormatter = htmlwidgets::JS("(value) => 'MPG is ' + value.toFixed(2)")
)
Leaving out the valueFormatter, I see this picture. The goal is to have the colored-dots in the tooltip and then on the RHS of the tooltip I want to see MPG is 21.50
(Note that its fixed to 2 digits! aka showing the trailing 0), MPG is 17.75
and MPG is 15.05
.
Unfortunately, the tooltip does not work and crashes/shows nothing.
Any help is appreciated.
答案1
得分: 1
问题在于传递给你的格式化程序的 value
是一个字符串。因此,你首先必须将其转换为一个 Number
,然后应用 toFixed
方法:
library(dplyr)
library(echarts4r)
mtcars %>%
group_by(cyl, gear) %>%
summarise(mpg = mean(mpg)) %>%
mutate(gear = as.character(gear)) %>%
e_charts(gear) %>%
e_bar(mpg) %>%
e_tooltip(
trigger = "axis",
valueFormatter = htmlwidgets::JS("(value) => 'MPG is ' + Number(value).toFixed(2)")
)
<details>
<summary>英文:</summary>
The issue is that the `value` passed to your formatter is a string. Hence, you have to first convert to a `Number` to apply the `toFixed` method:
library(dplyr)
library(echarts4r)
mtcars |>
group_by(cyl, gear) |>
summarise(mpg = mean(mpg)) |>
mutate(gear = as.character(gear)) |>
e_charts(gear) |>
e_bar(mpg) |>
e_tooltip(
trigger = "axis",
valueFormatter = htmlwidgets::JS("(value) => 'MPG is ' + Number(value).toFixed(2)")
)
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/1uhCu.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论