D3.js,为什么我的文字没有出现

时间:2014-06-09 13:55:15

标签: html text d3.js

我是新来的,所以请和我一起软。 我尝试将文字放在一个圆圈中:

var center = svg.append("circle")
  .attr("r", radius / 3)
  .on("click", zoomOut);

  center.append("text")
  .attr("dy", ".3em")
  .style("text-anchor", "middle")
  .text("My text");

我知道几何形状不能直接包含文本,因此我创建了一个带有circle.append(" text")。 但是,当我看到结果时,我的圈子中没有文字,但在代码中我的文字在这里: http://www.noelshack.com/2014-24-1402322012-sans-titre.png

有人可以帮帮我吗? 感谢。

2 个答案:

答案 0 :(得分:0)

正如本问题中所解释的:d3 add text to circle,解决方法是将文本插入圆圈。然后,您始终可以拥有圆圈和文本元素的容器:

您的代码将类似于:

var center = svg.append("circle")
  .attr("r", radius / 3)
  .on("click", zoomOut);

svg.append("text")
  .attr("dy", ".3em") // change dx and dy in order to center in the circle.
  .style("text-anchor", "middle")
  .text("My text");

答案 1 :(得分:0)

上面的答案是对的。要记住的一个原则是,图像/圆圈和文本应该在同一级别但不能嵌套。

相关问题