简体   繁体   中英

Labeling network nodes in D3

I need to draw labels (names) besides nodes with D3. Here's my code:

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

var width = window.innerWidth,
    height = 400;

var color = d3.scale.category20();

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

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)
    .call(d3.behavior.zoom().on("zoom", redraw))
        .append('g');

    function redraw() {
      svg.attr("transform",
          "translate(" + d3.event.translate + ")"
          + " scale(" + d3.event.scale + ")");}

          var drag = force.stop().drag()
          .on("dragstart", function(d) {
            d3.event.sourceEvent.stopPropagation(); 
          });

d3.json("/static/net.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", function(d) { return d.degree; })
      .style("fill", function(d) { return color(d.group); })
      .call(force.drag);

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

  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>

For some reason, labels only appear if I hover over nodes with the mouse, instead of always being drawn.

When I replace:

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

with:

node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name });

No label appear at all, even when hovering.

Is there something obvious I am missing in this code?

The issue is that you're trying to append a text element to a circle.

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

 // node here consists of svg circles.
 node.append("text")
      .text(function(d) { return d.name; })

Try this instead

var node = svg.selectAll(".node")
      .data(graph.nodes)
    .enter().append("g")
      .attr("class", "node")
      .call(force.drag); // moved this here

node.append("circle")
    .attr("r", function(d) { return d.degree; })
    .style("fill", function(d) { return color(d.group); })
    // .call(force.drag);

 // node here is a `g` element so we can append text elements to it.
 node.append("text")
      .text(function(d) { return d.name; })

I also moved the call(force.drag) up to after the .attr("class", "node") line in order to have it apply to all children of a given node element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM