简体   繁体   English

在D3中标记网络节点

[英]Labeling network nodes in D3

I need to draw labels (names) besides nodes with D3. 除了带有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. 我还将call(force.drag)移到.attr("class", "node")行之后,以便将其应用于给定node元素的所有子级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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