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
ても機能しませんでした。
「トップ」クラスは、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();
});