gpt4 book ai didi

javascript - 如何动态添加链接到 d3 拓扑?

转载 作者:行者123 更新时间:2023-12-02 16:52:59 28 4
gpt4 key购买 nike

我拿了sticky force layout示例并尝试添加额外链接并使用 enter() 更新布局,但随后所有链接都消失了,FireBug 也没有显示任何错误。

这些行不应该添加链接吗?
graph.links.push({"source": 0, "target": 11});<br/>
link = link.data(graph.links).enter().append("line").attr("class", function(d,i) {console.log(i);return "link";});

此外,console.log(i)输出18而不是19 。就好像它从未考虑过 graph 大小的增加一样。数组。

我检查了this example ,但即使在这里,似乎也使用 data()enter()功能应该足够了。

代码:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.link {
stroke: #000;
stroke-width: 1.5px;
}

.node {
cursor: move;
fill: #ccc;
stroke: #000;
stroke-width: 1.5px;
}

.node.fixed {
fill: #f00;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var graph =
{
"nodes": [
{"x": 469, "y": 410},
{"x": 493, "y": 364},
{"x": 442, "y": 365},
{"x": 467, "y": 314},
{"x": 477, "y": 248},
{"x": 425, "y": 207},
{"x": 402, "y": 155},
{"x": 369, "y": 196},
{"x": 350, "y": 148},
{"x": 539, "y": 222},
{"x": 594, "y": 235},
{"x": 582, "y": 185},
{"x": 633, "y": 200}
],
"links": [
{"source": 0, "target": 1},
{"source": 1, "target": 2},
{"source": 2, "target": 0},
{"source": 1, "target": 3},
{"source": 3, "target": 2},
{"source": 3, "target": 4},
{"source": 4, "target": 5},
{"source": 5, "target": 6},
{"source": 5, "target": 7},
{"source": 6, "target": 7},
{"source": 6, "target": 8},
{"source": 7, "target": 8},
{"source": 9, "target": 4},
{"source": 9, "target": 11},
{"source": 9, "target": 10},
{"source": 10, "target": 11},
{"source": 11, "target": 12},
{"source": 12, "target": 10}
]
};

var width = 960,
height = 500;

var force = d3.layout.force()
.size([width, height])
.charge(-400)
.linkDistance(40)
.on("tick", tick);

var drag = force.drag()
.on("dragstart", dragstart);

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

var link = svg.selectAll(".link"),
node = svg.selectAll(".node");

//d3.json("graph.json", function(error, graph) {

force.nodes(graph.nodes).links(graph.links).start();
link = link.data(graph.links).enter().append("line").attr("class", "link");

node = node.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 12)
.on("dblclick", dblclick)
.call(drag);
//});

console.log(graph.links.length);
graph.links.push({"source": 0, "target": 11});
console.log(graph.links.length);
link = link.data(graph.links).enter().append("line").attr("class", "link");


function tick() {
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; });
}

function dblclick(d) {
d3.select(this).classed("fixed", d.fixed = false);
}

function dragstart(d) {
d3.select(this).classed("fixed", d.fixed = true);
}

</script>

最佳答案

主要问题是

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

此后,link 将仅包含在您使用 .enter() 时刚刚添加到可视化效果的链接。这反过来意味着只有那些会在强制布局的 tick 事件期间更新,因为您在那里使用相同的 link 变量。 node 也有同样的问题。

要修复此问题,请在添加新元素后将选择变量设置为包含所有相关元素。

link = svg.selectAll(".link");

完整演示 here .

关于javascript - 如何动态添加链接到 d3 拓扑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26424301/

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