使用 d3.js 时可以在文本中插入换行符吗? [英] Can you insert a line break in text when using d3.js?
问题描述
我正在寻找一种使用 d3.js 在工具提示文本元素中使用换行符的方法.
I'm looking for a way to use a line break within a tooltip text element using d3.js.
.text("test" + "</br>" + "test")
上述以及其他类似的努力似乎不起作用.
The above, and other similar efforts, don't seem to work.
这里有一个线程似乎可以回答这个问题:
There's a thread here that seems to answer it:
https://groups.google.com/forum/?fromgroups=#!topic/d3-js/GgFTf24ltjc
但解决方案不是很清楚..html 在上述情况下如何使用?
but the solution isn't very clear. How would .html be used in the above situation?
.text(.html("test" + "" + "test"))
.text(.html("test" + "" + "test"))
没用?
谢谢
推荐答案
既然您已经知道要在何处中断文本,您可以考虑只添加单独的元素.
Since you already know where you want to break the text, you could consider just adding separate elements.
.append("text")
.attr("dy", "0em")
.text("line 1") // "line 1" or whatever value you want to add here.
然后对于第二行,只需添加一个偏移量
And then for the second line, just add an offset
.append("text")
.attr("dy", "1em") // you can vary how far apart it shows up
.text("line 2") // "line 2" or whatever value you want to add here.
这篇关于使用 d3.js 时可以在文本中插入换行符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!