2012-09-25 48 views
1

this example中,如何讓每個節點的文本成爲可點擊的鏈接?d3.js強制有向圖中的鏈接

我想類似這樣的代碼的東西,但值不點擊:

var links = text.append("a").attr("xlink:href", "http://www.google.com/"); 

// A copy of the text with a thick white stroke for legibility. 
links.append("svg:text") 
    .attr("x", 8) 
    .attr("y", ".31em") 
    .attr("class", "shadow") 
    .text(function(d) { return d.name; }); 

links.append("svg:text") 
    .attr("x", 8) 
    .attr("y", ".31em") 
    .text(function(d) { return d.name; }); 

編輯/解決方案:原來的CSS有這個attriubte:指針的事件:無; 我不得不刪除它,然後按照以利亞的建議使用它。

回答

2

不要使用鏈接,將其放置並直接附加到您的文本<g>,它應該工作。

text.append("svg:text") 
     .attr("x", 8) 
     .attr("y", ".31em") 
     .attr("class", "shadow") 
     .text(function(d) { return d.name; }) 
     .on("click", function() {yourFunction(yourVar)}) 
     .on("mouseover", function() {yourFunction2(yourVar)}) 
     .on("mouseout", function() {yourFunction3(yourVar)}) 
; 

另外,如果你想通過綁定的數據,你會是這樣做的:

.on("click", function(d) {yourFunction(d.yourVar)} 

而如果你想通過實際d對象,你可以像下面這樣做:

.on("click", yourFunction} 

在這種情況下,yourFunction(d,i)然後可以從綁定數據中引用d.whatever。

+0

謝謝,但我不能得到它的工作。這裏是我使用的代碼https://gist.github.com/3784067,這裏是活動頁面http://bl.ocks.org/3784067 –

+0

仍然不起作用。我只是將2行事件附件添加到原始代碼中。要點/演示:https://gist.github.com/3785177 http://bl.ocks.org/3785177 –

相關問題