我有一個基於大D3可縮放旭日圖「咖啡輪」的例子:旭日圖智能文本換行
在該示例中存在,在一個搜索空間簡單的文本換行代碼標籤,並在第一個空格字符環繞文字:
var textEnter = text.enter().append("text")
.style("fill-opacity", 1)
.style("fill", function(d) {
return brightness(d3.rgb(colour(d))) < 125 ? "#eee" : "#000";
})
.attr("text-anchor", function(d) {
return x(d.x + d.dx/2) > Math.PI ? "end" : "start";
})
.attr("dy", ".2em")
.attr("transform", function(d) {
var multiline = (d.name || "").split(" ").length > 1,
angle = x(d.x + d.dx/2) * 180/Math.PI - 90,
rotate = angle + (multiline ? -.5 : 0);
return "rotate(" + rotate + ")translate(" + (y(d.y) + padding) +
")rotate(" + (angle > 90 ? -180 : 0) + ")";
})
.on("click", click);
textEnter.append("tspan")
.attr("x", 0)
.text(function(d) {
return d.depth ? d.name.split(" ")[0] : "";
});
textEnter.append("tspan")
.attr("x", 0)
.attr("dy", "1em")
.text(function(d) {
return d.depth ? d.name.split(" ")[1] || "" : "";
});
此代碼工作正常,但它是非常基本的,特別是如果整個標籤將在現有sunbust段已安裝在同一行(eiether因爲特定實驗室el太短,或者可用空間足夠大,因爲sunburst被放大)。在密集的sunbursts包裝標籤不必要地導致圖看起來凌亂/混亂。
我想使文字處理更加智能化,與可用空間比較標籤的長度(注意,取決於段可見的深度段改變「寬度」 zoom'ed時)。
此外,如果標籤的可用空間已知,則文本環繞可能更加智能化,例如,如果標籤包含兩個以上的單詞,則代碼可以決定是在第一個空間還是第二個空間打破。
如果有人已經解決了這個問題,並有一些例子,非常感謝,如果它可以共享。或者,如果有人對如何將標籤的長度與變化的可用空間進行比較有任何想法?