【发布时间】:2018-02-12 23:29:50
【问题描述】:
我正在使用M.Bostock's wrap function 包装文本,但找不到证明它的方法。
这在 d3 中是否可行? 如果没有,有没有办法“模仿”这种文本处置?
编辑:感谢 Logikos 的建议,我从 M.Bostock 中找到了 this example,将 foreignObject 放入了 svg。
这里是sn-p:
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500);
svg.append("foreignObject")
.attr("width", 480)
.attr("height", 500)
.append("xhtml:body")
.style("font", "14px 'Helvetica Neue'")
.html("<h1>An HTML Foreign Object in SVG</h1><p>Veeery long text");
然后你只需要在 CSS 中添加:
body {text-align: justify;
text-align-last: start;
}
【问题讨论】:
-
只是一个警告:
foreignObject不适用于所有浏览器。 -
另外,我在标题中添加了“SVG”:没有 “D3 文本” 这样的东西。这里的问题是 SVG,而不是 D3。
-
@GerardoFurtado 是正确的,但是支持还不错,-caniuse.com/#search=foreignObject 在大多数情况下,除了 Opera mini,如果您参考我发布给您的链接,它将适用于您想要做的事情将会了解到过滤效果有一些限制。
-
@Logikos 它不会为任何 IE(11 及以下)分叉。而且(不幸的是)很多人使用 Internet Explorer。
标签: d3.js svg text justify text-justify