javascript - D3 v4 同时移动圆圈和文本

标签 javascript d3.js

作为起点,我使用 this code使用 D3 v4 创建有向图。

我想在每个圆圈上附加一段文字。我最初的想法是为数据中的每个节点创建一个节点元素,并向它们添加一个圆圈和文本元素(将来可能会添加更多内容)。

但是,圆圈​​和文本似乎根本没有呈现,即使我更改了 tick 函数来更新它们的位置,如下所示:

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

我的实现可以在 this jsfiddle 中找到及以下:

// Create somewhere to put the force directed graph
var svg = d3.select("svg"),
    width = +svg.attr("width"),
    height = +svg.attr("height");

var radius = 20;

// DATA
var nodes_data =  [
    {"id": "Lillian", "sex": "F"},
    {"id": "Gordon", "sex": "M"},
    {"id": "Sylvester", "sex": "M"},
    {"id": "Mary", "sex": "F"},
    {"id": "Helen", "sex": "F"},
    {"id": "Jamie", "sex": "M"},
    {"id": "Jessie", "sex": "F"},
    {"id": "Ashton", "sex": "M"},
    {"id": "Duncan", "sex": "M"},
    {"id": "Evette", "sex": "F"},
    {"id": "Mauer", "sex": "M"},
    {"id": "Fray", "sex": "F"},
    {"id": "Duke", "sex": "M"},
    {"id": "Baron", "sex": "M"},
    {"id": "Infante", "sex": "M"},
    {"id": "Percy", "sex": "M"},
    {"id": "Cynthia", "sex": "F"},
    {"id": "Feyton", "sex": "M"},
    {"id": "Lesley", "sex": "F"},
    {"id": "Yvette", "sex": "F"},
    {"id": "Maria", "sex": "F"},
    {"id": "Lexy", "sex": "F"},
    {"id": "Peter", "sex": "M"},
    {"id": "Ashley", "sex": "F"},
    {"id": "Finkler", "sex": "M"},
    {"id": "Damo", "sex": "M"},
    {"id": "Imogen", "sex": "F"}];
var links_data = [
    {"source": "Sylvester", "target": "Gordon", "type":"A" },
    {"source": "Sylvester", "target": "Lillian", "type":"A" },
    {"source": "Sylvester", "target": "Mary", "type":"A"},
    {"source": "Sylvester", "target": "Jamie", "type":"A"},
    {"source": "Sylvester", "target": "Jessie", "type":"A"},
    {"source": "Sylvester", "target": "Helen", "type":"A"},
    {"source": "Helen", "target": "Gordon", "type":"A"},
    {"source": "Mary", "target": "Lillian", "type":"A"},
    {"source": "Ashton", "target": "Mary", "type":"A"},
    {"source": "Duncan", "target": "Jamie", "type":"A"},
    {"source": "Gordon", "target": "Jessie", "type":"A"},
    {"source": "Sylvester", "target": "Fray", "type":"E"},
    {"source": "Fray", "target": "Mauer", "type":"A"},
    {"source": "Fray", "target": "Cynthia", "type":"A"},
    {"source": "Fray", "target": "Percy", "type":"A"},
    {"source": "Percy", "target": "Cynthia", "type":"A"},
    {"source": "Infante", "target": "Duke", "type":"A"},
    {"source": "Duke", "target": "Gordon", "type":"A"},
    {"source": "Duke", "target": "Sylvester", "type":"A"},
    {"source": "Baron", "target": "Duke", "type":"A"},
    {"source": "Baron", "target": "Sylvester", "type":"E"},
    {"source": "Evette", "target": "Sylvester", "type":"E"},
    {"source": "Cynthia", "target": "Sylvester", "type":"E"},
    {"source": "Cynthia", "target": "Jamie", "type":"E"},
    {"source": "Mauer", "target": "Jessie", "type":"E"},
    {"source": "Duke", "target": "Lexy", "type":"A"},
    {"source": "Feyton", "target": "Lexy", "type":"A"},
    {"source": "Maria", "target": "Feyton", "type":"A"},
    {"source": "Baron", "target": "Yvette", "type":"E"},
    {"source": "Evette", "target": "Maria", "type":"E"},
    {"source": "Cynthia", "target": "Yvette", "type":"E"},
    {"source": "Maria", "target": "Jamie", "type":"E"},
    {"source": "Maria", "target": "Lesley", "type":"E"},
    {"source": "Ashley", "target": "Damo", "type":"A"},
    {"source": "Damo", "target": "Lexy", "type":"A"},
    {"source": "Maria", "target": "Feyton", "type":"A"},
    {"source": "Finkler", "target": "Ashley", "type":"E"},
    {"source": "Sylvester", "target": "Maria", "type":"E"},
    {"source": "Peter", "target": "Finkler", "type":"E"},
    {"source": "Ashley", "target": "Gordon", "type":"E"},
    {"source": "Maria", "target": "Imogen", "type":"E"}];

