gpt4 book ai didi

d3.js - 为什么我的图像没有显示在此 D3 图表的节点中?

转载 作者:行者123 更新时间:2023-12-01 12:43:10 25 4
gpt4 key购买 nike

fiddle :http://jsfiddle.net/jzaun/SCb7T/

代码:

var width = 500,
height = 500;

var dotSize = 50;

var color = d3.scale.category20();

var force = d3.layout.force()
.charge(-10020)
.linkDistance(dotSize)
.size([width, height]);

var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);

var graph = {
"nodes":[
{"name":"Name","group":1},
{"name":"Name","group":2},
{"name":"Name","group":3},
{"name":"Name","group":4}
],
"links":[
{"source":1,"target":0,"value":1},
{"source":2,"target":0,"value":1},
{"source":3,"target":0,"value":1}
]
};


force
.nodes(graph.nodes)
.links(graph.links)
.start();

var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("line")
.attr("class", "link");

var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", dotSize)
.style("fill", function(d) { return color(d.group); });

node.append("image")
.attr("xlink:href", "http://lorempixel.com/64/64/cats")
.attr("x", -32)
.attr("y", -32)
.attr("width", 64)
.attr("height", 64);

node.append("title")
.text(function(d) { return d.name; });

force.on("tick", function() {
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("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});

最佳答案

您不能将 image 附加到 circle 元素——SVG 规范不允许这样做。相反,直接附加图像并在 tick 函数中对它们设置 transform 而不是 cx/cy

完整示例 here .

关于d3.js - 为什么我的图像没有显示在此 D3 图表的节点中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22361687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com