【问题标题】:Add different images for nodes in d3 v4 network graph为 d3 v4 网络图中的节点添加不同的图像
【发布时间】:2018-02-07 02:05:28
【问题描述】:

我知道有几个主题讨论了这个问题,但没有找到对我有用的结果。我正在尝试修改经典的 d3 网络图 Les miserables 示例(此处的 d3v4 版本:https://bl.ocks.org/mbostock/4062045)为不同的节点添加不同的图像 - 文件的相对路径作为节点属性之一给出,例如。

 {"id": "Valjean", "group": 1, img: "images/male.png"},

我想要达到的效果与此类似:https://bl.ocks.org/mbostock/950642 但在 d3v4 中制作,并且不同节点的图像不同。

我发现的所有示例(还有 this 承诺代码 sn-p,不幸的是,这对我不起作用)指向类似的方法,看起来或多或少像这样(在 d3 v4 和 v3 中):

node.append("image")
      .attr("xlink:href", function(d) { return d.img })
      .attr("x", -8)
      .attr("y", -8)
      .attr("width", 16)
      .attr("height", 16);

但是,尽管花费了几个小时,我还是无法让它工作。有什么想法吗?

【问题讨论】:

    标签: image d3.js


    【解决方案1】:

    这是一个快速示例,它将您的 v4 示例与基于您的 v3 示例的图像相结合:

    <!DOCTYPE html>
    <meta charset="utf-8">
    <style>
      .links line {
        stroke: #999;
        stroke-opacity: 0.6;
      }
      
      .nodes circle {
        stroke: #fff;
        stroke-width: 1.5px;
      }
    </style>
    <svg width="960" height="600"></svg>
    <script src="https://d3js.org/d3.v4.min.js"></script>
    <script>
      var svg = d3.select("svg"),
        width = +svg.attr("width"),
        height = +svg.attr("height");
    
      var color = d3.scaleOrdinal(d3.schemeCategory20);
    
      var simulation = d3.forceSimulation()
        .force("link", d3.forceLink().id(function(d) {
          return d.id;
        }))
        .force("charge", d3.forceManyBody())
        .force("center", d3.forceCenter(width / 2, height / 2));
    
      d3.json("https://jsonblob.com/api/9f7e2c48-8ccd-11e7-8b46-ef38640909a4", function(error, graph) {
        if (error) throw error;
    
        var link = svg.append("g")
          .attr("class", "links")
          .selectAll("line")
          .data(graph.links)
          .enter().append("line")
          .attr("stroke-width", function(d) {
            return Math.sqrt(d.value);
          });
    
        var node = svg.append("g")
          .attr("class", "nodes")
          .selectAll("image")
          .data(graph.nodes)
          .enter().append("image")
          .attr("xlink:href", "https://github.com/favicon.ico")
          .attr("x", -8)
          .attr("y", -8)
          .attr("width", 16)
          .attr("height", 16)
          .call(d3.drag()
            .on("start", dragstarted)
            .on("drag", dragged)
            .on("end", dragended));
    
    
        node.append("title")
          .text(function(d) {
            return d.id;
          });
    
        simulation
          .nodes(graph.nodes)
          .on("tick", ticked);
    
        simulation.force("link")
          .links(graph.links);
    
        function ticked() {
          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("transform", function(d) {
              return "translate(" + d.x + "," + d.y + ")";
    
            });
        }
      });
    
      function dragstarted(d) {
        if (!d3.event.active) simulation.alphaTarget(0.3).restart();
        d.fx = d.x;
        d.fy = d.y;
      }
    
      function dragged(d) {
        d.fx = d3.event.x;
        d.fy = d3.event.y;
      }
    
      function dragended(d) {
        if (!d3.event.active) simulation.alphaTarget(0);
        d.fx = null;
        d.fy = null;
      }
    </script>  d.fx = d3.event.x;
        d.fy = d3.event.y;
      }
    
      function dragended(d) {
        if (!d3.event.active) simulation.alphaTarget(0);
        d.fx = null;
        d.fy = null;
      }
    </script>

    添加您的图像应该就像更改和.attr("xlink:href" 一样简单:

    .attr("xlink:href", function(d){
       return d.img;
    )};
    

    【讨论】:

    • 像魅力一样工作。谢谢!但是,如果其他人遇到类似问题并想将图像放在圆圈内,您可以尝试此处提到的 defs:stackoverflow.com/questions/25881186/…,然后使用 if then 条件检查函数中的某些节点属性。
    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多