2016-04-21 78 views
0

我試圖選擇/取消選擇單擊的行(鏈接)。但我已經有鼠標懸停功能,它應該不同於鼠標懸停,當我選擇一條線,該線的顏色需要改變,它需要繪製一個餅圖,取消選擇應該可用的點擊。通過點擊d3選擇/取消選擇

我有什麼:

nodeenter.on("mouseover", function(d){ 
console.log(d); 
d3.select(this).attr("fill", "yellow"); 

return tooltip.style("visibility", "visible").text("This node's id is: " + d.id + " and " + "Name: " + d.name);}) 
.on("mousemove", function(){ 

    return tooltip.style("top", 
    (d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px");}) 
.on("mouseout", function(d){ 
    d3.select(this).attr("fill", "rgb(0, 0, " +(d*10) + ")"); 
    return tooltip.style("visibility", "hidden");}); 

link.on("mouseover", function(d){ 
    console.log(d) 
    d3.selectAll('.'+d.id).style('stroke','aqua'); 
    return tooltip.style("visibility", "visible").text("This line's id is: " + d.id + " and " + "Name: " + d.name);}) 
.on("mousemove", function(){return tooltip.style("top", 
    (d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px");}) 
.on("mouseout", function(d){ 
    d3.selectAll('.'+d.id).style('stroke','black'); 
    return tooltip.style("visibility", "hidden");}); 

//to select 
link.on("click", function(d){ 
    if (!d3.select(this).classed("selected")){ 
    d3.select(this).classed("selected", true) 
    d3.select(this).style("stroke","red"); 
    }else{ 
    d3.select(this).classed("selected", false); 
    d3.select(this).style("stroke","black"); 
    } 
}); 
+0

上傳你的代碼的一些平臺像小提琴請。 –

+0

當然!即將發佈 –

+0

@DavidGuan在js小提琴中可以看起來很荒謬。基本上我想要做的是通過單擊它並以相同的方式取消選擇它在兩個節點之間選擇一條線。你能幫我嗎 ? –

回答

2

這裏的結果:https://jsfiddle.net/xcn35ycm/4/

我綁定一個上點擊功能的鏈接。

links.on("click", function(d){ 
    if (!d3.select(this).classed("selected")){ 
    d3.select(this).classed("selected", true) 
    d3.select(this).transition().attr("stroke","red"); 
    }else{ 
    d3.select(this).classed("selected", false); 
    d3.select(this).transition().attr("stroke","black"); 
    } 

,並更新鼠標移開功能

.on("mouseout", function(d){ 
    if(!d3.select(this).classed("selected")){ 
    d3.selectAll('.'+d.id).style('stroke','black'); 
    return tooltip.style("visibility", "hidden"); 
    } 
}); 
+0

對我來說看起來不錯,我會在早上嘗試第一件事!謝謝'! –

+0

它像我一樣工作,當我將鼠標移開時,它變成黑色 –

+0

您是否仍然保持鼠標移出功能,線條再次變黑? – echonax