[英]Adding Links to Nodes in D3
因此,我正在處理d3視覺中的一個問題,在該問題中,我正在嘗試創建具有可點擊鏈接的節點。 我只是在學習d3,所以在創建這些鏈接時遇到了麻煩。 到目前為止,到目前為止,我已經在所有節點上獲得了鏈接,但實際上我想實現的是為每個節點設置不同的鏈接。 目前,我只是為每個節點輸入一個url,但是我將如何選擇一個節點並為其附加鏈接。 我想擁有它,以便當用戶單擊對象節點時它會轉到另一個頁面。
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");
}
這使得每個節點都具有相同的確切鏈接,我不知道如何分配不同的鏈接。 有人可以幫忙嗎?
假設您有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");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.