// Set up the simulation and add forces
var simulation = d3.forceSimulation()
                    .nodes(nodes_data);

var link_force =  d3.forceLink(links_data)
                        .id(function(d) { return d.id; });

var charge_force = d3.forceManyBody()
    .strength(-200);

var center_force = d3.forceCenter(width / 2, height / 2);

simulation
    .force("charge_force", charge_force)
    .force("center_force", center_force)
    .force("links",link_force);


// Add tick instructions:
simulation.on("tick", tickActions );

// Add encompassing group for the zoom
var g = svg.append("g")
    .attr("class", "everything");

// Draw lines for the links
var link = g.append("g")
        .attr("class", "links")
            .selectAll("line")
            .data(links_data)
            .enter()
                .append("line")
                    .attr("stroke-width", 2)
                    .style("stroke", linkColour);

// Draw circles and texts for the nodes
var nodes = g.append("g")
        .attr("class", "nodes");

var node = nodes.selectAll("node")
        .data(nodes_data)
        .enter()
            .append("node");

var circle = node.append("circle")
        .attr("r", radius)
        .attr("fill", circleColour)
        .attr("cx", function(d){return d.x})
        .attr("cy", function(d){return d.y});

var text = node.append("text")
        .text("asdasdasdas")
        .attr("cx", function(d){return d.x})
        .attr("cy", function(d){return d.y});

// Add drag capabilities
var drag_handler = d3.drag()
    .on("start", drag_start)
    .on("drag", drag_drag)
    .on("end", drag_end);

drag_handler(node);

// Add zoom capabilities
var zoom_handler = d3.zoom()
    .on("zoom", zoom_actions);

zoom_handler(svg);

/** Functions **/

function circleColour(d){
    if(d.sex == "M"){
        return "blue";
    } else {
        return "pink";
    }
}

// Function to choose the line colour and thickness
function linkColour(d){
    return "black";
}

// Drag functions
function drag_start(d) {
 if (!d3.event.active) simulation.alphaTarget(0.3).restart();
    d.fx = d.x;
    d.fy = d.y;
}

// Make sure you can't drag the circle outside the box
function drag_drag(d) {
  d.fx = d3.event.x;
  d.fy = d3.event.y;
}

function drag_end(d) {
  if (!d3.event.active) simulation.alphaTarget(0);
  d.fx = null;
  d.fy = null;
}

// Zoom functions
function zoom_actions(){
    g.attr("transform", d3.event.transform)
}

function tickActions() {
    // update node positions each tick of the simulation
    node
        .attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });

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

最佳答案

您的代码中存在三个问题:

  1. 没有名为 <node> 的 SVG 元素.因此,而不是...

    .append("node");
    

    ……应该是:

    .append("g");
    
  2. 文本元素没有cxcy属性。

  3. 而不是更改 cxcynode , 它没有(因为它是一个 <g> 元素),你必须翻译它:

    node.attr("transform", function(d) {
        return "translate(" + d.x + "," + d.y + ")"
    });
    

总而言之,这是您更新后的代码:

.links line {
  stroke: #999;
  stroke-opacity: 0.6;
}

