gpt4 book ai didi

javascript - 组的 D3 v4 更新模式

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:12:50 25 4
gpt4 key购买 nike

给定以下布局:

<g>
... // many nodes
<g>
<circle></circle>
<text></text>
</g>
...
</g>

正确的更新模式在 d3 v4 中是什么样子的?我必须在 merge(?) 中使用什么作为参数,我必须多久调用一次 merge(仅在节点上?节点 + 圆圈 + 文本?)

我在 fiddle 上创建了一个工作示例:https://jsfiddle.net/cvvfsg97/6/

代码:

function update(items) {
node = nodeLayer.selectAll(".node")
.data(items, function(d) { return d.id; })

node = node.enter() // insert
.append("g")
.attr("class", "node");

node.append("circle") // insert
.attr("r", 2.5)
.attr('class', 'circle')
.merge(nodeLayer.selectAll('.node > circle')) // is this correct?! // merge
.attr('fill', 'red') // just for testing purposes
.exit().remove(); // exit

node.append("text") // insert
.attr("dy", 3)
.text(function(d) { return d.name; })
.merge(nodeLayer.selectAll('.node > text')) // is this correct?! // merge
.attr('fill', 'green') // just for testing purposes
.exit().remove();

node.merge(nodeLayer.selectAll('.node')) // is this correct?! // merge
.attr('class', 'anotherClass')
.exit().remove(); // does not work // exit
}

有人可以清楚地说明如何在组中使用 enter()、merge()、exit() 吗?

我可能喜欢在每个阶段对每个元素进行更改。


更新:我简化了示例,不需要链接或强制布局。我的问题只是关于更新模式,而不是力量。更新后的 jsfiddle 没有强制布局。

最佳答案

你把模式复杂化了。这是正确编写的更新函数:

function update(items) {

var node = nodeLayer.selectAll(".node") // bind the data, this is update
.data(items, function(d) {
return d.id;
});

node.exit().remove(); // exit, remove the g

nodeEnter = node.enter() // enter, append the g
.append("g")
.attr("class", "node");

nodeEnter.append("circle") // enter, append the circle on the g
.attr("r", 2.5)
.attr('class', 'circle')
.attr('fill', 'red');

nodeEnter.append("text") // enter, append the text on the g
.attr("dy", 3)
.text(function(d) {
return d.name;
})
.attr('fill', 'green');

node = nodeEnter.merge(node); // enter + update on the g

node.attr('transform', function(d){ // enter + update, position the g
return 'translate(' + d.x + ',' + d.y + ')';
});

node.select("text") // enter + update on subselection
.text(function(d) {
return d.name;
});

}

这里是通过多个调用运行的:

<!DOCTYPE html>
<html>

<head>
<script data-require="d3@4.0.0" data-semver="4.0.0" src="https://d3js.org/d3.v4.min.js"></script>
</head>

<body>
<script>

var nodeLayer = d3.select('body')
.append('svg')
.attr('width',500)
.attr('height',500);

update([
{
id: 1,
name: 'A',
x: Math.random() * 500,
y: Math.random() * 500
},{
id: 2,
name: 'B',
x: Math.random() * 500,
y: Math.random() * 500
},{
id: 3,
name: 'C',
x: Math.random() * 500,
y: Math.random() * 500
}
]);

setTimeout(function(){
update([
{
id: 1,
name: 'A',
x: Math.random() * 500,
y: Math.random() * 500
},{
id: 4,
name: 'This is a new name...',
x: Math.random() * 500,
y: Math.random() * 500
},{
id: 3,
name: 'C',
x: Math.random() * 500,
y: Math.random() * 500
}
]);
}, 3000);

function update(items) {

var node = nodeLayer.selectAll(".node")
.data(items, function(d) {
return d.id;
});

node.exit().remove(); // exit, remove the g

nodeEnter = node.enter() // enter the g
.append("g")
.attr("class", "node");

nodeEnter.append("circle") // enter the circle on the g
.attr("r", 2.5)
.attr('class', 'circle')
.attr('fill', 'red');

nodeEnter.append("text") // enter the text on the g
.attr("dy", 3)
.attr('fill', 'green');

node = nodeEnter.merge(node); // enter + update

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

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

}
</script>
</body>

</html>

关于javascript - 组的 D3 v4 更新模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41625978/

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