在d3 v4网络图中为节点添加不同的图像 [英] Add different images for nodes in d3 v4 network graph

查看:276
本文介绍了在d3 v4网络图中为节点添加不同的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know it's there're a few topics that discuss the issue but non of the found results worked for me. I'm trying to modify the classic d3 network graph Les miserables example (d3v4 version HERE: https://bl.ocks.org/mbostock/4062045) to add different images for different nodes - the relative path of the file being given as one of the node attributes, eg.

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

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

What I'm trying to achieve is similar to this:https://bl.ocks.org/mbostock/950642 but made in d3v4, and different images for different nodes.

全部我找到的例子(也是这个的代码片段,遗憾的是它没有'为我工作)指向类似的方法,看起来或多或少像这样(在d3 v4和v3中):

All examples that I found (also this promissing code snippet, which unfortunately doesnt't work for me) point me to similar approach, which looks more or less like this(both in d3 v4 and v3):

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

然而,尽管花了几个小时,但我还是无法运作。任何想法?

However, despite a few hours spent I can not make it work. Any ideas?

推荐答案

这是一个结合 v4 示例的快速示例使用基于 v3 的图像示例:

Here's a quick example which combines your v4 example with an image based on your v3 example:

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

Adding in your images should then be as simple as changing and .attr("xlink:href" to:

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

这篇关于在d3 v4网络图中为节点添加不同的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