如何使用D3库将SVG元素有效地置于z-order的顶部?
我的具体情况是,饼图在鼠标悬停在给定片段上时通过添加stroke
到path
来突出显示。下面是生成我的图表的代码块:
svg.selectAll("path")
.data(d)
.enter().append("path")
.attr("d", arc)
.attr("class", "arc")
.attr("fill", function(d) { return color(d.name); })
.attr("stroke", "#fff")
.attr("stroke-width", 0)
.on("mouseover", function(d) {
d3.select(this)
.attr("stroke-width", 2)
.classed("top", true);
//.style("z-index", 1);
})
.on("mouseout", function(d) {
d3.select(this)
.attr("stroke-width", 0)
.classed("top", false);
//.style("z-index", -1);
});
我尝试了几个选项,但是到目前为止都没有运气。尝试使用style("z-index")
并调用classed
都没有起作用。
"top"类在我的CSS中定义如下:
.top {
fill: red;
z-index: 100;
}
fill
语句用来确保我知道它是否正确地开启/关闭。它是正确的。
我听说使用sort
是一种选择,但我不清楚如何将“选定”的元素置于顶部。
更新:
我通过以下代码解决了我的特定情况,在mouseover
事件上添加了一个新的弧形到SVG中以显示高亮。
svg.selectAll("path")
.data(d)
.enter().append("path")
.attr("d", arc)
.attr("class", "arc")
.style("fill", function(d) { return color(d.name); })
.style("stroke", "#fff")
.style("stroke-width", 0)
.on("mouseover", function(d) {
svg.append("path")
.attr("d", d3.select(this).attr("d"))
.attr("id", "arcSelection")
.style("fill", "none")
.style("stroke", "#fff")
.style("stroke-width", 2);
})
.on("mouseout", function(d) {
d3.select("#arcSelection").remove();
});