gpt4 book ai didi

javascript - 通过单击 d3 选择/取消选择

转载 作者:行者123 更新时间:2023-12-02 14:44:03 25 4
gpt4 key购买 nike

我正在尝试选择/取消选择点击行(链接)。但我已经有了鼠标悬停功能,它应该与鼠标悬停不同,当我选择一条线时,线条的颜色需要更改,并且需要绘制饼图,并且取消选择也应该可以通过单击进行。

我有什么:

    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");
}
});

最佳答案

结果如下:https://jsfiddle.net/xcn35ycm/4/

我将点击函数绑定(bind)到链接。

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");
}

并更新mouseout函数

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

关于javascript - 通过单击 d3 选择/取消选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36757861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com