D3.js - 文本未显示在节点中

时间:2018-04-27 06:58:47

标签: javascript d3.js svg

我找到了这个d3代码,我无法将文本放到节点上。我已经看到了一些为此创建父类的解决方案,但是我在尝试它们时遇到错误。当前代码有效,但不添加文本:(

这是工作代码的plnkr链接 -

http://plnkr.co/edit/dqAaEhJnnK4i2nBsTaNu?p=preview

此外,这是代码

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.node {
  stroke: #fff;
  stroke-width: 1.5px;
}

.link {
  stroke: #999;
  stroke-opacity: .6;
}

</style>
<body>
<button id="clusterButton" type="button">Cluster</button>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script type="text/javascript" src="../netClustering.js">   </script>
<!-- <script type="text/javascript" src="groupInABox.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 gb = GroupInABox(force, "");

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



d3.json("miserables.json", function(error, graph) {


  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", 5)
      .call(force.drag);

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

  node.append("text")
      .attr("dx", 12)
      .attr("dy", "0.35em")
      .text(function(d){ return d.name; });
//      .call(force.drag);
//      .attr("font-size", function(d){ return d.influence*1.5>9? d.influence*1.5: 9; })


  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; });
  });

  d3.select("#clusterButton").on("click", function () {
    netClustering.cluster(graph.nodes, graph.links);

    svg.selectAll(".node").transition().duration(500).style("fill", function(d) { return color(d.cluster); });
  });
});

</script>

1 个答案:

答案 0 :(得分:2)

您要将文本附加到圈子作为子节点。这不会呈现。圆圈不能有文字。有两种方法,或者用相同的数据定义一个新的输入选择并附加文本,或者创建一个g元素追加圆圈和文本,并在每个刻度上勾选,vupdate变换属性。我做了第一个,更改脚本的下面部分,它将呈现:

//  node.append("title")
//      .text(function(d) { return d.name; });
  var texts  =svg.selectAll(".texts")
      .data(graph.nodes)
      .enter()
      .append("text")
      .attr("dx", 12)
      .attr("dy", "0.35em")
      .text(function(d){ return d.name; });
//      .call(force.drag);
//      .attr("font-size", function(d){ return d.influence*1.5>9? d.influence*1.5: 9; })


  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; });

    texts.attr("x", function(d) { return d.x; })
        .attr("y", function(d) { return d.y; });
  });

你整个html:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.node {
  stroke: #fff;
  stroke-width: 1.5px;
}

.link {
  stroke: #999;
  stroke-opacity: .6;
}

</style>
<body>
<button id="clusterButton" type="button">Cluster</button>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script type="text/javascript" src="netClustering.js">   </script>
<!-- <script type="text/javascript" src="groupInABox.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 gb = GroupInABox(force, "");

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



d3.json("miserables.json", function(error, graph) {


  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", 5)
      .call(force.drag);

 var texts  =svg.selectAll(".texts")
      .data(graph.nodes)
      .enter()
      .append("text")
      .attr("dx", 12)
      .attr("dy", "0.35em")
      .text(function(d){ return d.name; });
//      .call(force.drag);
//      .attr("font-size", function(d){ return d.influence*1.5>9? d.influence*1.5: 9; })


  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; });

    texts.attr("x", function(d) { return d.x; })
        .attr("y", function(d) { return d.y; });
  });

  d3.select("#clusterButton").on("click", function () {
    netClustering.cluster(graph.nodes, graph.links);

    svg.selectAll(".node").transition().duration(500).style("fill", function(d) { return color(d.cluster); });
  });
});

</script>