admin管理员组文章数量:1196423
I want to be able to return html from the text function like this:
textEnter.append("tspan")
.attr("x", 0)
.text(function(d,i) {
return 'some text' + '<br/>' + d.someProp;
})
Tried to use <br>
, but didnot work. How do I achieve this?
I want to be able to return html from the text function like this:
textEnter.append("tspan")
.attr("x", 0)
.text(function(d,i) {
return 'some text' + '<br/>' + d.someProp;
})
Tried to use <br>
, but didnot work. How do I achieve this?
1 Answer
Reset to default 23EDITED ANSWER
Just noticed that you're working with a tspan here. Unfortunately you can't insert line breaks into svg text elements. Multiline text with SVG requires breaking up the text yourself and then laying it out by setting the dy
attribute. D3 makes the laying out process pretty straight forward, but it still takes extra work.
More info in the intro paragraph here: http://www.w3.org/TR/SVG/text.html
OLD ANSWER (applies if using html elements, not svg)
D3 has a separate method for this: the html()
method, which works just like text()
but unescaped. More info here. So, easily enough, you just need:
textEnter.append("tspan")
.attr("x", 0)
.html(function(d,i) {
return 'some text' + '<br/>' + d.someProp;
})
本文标签: javascriptHow to return html in d3 text functionStack Overflow
版权声明:本文标题:javascript - How to return html in d3 text function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738501606a2090301.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
.html
or.innerHTML
instead of.text()
? – BBog Commented May 20, 2013 at 20:04