简体   繁体   English

为D3强制定向布局节点设置不同的图像

[英]Setting different images for D3 force-directed layout nodes

I've been trying to make a specific type of force direct graph, similar to this ( http://bl.ocks.org/mbostock/950642 ): 我一直试图制作一种特定类型的力直接图,类似于此( http://bl.ocks.org/mbostock/950642 ):

在此输入图像描述

However, instead of having all the same images I wish to have different images representing different information on the graph. 然而,我不希望拥有所有相同的图像,而是希望在图表上具有表示不同信息的不同图像。

My first step to this is being able to change all the circle images to random linked shapes. 我的第一步是能够将所有圆形图像更改为随机链接形状。 Whatever I try to implement in my code, the circles I have just disappear, instead of being replaced by different shapes. 无论我尝试在我的代码中实现什么,我刚刚消失的圆圈,而不是被不同的形状取代。 Any help on this problem would be great. 对这个问题的任何帮助都会很棒。 Here is the code. 这是代码。 Sorry, I'm also new to this site. 对不起,我也是这个网站的新手。

 // nodes
    var nodeSelecton = svg.selectAll(".node").data(nodes).enter().append("g").attr({
        class : "node"
    }).call(force.drag);

    nodeSelecton.append("circle").attr({
       r : nodeRadius 

    }).style("fill", function(d) {
        return color(d.group);
    });

    nodeSelecton.append("svg:text").attr("text-anchor", "middle").attr('dy',   ".35em").text(function(d) {
        return d.name;
    });

    // Add a new random shape.
      // nodes.push({
        //   type: d3.svg.symbolTypes[~~(Math.random() * d3.svg.symbolTypes.length)],
          // size: Math.random() * 300 + 100

This is a jsfiddle that is exuivalent to the first example that you linked. 是一个与你链接的第一个例子不同的jsfiddle。 I juist changed getting data to be from the JavaScript code instead of json file, since jsfiddle doesn't support external json files that well. 我juist改变了从JavaScript代码而不是json文件获取数据,因为jsfiddle不支持外部json文件。


First Solution 第一解决方案

Now, let's replace constant image with set of different images 现在,让我们用一组不同的图像替换恒定图像

Instead of this code: 而不是这段代码:

.attr("xlink:href", "https://github.com/favicon.ico")

we'll insert this code: 我们将插入此代码:

.attr("xlink:href", function(d) {
    var rnd = Math.floor(Math.random() * 64 + 1);
    var imagePath =
           "http://www.bigbiz.com/bigbiz/icons/ultimate/Comic/Comic"
           + rnd.toString() + ".gif";
    console.Log(imagePath);
    return imagePath;
})

and we'll get this : 我们会得到这个

在此输入图像描述


Second Solution 二解决方案

As you suggested in your code from the question, one could use built-in SVG symbols. 正如您在问题的代码中所建议的那样,可以使用内置的SVG符号。

Instead of this whole segment for inserting images: 而不是整个段插入图像:

node.append("image")
    .attr("xlink:href", "https://github.com/favicon.ico")
    .attr("x", -8)
    .attr("y", -8)
    .attr("width", 16)
    .attr("height", 16);

we could use this code: 我们可以使用这段代码:

node.append("path")
    .attr("d", d3.svg.symbol()
    .size(function(d) {
        return 100;
    })
    .type(function(d) {
        return d3.svg.symbolTypes[~~(Math.random() * d3.svg.symbolTypes.length)];
    }))
    .style("fill", "steelblue")
    .style("stroke", "white")
    .style("stroke-width", "1.5px")
    .call(force.drag);

and we'll get this : 我们会得到这个

在此输入图像描述


Hope this helps. 希望这可以帮助。

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

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