gpt4 book ai didi

force-layout - 使用 d3 v4 重新创建可折叠力布局

转载 作者:行者123 更新时间:2023-12-02 04:06:27 24 4
gpt4 key购买 nike

我一直在尝试使用 d3js v4 创建一个可折叠的力布局,类似于:https://mbostock.github.io/d3/talk/20111116/force-collapsible.html

我已经能够创建布局本身。但无法更新。有人可以帮忙吗?

这是我的js代码:

var width = 960,
height = 600;

var root = {
"name": "server1900",
"children": [{
"name": "server913",
"_children": null,
"children": [{
"name": "server948"
}, {
"name": "server946"
}]
}, {
"name": "server912",
"_children": null,
"children": [{
"name": "server984"
}, {
"name": "server983"
}]
}, {
"name": "server911",
"_children": null,
"children": [{
"name": "server999",
"_children": null,
"children": [{
"name": "server992"
}]
}]
}]
};

root = d3.hierarchy(root);

var i = 0;

var transform = d3.zoomIdentity;;

var nodeSvg, linkSvg, simulation, nodeEnter, linkEnter ;

var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.call(d3.zoom().scaleExtent([1 / 2, 8]).on("zoom", zoomed))
.append("g")
.attr("transform", "translate(40,0)");

function zoomed() {
svg.attr("transform", d3.event.transform);
}

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))
.on("tick", ticked);

update();

function update() {
var nodes = flatten(root);
var links = root.links();

simulation
.nodes(nodes)

simulation.force("link")
.links(links);

linkSvg = svg.selectAll(".link")
.data(links, function(d) { return d.target.id; })

linkSvg.exit().remove();

linkSvg = linkSvg.enter()
.append("line")
.attr("class", "link");

nodeSvg = svg.selectAll(".node")
.data(nodes, function(d) { return d.id; })

nodeSvg.exit().remove();

nodeSvg = nodeSvg.enter()
.append("g")
.attr("class", "node")
.on("click", click)
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended))

nodeSvg.append("circle")
.attr("r", 4 )
.append("title")
.text(function(d) { return d.data.name; })

nodeSvg.append("text")
.attr("dy", 3)
.attr("x", function(d) { return d.children ? -8 : 8; })
.style("text-anchor", function(d) { return d.children ? "end" : "start"; })
.text(function(d) { return d.data.name; });



}

function ticked() {
linkSvg
.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; });

nodeSvg
.attr("transform", function(d) { return "translate(" + d.x + ", " + d.y + ")"; });
}

function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
update();
simulation.restart();
} else {
d.children = d._children;
d._children = null;
update();
simulation.restart();
}
}

function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart()
simulation.fix(d);
}

function dragged(d) {
simulation.fix(d, d3.event.x, d3.event.y);
}

function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
simulation.unfix(d);
}

function flatten (root) {
// hierarchical data to flat data for force layout
var nodes = [];
function recurse(node) {
if (node.children) node.children.forEach(recurse);
if (!node.id) node.id = ++i;
else ++i;
nodes.push(node);
}
recurse(root);
return nodes;
}
line {
stroke: #666;
}

.node {
pointer-events: all;
}

circle {
stroke: none;
stroke-width: 40px;
}

.node text {
font: 8px sans-serif;
}
<script src="https://d3js.org/d3.v4.0.0-alpha.50.min.js"></script>

这是我的 fiddle 。 https://jsfiddle.net/t4vzg650/4/

谢谢

最佳答案

我忘记在 Enter() 之后合并旧节点。

link = svg.selectAll(".link").data(links, function(d) { return d.target.id; })
var linkEnter = link.enter().append("line").attr("class", "link");
link = linkEnter.merge(link);

感谢 Mike Bostock 帮助我解决了这个问题。我以为 d3 v4 有问题,结果我没有完全阅读更改:|

请参阅此了解更多信息:https://github.com/d3/d3-force/issues/37

修复 fiddle :https://jsfiddle.net/t4vzg650/6/

关于force-layout - 使用 d3 v4 重新创建可折叠力布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38011393/

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