【发布时间】:2023-03-13 03:38:01
【问题描述】:
我是 d3 的新手。我已经为 3 个矩形的出现编写了下面的代码,并让它们有一些鼠标交互。
svg.append("g").selectAll("rect")
.data([1,2,3])
.enter()
.append("rect")
.attr("x",function(d,i){return 600+i*50;})
.attr("y",30)
.attr("width",40)
.attr("height",40)
.attr("fill","blue")
.on("mouseover",function(d) {
d3.select(this).append("text")
.attr("y", "100")
.attr("x", "500")
.attr("class", "hover")
.attr("font-family", "sans-serif")
.attr("font-size", "12px")
.style("fill", "black")
.text(function(d){console.log("mouseover");
return "Text I want";})
})
.on("mouseout", function(d) {
d3.select(this).select("text.hover").remove();
})
.on("click",function(d){console.log(d);
if(d==1)
Pie(array1,array2);
if(d==2)
Pie(array3,array4);
if(d==3)
Pie(array5,array6);
})
.style("opacity",1e-6)
.transition()
.duration(1000)
.style("opacity",0.8);
console.log("mouseover") 出现在控制台上,但浏览器上没有显示任何文本。点击事件适用于正在构建的饼图。谁能发现出了什么问题?提前谢谢你。
【问题讨论】:
标签: javascript d3.js mouseover