javascript D3js如何在.enter上下文中附加2个相同级别的孩子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12230241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:39:55  来源:igfitidea点击:

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

javascriptd3.js

提问by mikmikmik

I'm having an issue trying to put a circle and a text inside a group (same level, not inside each other) in the .enter() context

我在尝试将一个圆圈和一个文本放在 .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

Is there a way to get back to the parent, sg like this:

有没有办法回到父级,像这样:

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

回答by Felix Kling

Just assign the parent d3 wrapper to a variable:

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

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