echarts4r 中在 tooltip 中使用 valueFormatter。

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

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.75MPG is 15.05

echarts4r 中在 tooltip 中使用 valueFormatter。

不幸的是,工具提示无法正常工作,而且会崩溃/不显示任何内容。
感谢任何帮助。

英文:

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.

echarts4r 中在 tooltip 中使用 valueFormatter。

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)")
  )

echarts4r 中在 tooltip 中使用 valueFormatter。


<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>



huangapple
  • 本文由 发表于 2023年2月13日 23:05:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75437650.html
匿名

发表评论

匿名网友

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

确定