2016-04-21 75 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