简体   繁体   English

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

[英]D3.js - Text not showing up in node

I found this d3 code, and I am unable to put text to the nodes. 我找到了此d3代码,无法将文本放入节点。 I have seen some solutions to create a parent class for this but I am getting errors trying them out. 我已经看到一些为此创建父类的解决方案,但是尝试它们时出现错误。 The current code works but doesn't add text :( 当前代码有效,但不添加文本:(

Here's the plnkr link for the working code - 这是工作代码的plnkr链接-

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

Also, this is the code 另外,这是代码

<!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>

You are appending a text to circle as child node. 您要附加一个文本,以圆圈作为子节点。 That will not render. 那不会渲染。 A circle can't have a text. 圈子不能有文字。 There are 2 ways, either define a new enter selection with the same data and append texts ,or create ag element append circle and text and on tick,vupdate transform attribute each tick. 有两种方法,要么用相同的数据定义一个新的enter选择并附加文本,要么创建一个g元素附加圆圈和文本,并在刻度上创建每个刻度的vupdate transform属性。 I did the first one, change the below part of your script and it will render: 我做了第一个,更改脚本的以下部分,它将呈现:

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

You entire html: 您整个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>

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

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