使用 D3 v4 和 D3plus 将文本绕成圆圈

Mia*_*Mia 5 d3.js d3plus

我正在使用 D3 v4,我想用 D3plus 将文本绕成一个圆圈。我尝试了两种方法,但对我来说没有任何作用。

第一种方法

我采用了https://bl.ocks.org/davelandry/a39f0c3fc52804ee859a中的示例。这是我的代码的重要部分:

<head>
  <script type="text/javascript" src="https://d3js.org/d3.v4.min.js"></script>
  <script type="text/javascript" src="https://d3plus.org/js/d3plus.js"></script>
</head>

<body>

  <div id="graphContainer" class="graphContainer">
    <svg width="960" height="600"></svg>
  </div>

  <script>
    d3.json('licenseSmall.json', function(error, graph) {
       if (error) throw error;

       var nodeContainer = svg.append("g")
         .attr("class", "nodeContainer");

       var node = nodeContainer.selectAll(".node")
         .data(graph.nodes)
         .enter().append("g")
         .attr("class", "nodes")
         .call(d3.drag()
           .on("start", dragstarted)
           .on("drag", dragged)
           .on("end", dragended));

       var circle = node.append("circle")
         .attr("r", 20)
         .attr("fill", function(d) { return color(d.color); });

       node.append("text")
         .attr("id", "text")
         .attr("dx", -10)
         .attr("dy", ".35em")
         .attr("test-anchor", "middle")
         .text(function(d) { return getText(d) });

       // Wrap text in a circle.
       d3plus.textwrap()
         .container(d3.select("#text"))
         .draw();

    });
  </script>

</body>
Run Code Online (Sandbox Code Playgroud)

使用这段代码我得到两个错误:

类型错误:d3.scale 未定义

ReferenceError:d3plus 未定义

所以看起来 d3plus 不能与 D3 v4 一起使用...

然后我找到了这篇文章https://groups.google.com/forum/#!topic/d3plus/WN2Ruj3kjPA 并尝试从链接的 Github 项目 d3plus-text 中获取示例(抱歉,我没有足够的声誉来发布链接)。

所以我改变了我的代码:

老的

   <head>
     <script type="text/javascript" src="https://d3js.org/d3.v4.min.js"></script>
     <script type="text/javascript" src="https://d3plus.org/js/d3plus.js"></script>
   </head>
Run Code Online (Sandbox Code Playgroud)

新的

   <head>
     <script type="text/javascript" src="https://d3js.org/d3.v4.min.js"></script>
     <script src="https://d3plus.org/js/d3plus-text.v0.9.full.min.js"></script>
   </head>
Run Code Online (Sandbox Code Playgroud)

在我刚刚替换的脚本中

    // Wrap text in a circle.
    d3plus.textwrap()
      .container(d3.select("#text"))
      .draw();
Run Code Online (Sandbox Code Playgroud)

   new d3plus.TextBox()
      .data(d3.select("#text").data())
      .fontSize(16)
      .width(200)
      .x(function(d, i) { return i * 250; })
      .render();
Run Code Online (Sandbox Code Playgroud)

但实际上什么也没发生。文字看起来和以前一样。

我收到此错误:

类型错误:t 在 d3.v4.min.js:4:27260 处未定义

我不知道如何正确采用这个例子。我没有找到任何其他关于如何使用 d3plus.TextBox() 的示例

我做错了什么?

小智 3

使用d3plus

d3plus.textwrap()
    .container(d3.select("#your-div"))
    .shape('circle')
    .width(290)
    .height(75)
    .resize(true)
    .draw();   
Run Code Online (Sandbox Code Playgroud)