我的搜索方法是将工具提示连接到使用forcenetwork之间的节点之间的链接(即边缘)的方法。这些是我发现的最相关的例子:
- 如何将工具提交添加到SankeyNetwork链接:在Sankey Tooltip中显示边缘信息
- 如何添加工具提示forcenetwork节点:为NetworkD3 App实施工具提示
那么,如何将工具提示添加到Forcenetwork链接中?是否可以?我看到Forcenetwork具有一个点击属性,您可以使用HTMLWIDGETS调用JS。不幸的是,ClickAction似乎对节点作用,而不是它们之间的链接。
这是我可重现的示例:
library(networkD3)
library(htmlwidgets)
# Load data
data(MisLinks)
data(MisNodes)
# Make network using sample data
fn <- forceNetwork(
Links = MisLinks, Nodes = MisNodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
Group = "group"
)
# Get the target variable in fn$x$links (an integer id) to show up as a tooltip when user hovers over a link (i.e. edge) in the graph
fnrender <- htmlwidgets::onRender(
fn,
'
function(el, x) {
d3.selectAll(".link").select("title")
.text(function(d) { return d.target; });
}
'
)
# display the result
fnrender
我的目标是让一个字符串变量描述当用户徘徊在它们之间的链接上时,显示了2个节点之间的关系。关于如何前进的任何建议将不胜感激。
您必须'附加'标题...
library(networkD3)
library(htmlwidgets)
# Load data
data(MisLinks)
data(MisNodes)
# Make network using sample data
fn <- forceNetwork(
Links = MisLinks, Nodes = MisNodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
Group = "group"
)
# Get the target variable in fn$x$links (an integer id) to show up as a tooltip when user hovers over a link (i.e. edge) in the graph
fnrender <- htmlwidgets::onRender(
fn,
'
function(el, x) {
d3.selectAll(".link").append("svg:title")
.text(function(d) { return d.source.name + " -> " + d.target.name; })
}
'
)
# display the result
fnrender