【发布时间】:2012-11-15 17:16:40
【问题描述】:
使用 D3 库将 SVG 元素置于 z 顺序顶部的有效方法是什么?
我的具体方案是一个饼图,当鼠标悬停在给定的一块时,它会突出显示(通过在path 中添加stroke)。生成我的图表的代码块如下:
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();
});
【问题讨论】:
标签: javascript svg d3.js