D3js如何在.enter上下文中在同一级别附加两个子级

D3js how to append 2 children at the same level in .enter context

本文关键字:两个 一级 enter 上下文 D3js      更新时间:2023-09-26

我在尝试将圆圈和文本放入.enter()上下文中的组(相同级别,而不是彼此内部)时遇到问题

var categorized = g1.selectAll("g.node").data(dataset, function(d){return d.id})
categorized
.enter()
    .append("g")
    .attr("id", function(d,i){return d.id;});
categorized
.enter().append("circle")
    .style("fill", "#ddd");
// throws an error
categorized
.append('text')
    .text(function(d,i){return d.count});
// this is working but is an update so I have to remove the text on exit

有没有一种方法可以回到父级,sg是这样的:

categorized
.enter()
.append("g")
.append("circle")
.getBackToParent // the g
.append("text");

只需将父d3包装器分配给一个变量:

var g = categorized.enter().append("g");
g.append("circle").style("fill", "#ddd");
g.append("text").text(function(d,i){return d.count});