0
這是相當簡單的,以創建在D3一些文本,並讓它換行:D3自動換行與數據驅動寬度限制
var textElement = svg.selectAll('text')
.data(myData)
.enter()
.append('text')
.text(someVeryLongText)
.call(wrapText, allowedWidth);
的wrapText()函數是使用的相當標準的實施例在那裏一個實現(例如http://bl.ocks.org/mbostock/7555321)。
我的問題是,當我想爲每個文本字段依賴於數據的允許寬度,這樣的:
...
.text(someVeryLongText)
.call(wrapText, function(d) {
return d.someCondition ? 100 : 200;
});
是這樣的可能嗎?
棒極了!謝謝:-) – ThisRestlessPilgrim
只要記住,在目前的形式下,如果已經存在tspan元素,它將會中斷。但如果你稍微調整一下,很容易修復。 –