How can I append text to and render that text from a line in a force directed graph in D3.js?

前端 未结 1 689
青春惊慌失措
青春惊慌失措 2021-01-16 03:29

I\'m trying to learn how to use the force.layout features in D3.js and have been building a \"Force Directed Radial Graph\".

I\'ve been able to successfully append a

1条回答
  •  走了就别回头了
    2021-01-16 03:44

    The sample already shows how to add text to the edges. Here is the relevant piece of code:

          // Append text to Link edges
      var linkText = svgCanvas.selectAll(".gLink")
          .data(force.links())
        .append("text")
      .attr("font-family", "Arial, Helvetica, sans-serif")
      .attr("x", function(d) {
          if (d.target.x > d.source.x) { return (d.source.x + (d.target.x - d.source.x)/2); }
          else { return (d.target.x + (d.source.x - d.target.x)/2); }
      })
          .attr("y", function(d) {
          if (d.target.y > d.source.y) { return (d.source.y + (d.target.y - d.source.y)/2); }
          else { return (d.target.y + (d.source.y - d.target.y)/2); }
      })
      .attr("fill", "Maroon")
          .style("font", "normal 12px Arial")
          .attr("dy", ".35em")
          .text(function(d) { return d.linkName; });
    

    0 讨论(0)
提交回复
热议问题