use*_*291 4 javascript image d3.js force-layout
我一直试图制作一种特定类型的力直接图,类似于此(http://bl.ocks.org/mbostock/950642):

然而,我不希望拥有所有相同的图像,而是希望在图表上具有表示不同信息的不同图像.
我的第一步是能够将所有圆形图像更改为随机链接形状.无论我尝试在我的代码中实现什么,我刚刚消失的圆圈,而不是被不同的形状取代.对这个问题的任何帮助都会很棒.这是代码.对不起,我也是这个网站的新手.
// 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
Run Code Online (Sandbox Code Playgroud)
这是一个与你链接的第一个例子不同的jsfiddle.我juist改变了从JavaScript代码而不是json文件获取数据,因为jsfiddle不支持外部json文件.
现在,让我们用一组不同的图像替换恒定图像
而不是这段代码:
.attr("xlink:href", "https://github.com/favicon.ico")
Run Code Online (Sandbox Code Playgroud)
我们将插入此代码:
.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;
})
Run Code Online (Sandbox Code Playgroud)
我们会得到这个:

正如您在问题的代码中所建议的那样,可以使用内置的SVG符号.
而不是整个段插入图像:
node.append("image")
.attr("xlink:href", "https://github.com/favicon.ico")
.attr("x", -8)
.attr("y", -8)
.attr("width", 16)
.attr("height", 16);
Run Code Online (Sandbox Code Playgroud)
我们可以使用这段代码:
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);
Run Code Online (Sandbox Code Playgroud)
我们会得到这个:

希望这可以帮助.