20

我在尝试在 .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");
4

1 回答 1

36

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

var g = categorized.enter().append("g");
g.append("circle").style("fill", "#ddd");
g.append("text").text(function(d,i){return d.count});
于 2012-09-01T18:23:45.737 回答