How to add text to d3.js nodes?

不羁岁月 提交于 2019-12-24 00:26:54

问题


Making first steps with d3.js I have taken sample code from this sample page and try to change it the way I need it.

Actually I want to add TEXT to the colored nodes. They already have a title property set, but I can't manage to add not-tooltip text.

Reference and introduction documents did not help on this.

Here is the code, my fortuneless approach is marked:

<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var width = 960,
    height = 500;

var color = d3.scale.category20();

var force = d3.layout.force()
    .charge(-120)
    .linkDistance(30)
    .size([width, height]);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

    //d3.select("body").transition()
    //.style("background-color", "black");

d3.json("miserables1.json", function(error, graph) {
  if (error) throw error;

  force
      .nodes(graph.nodes)
      .links(graph.links)
      .start();

  var link = svg.selectAll(".link")
      .data(graph.links)
      .enter().append("line")
      .attr("class", "link")
      .style("stroke-width", function(d) { return Math.sqrt(d.value); });     

  var node = svg.selectAll(".node")
      .data(graph.nodes)
    .enter().append("circle")
      .attr("class", "node")
      //.attr("r", 20)
      .attr("r",  function(d) { return d.radius; }) 
      .style("fill", function(d) { return color(d.group); })
      .call(force.drag);

  node.append("title")
      .text(function(d) { return d.name; });

// -----> my approach to add text to the nodes:
  node.insert("div", ":first-child")
     .append("text")
     .style("fill", "#0000ff")
     .attr("width", "10")
     .attr("height", "10")
     .text(function(d) { return d.name; });
// -----> end of fortuneless approach.

  force.on("tick", function() {
    link.attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });

    node.attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });
  });
});

</script>

回答1:


If you look at the nodes:

var node = svg.selectAll(".node")
  .data(graph.nodes)
.enter().append("circle")

And look at what you did after that:

node.insert("div", ":first-child")
 .append("text")

You'll see the problem: You are trying to append texts to circles, and this doesn't work. Besides that, you can not append <div> or any other HTML tag to a SVG (foreignObject is a different story).

So, this is a solution:

var myText = svg.selectAll(".mytext")
                .data(graph.nodes)
                .enter()
                .append("text")
                //the rest of your code

Here is the working plunker: https://plnkr.co/edit/UwQfPscQiOg87IEMYtET?p=preview



来源:https://stackoverflow.com/questions/38146965/how-to-add-text-to-d3-js-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!