使用d3.js时可以在文本中插入换行符吗?

Dan*_*ton 45 javascript d3.js

我正在寻找一种方法,使用d3.js在工具提示文本元素中使用换行符.

.text("test" +  "</br>" + "test")
Run Code Online (Sandbox Code Playgroud)

上述和其他类似的努力似乎不起作用.

这里有一个线程似乎回答了它:

https://groups.google.com/forum/?fromgroups=#!topic/d3-js/GgFTf24ltjc

但解决方案不是很清楚.在上述情况下如何使用.html?

.text(.html("test"+""+"test"))

没用?

谢谢

lsu*_*guy 16

由于您已经知道要打破文本的位置,因此可以考虑添加单独的元素.

.append("text")
  .attr("dy", "0em")
  .text("line 1")   // "line 1" or whatever value you want to add here.
Run Code Online (Sandbox Code Playgroud)

然后对于第二行,只需添加一个偏移量

.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.
Run Code Online (Sandbox Code Playgroud)

  • 你能提供任何成功的例子吗?我只是按照你的方式,不能做出任何预期的结果. (4认同)
  • 在我的情况下,只能显示第 1 行,其他都消失了 (4认同)