gpt4 book ai didi

javascript - 对forceSimulation 的更改禁用拖动

转载 作者:太空宇宙 更新时间:2023-11-04 15:49:10 25 4
gpt4 key购买 nike

我修改了Bostock's force direct graph example通过添加标签和更改一些forceSimulation参数来生成我想要的布局:

<!DOCTYPE html>
<meta http-equiv="content-type" content="text/html; charset=UTF8">
<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" charset="urf-8"></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("http://localhost/mail/bostock/getneighborhood.php?contact=5244", 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(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node")
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));

node.append("circle")
.attr("r", 5)
.attr("fill", function(d) { return color(d.group); });


node.append("text")
.attr("dx", 12)
.attr("dy", ".32em")
.attr("font-family", "sans-serif")
.attr("font-size", "8px")
.text(function(d) { return d.id; });

var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }).distance(150).strength(1))
.force("collide",d3.forceCollide( function(d){return d.r + 8 }).iterations(16) )
.force("charge", d3.forceManyBody().strength(-200))
.force("center", d3.forceCenter(width / 2, height / 2));

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>

唯一的问题是拖动功能不再起作用。谁能建议如何修复?

最佳答案

如果您查看代码,您会发现有两个名为simulation的变量。其中之一位于 d3.json 之外...

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 中的另一个:

var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) {
return d.id;
}).distance(150).strength(1))
.force("collide", d3.forceCollide(function(d) {
return d.r + 8
}).iterations(16))
.force("charge", d3.forceManyBody().strength(-200))
.force("center", d3.forceCenter(width / 2, height / 2));

移动节点的模拟是内部模拟。但是,由于函数范围的原因,拖动函数引用了外部变量。

解决方案:仅保留一个模拟,外部 d3.json

关于javascript - 对forceSimulation 的更改禁用拖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43283778/

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