「フォースレイアウトの変更」の例をd3v4に更新したいと思います。実用的な例はhttps://bl.ocks.org/mbostock/1095795です。
ただし、そうすると、ノードが左上隅に表示されます。これを修正するにはどうすればよいですか?あなたはhttps://bl.ocks.org/stevescc/f964f8f4658bc11319765f83f67b7f9fで私のフォークを見ることができます
フォークで行った変更を確認するには、こちらを参照してください。私の知る限り、私が行った変更は、v4への変更によって引き起こされたものだけです。
以下は完全を期すための完全なindex.htmlです。
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.link {
stroke: #000;
stroke-width: 1.5px;
}
.node {
fill: #000;
stroke: #fff;
stroke-width: 1.5px;
}
.node.a { fill: #1f77b4; }
.node.b { fill: #ff7f0e; }
.node.c { fill: #2ca02c; }
</style>
<body>
<script src="//d3js.org/d3.v4.js"></script>
<script>
var width = 960,
height = 500;
var color = d3.scaleOrdinal(d3.schemeCategory10);
var nodes = [],
links = [];
var force = d3.forceSimulation()
.nodes(nodes)
.force("link", d3.forceLink(links)
.distance(120))
.force("charge", d3.forceManyBody().strength(-400))
.force("X", d3.forceX().x(width/2))
.force("Y", d3.forceY().y(height/2))
.on("tick", tick);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var node = svg.selectAll(".node"),
link = svg.selectAll(".link");
// 1. Add three nodes and three links.
setTimeout(function() {
var a = {id: "a"}, b = {id: "b"}, c = {id: "c"};
nodes.push(a, b, c);
links.push({source: a, target: b}, {source: a, target: c}, {source: b, target: c});
start();
}, 0);
// 2. Remove node B and associated links.
setTimeout(function() {
nodes.splice(1, 1); // remove b
links.shift(); // remove a-b
links.pop(); // remove b-c
start();
}, 3000);
// Add node B back.
setTimeout(function() {
var a = nodes[0], b = {id: "b"}, c = nodes[1];
nodes.push(b);
links.push({source: a, target: b}, {source: b, target: c});
start();
}, 6000);
function start() {
link = link.data(force.force('link').links(), function(d) { return d.source.id + "-" + d.target.id; });
link.enter().insert("line", ".node").attr("class", "link");
link.exit().remove();
node = node.data(force.nodes(), function(d) { return d.id;});
node.enter().append("circle").attr("class", function(d) { return "node " + d.id; }).attr("r", 8);
node.exit().remove();
force.restart();
}
function tick() {
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
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; });
}
</script>
ブロックがd3v4に更新されます(https://bl.ocks.org/mbostock/1095795)。そして問題は、入力された要素をmerge
既存の要素とマージ(関数)する必要があることでした。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加