私のプロジェクトでは、さまざまなチャートが大量にあり、すべてが D3J で処理されます。グラフの 1 つは、ラベル付きのドーナツ型である必要があります。このチュートリアルに基づいて、このグラフを作成しました。ご覧のとおり (データによって異なります)、ラベル テキストはオーバーレイになります。
私の質問は、ここからのハイチャートに基づいて、以下の例のようにする方法はありますか?
ありがとう。
Solving D3 Label Placement with Constraint Relaxingでラベルを呼び出すときに制約緩和を使用して、ラベルが重複する問題の解決策があると思います。
ここでラベルをざっと見てみましたが、うまくいくようです。これは私の変更されたコード スニペットです。
alpha = 0.5;
spacing = 5;
function relax() {
again = false;
text.each(function (d, i) {
a = this;
da = d3.select(a);
y1 = da.attr("y");
text.each(function (d, j) {
b = this;
// a & b are the same element and don't collide.
if (a == b) return;
db = d3.select(b);
// a & b are on opposite sides of the chart and
// don't collide
if (da.attr("text-anchor") != db.attr("text-anchor")) return;
// Now let's calculate the distance between
// these elements.
y2 = db.attr("y");
deltaY = y1 - y2;
// If spacing is greater than our specified spacing,
// they don't collide.
if (Math.abs(deltaY) > spacing) return;
// If the labels collide, we'll push each
// of the two labels up and down a little bit.
again = true;
sign = deltaY > 0 ? 1 : -1;
adjust = sign * alpha;
da.attr("y",+y1 + adjust);
db.attr("y",+y2 - adjust);
});
});
if(again) {
setTimeout(relax,20)
}
}
relax();
上記のリンク先のチュートリアルの次の手順に従って、ポリラインを更新し、ラベルに従って新しい位置に移動します。幸運を!