56

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

.text("test" +  "</br>" + "test")

上述以及其他类似的努力似乎不起作用。

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

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

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

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

没用?

谢谢

4

2 回答 2

21

由于您已经知道要在哪里打断文本,您可以考虑只添加单独的元素。

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

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

.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.
于 2016-07-25T00:23:17.910 回答
1

最好对第 1 行和第 2 行使用 'tspan' 属性 dy = 0em 和 dy = 1.2em

于 2017-02-01T06:32:39.230 回答