使用 D3 库将 SVG 元素置于 z 顺序顶部的有效方法是什么?
我的特定情况是一个饼图其中突出(通过添加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
声明是为了确保我知道它正在正确打开/关闭。它是。
我听说 usingsort
是一个选项,但我不清楚如何实现将“选定”元素带到顶部。
更新:
我使用以下代码修复了我的特殊情况,该代码在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();
});