因此,我正在处理 d3 视觉效果中的一个问题,其中我试图创建具有可点击链接的 Node 。我刚刚学习 d3,所以在创建这些链接时遇到了困难。现在到目前为止,我已经在所有 Node 上获得了链接,但我想要实现的本质是为每个 Node 拥有不同的链接。目前,我只是为每个 Node 输入一个网址,但我如何选择一个单独的 Node 并附加一个链接到它。我希望拥有它,以便当用户单击对象 Node 时它会转到另一个页面。
var onode = svg.append('g').selectAll(".outer_node")
.data(data.outer)
.enter().append("g")
.attr("class", "outer_node")
.on("mouseover", mouseover)
.on("mouseout", mouseout)
.on("click", click);
function click (d){
window.open("https://www.google.com", "_self");
}
这使得每个 Node 都有相同的链接,我不知道如何分配不同的链接。有人可以帮忙吗?
最佳答案
假设数据集中存储了 URL,您可以在函数中访问该 URL:
function click (d){
let url = "http://" + d.url; //or whatever your URL field is called in the data
window.open(url, "_self");
}
您提到“数组中的位置”,因此如果 URL 位于单独的数组中,但与数据集对齐,那么您可以使用
function click (d, i){ //is this 0 based index of each item in the selection
let url = urlData[i]
window.open(url, "_self");
}
关于javascript - 在 D3 中添加 Node 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46412369/