这是一个旧问题的后续问题(更改 highcharter sankey 中的标签),其中解决方案显示了如何更改 R highcharter sankey 中的链接工具提示。
我现在还想更改节点的工具提示(现在它只说未定义)。理想情况下,它应该包含每个节点所有链接的总和。
这是一个标准的桑基:
library(purrr)
library(tidyverse)
library(highcharter)
# data
df <- tibble(from = c("Brazil", "Brazil", "Poland", "Spain", "Poland"),
toto = c("Portugal", "Spain", "Portugal", "Brazil", "Poland"),
exports = c(5324523.4523, 232452345.234, 2345234.52345, 3234523.45234, 63452345.2345),
head = "Exports")
# standard sankey
highchart() %>%
hc_add_series(
type = "sankey",
data = df |> pmap(~ list(from = ..1, to = paste0(..2, ' '), weight = ..3, head = ..4))
)
并带有格式化的工具提示(但仅适用于链接,不适用于节点):
# formatted tooltips (but tooltips for nodes dont work)
highchart() %>%
hc_add_series(
type = "sankey",
data = df |> pmap(~ list(from = ..1, to = paste0(..2, ' '), weight = ..3, head = ..4))
) |>
hc_tooltip(formatter = JS("function () { return this.point.head + ': ' +'<b>' +
this.point.from + ' to ' +
this.point.to + '</b><br /> ' +
Highcharts.numberFormat(this.point.weight)
}"
)
)
截图:
我尝试遵循https://jsfiddle.net/BlackLabel/romtnqx5/但我无法使其工作。
我只能更改节点的工具提示,但只有当我只将文本放入其中而不更改链接的工具提示时它才有效:
highchart() %>%
hc_add_series(
type = "sankey",
data = df |> pmap(~ list(from = ..1, to = paste0(..2, ' '), weight = ..3, head = ..4, sum = ..5))
) |>
hc_tooltip(
nodeFormatter = JS("function() {
return 'only text works'
}")
# ,
# formatter = JS("function () { return this.point.head + ': ' +'<b>' +
# this.point.from + ' to ' +
# this.point.to + '</b><br /> ' +
# Highcharts.numberFormat(this.point.weight)
# }"
# )
)
感谢您的任何建议!
一系列
type="sankey"
允许使用 pointFormatter=
和 nodeFormatter=
分别为链接(又名点)和节点指定工具提示格式化程序(请参阅Highcharts Sankey Charts 中的链接和节点的不同工具提示文本):
library(tidyverse)
library(highcharter)
# data
df <- tibble(
from = c("Brazil", "Brazil", "Poland", "Spain", "Poland"),
toto = c("Portugal", "Spain", "Portugal", "Brazil", "Poland"),
exports = c(5324523.4523, 232452345.234, 2345234.52345, 3234523.45234, 63452345.2345),
head = "Exports"
)
# standard sankey
highchart() |>
hc_add_series(
type = "sankey",
data = df |> pmap(~ list(from = ..1, to = paste0(..2, " "), weight = ..3, head = ..4)),
tooltip = list(
nodeFormatter = JS(
"function () {",
"console.log(this);",
"return this.name + ': ' +'<b>' +",
"Highcharts.numberFormat(this.sum)",
"}"
),
pointFormatter = JS(
"function () {",
"return this.head + ': ' +'<b>' +",
"this.from + ' to ' +",
"this.to + '</b><br /> ' +",
"Highcharts.numberFormat(this.weight)",
"}"
)
)
)