.nodes circle {
  stroke: black;
  stroke-width: 0px;
}
<svg width="800" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
  // Create somewhere to put the force directed graph
  var svg = d3.select("svg"),
    width = +svg.attr("width"),
    height = +svg.attr("height");

  var radius = 20;

  // DATA
  var nodes_data = [{
    "id": "Lillian",
    "sex": "F"
  }, {
    "id": "Gordon",
    "sex": "M"
  }, {
    "id": "Sylvester",
    "sex": "M"
  }, {
    "id": "Mary",
    "sex": "F"
  }, {
    "id": "Helen",
    "sex": "F"
  }, {
    "id": "Jamie",
    "sex": "M"
  }, {
    "id": "Jessie",
    "sex": "F"
  }, {
    "id": "Ashton",
    "sex": "M"
  }, {
    "id": "Duncan",
    "sex": "M"
  }, {
    "id": "Evette",
    "sex": "F"
  }, {
    "id": "Mauer",
    "sex": "M"
  }, {
    "id": "Fray",
    "sex": "F"
  }, {
    "id": "Duke",
    "sex": "M"
  }, {
    "id": "Baron",
    "sex": "M"
  }, {
    "id": "Infante",
    "sex": "M"
  }, {
    "id": "Percy",
    "sex": "M"
  }, {
    "id": "Cynthia",
    "sex": "F"
  }, {
    "id": "Feyton",
    "sex": "M"
  }, {
    "id": "Lesley",
    "sex": "F"
  }, {
    "id": "Yvette",
    "sex": "F"
  }, {
    "id": "Maria",
    "sex": "F"
  }, {
    "id": "Lexy",
    "sex": "F"
  }, {
    "id": "Peter",
    "sex": "M"
  }, {
    "id": "Ashley",
    "sex": "F"
  }, {
    "id": "Finkler",
    "sex": "M"
  }, {
    "id": "Damo",
    "sex": "M"
  }, {
    "id": "Imogen",
    "sex": "F"
  }];
  var links_data = [{
    "source": "Sylvester",
    "target": "Gordon",
    "type": "A"
  }, {
    "source": "Sylvester",
    "target": "Lillian",
    "type": "A"
  }, {
    "source": "Sylvester",
    "target": "Mary",
    "type": "A"
  }, {
    "source": "Sylvester",
    "target": "Jamie",
    "type": "A"
  }, {
    "source": "Sylvester",
    "target": "Jessie",
    "type": "A"
  }, {
    "source": "Sylvester",
    "target": "Helen",
    "type": "A"
  }, {
    "source": "Helen",
    "target": "Gordon",
    "type": "A"
  }, {
    "source": "Mary",
    "target": "Lillian",
    "type": "A"
  }, {
    "source": "Ashton",
    "target": "Mary",
    "type": "A"
  }, {
    "source": "Duncan",
    "target": "Jamie",
    "type": "A"
  }, {
    "source": "Gordon",
    "target": "Jessie",
    "type": "A"
  }, {
    "source": "Sylvester",
    "target": "Fray",
    "type": "E"
  }, {
    "source": "Fray",
    "target": "Mauer",
    "type": "A"
  }, {
    "source": "Fray",
    "target": "Cynthia",
    "type": "A"
  }, {
    "source": "Fray",
    "target": "Percy",
    "type": "A"
  }, {
    "source": "Percy",
    "target": "Cynthia",
    "type": "A"
  }, {
    "source": "Infante",
    "target": "Duke",
    "type": "A"
  }, {
    "source": "Duke",
    "target": "Gordon",
    "type": "A"
  }, {
    "source": "Duke",
    "target": "Sylvester",
    "type": "A"
  }, {
    "source": "Baron",
    "target": "Duke",
    "type": "A"
  }, {
    "source": "Baron",
    "target": "Sylvester",
    "type": "E"
  }, {
    "source": "Evette",
    "target": "Sylvester",
    "type": "E"
  }, {
    "source": "Cynthia",
    "target": "Sylvester",
    "type": "E"
  }, {
    "source": "Cynthia",
    "target": "Jamie",
    "type": "E"
  }, {
    "source": "Mauer",
    "target": "Jessie",
    "type": "E"
  }, {
    "source": "Duke",
    "target": "Lexy",
    "type": "A"
  }, {
    "source": "Feyton",
    "target": "Lexy",
    "type": "A"
  }, {
    "source": "Maria",
    "target": "Feyton",
    "type": "A"
  }, {
    "source": "Baron",
    "target": "Yvette",
    "type": "E"
  }, {
    "source": "Evette",
    "target": "Maria",
    "type": "E"
  }, {
    "source": "Cynthia",
    "target": "Yvette",
    "type": "E"
  }, {
    "source": "Maria",
    "target": "Jamie",
    "type": "E"
  }, {
    "source": "Maria",
    "target": "Lesley",
    "type": "E"
  }, {
    "source": "Ashley",
    "target": "Damo",
    "type": "A"
  }, {
    "source": "Damo",
    "target": "Lexy",
    "type": "A"
  }, {
    "source": "Maria",
    "target": "Feyton",
    "type": "A"
  }, {
    "source": "Finkler",
    "target": "Ashley",
    "type": "E"
  }, {
    "source": "Sylvester",
    "target": "Maria",
    "type": "E"
  }, {
    "source": "Peter",
    "target": "Finkler",
    "type": "E"
  }, {
    "source": "Ashley",
    "target": "Gordon",
    "type": "E"
  }, {
    "source": "Maria",
    "target": "Imogen",
    "type": "E"
  }];

  // Set up the simulation and add forces
  var simulation = d3.forceSimulation()
    .nodes(nodes_data);

  var link_force = d3.forceLink(links_data)
    .id(function(d) {
      return d.id;
    });

  var charge_force = d3.forceManyBody()
    .strength(-200);

  var center_force = d3.forceCenter(width / 2, height / 2);

  simulation
    .force("charge_force", charge_force)
    .force("center_force", center_force)
    .force("links", link_force);


  // Add tick instructions:
  simulation.on("tick", tickActions);

  // Add encompassing group for the zoom
  var g = svg.append("g")
    .attr("class", "everything");

  // Draw lines for the links
  var link = g.append("g")
    .attr("class", "links")
    .selectAll("line")
    .data(links_data)
    .enter()
    .append("line")
    .attr("stroke-width", 2)
    .style("stroke", linkColour);

  // Draw circles and texts for the nodes
  var nodes = g.append("g")
    .attr("class", "nodes");

  var node = nodes.selectAll("node")
    .data(nodes_data)
    .enter()
    .append("g");

  var circle = node.append("circle")
    .attr("r", radius)
    .attr("fill", circleColour);

  var text = node.append("text")
    .text("asdasdasdas")
    .attr("y", -22)
    .attr("text-anchor", "middle");

  // Add drag capabilities
  var drag_handler = d3.drag()
    .on("start", drag_start)
    .on("drag", drag_drag)
    .on("end", drag_end);

  drag_handler(node);

  // Add zoom capabilities
  var zoom_handler = d3.zoom()
    .on("zoom", zoom_actions);

  zoom_handler(svg);

  /** Functions **/

  function circleColour(d) {
    if (d.sex == "M") {
      return "blue";
    } else {
      return "pink";
    }
  }

  // Function to choose the line colour and thickness
  function linkColour(d) {
    return "black";
  }

  // Drag functions
  function drag_start(d) {
    if (!d3.event.active) simulation.alphaTarget(0.3).restart();
    d.fx = d.x;
    d.fy = d.y;
  }

  // Make sure you can't drag the circle outside the box
  function drag_drag(d) {
    d.fx = d3.event.x;
    d.fy = d3.event.y;
  }

  function drag_end(d) {
    if (!d3.event.active) simulation.alphaTarget(0);
    d.fx = null;
    d.fy = null;
  }

  // Zoom functions
  function zoom_actions() {
    g.attr("transform", d3.event.transform)
  }

  function tickActions() {
    // update node positions each tick of the simulation
    node.attr("transform", function(d) {
      return "translate(" + d.x + "," + d.y + ")"
    });

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

</script>

关于javascript - D3 v4 同时移动圆圈和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44498954/

相关文章:

javascript - 平均堆栈 : How to update a function's result to the database?

javascript - MongoDB 集群中的 IP 白名单 Firebase 云函数

javascript - 为什么这些标签不可见

javascript - 如何使用 d3.js 沿 GeoJSON 路径为对象设置动画?

javascript - 如何在D3图中添加点?

javascript - 解析一个 JSON 字符串并显示它

javascript - 在不滚动页面的情况下将带有 javascript 的哈希添加到 url?

javascript - 在单独的函数中使用 Node 中回调的结果

javascript - 通过在 Google map 上拖放标记来获取用户地址

Javascript运行密集型可视化操作而不卡住浏览器