Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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");
like image 211
mikmikmik Avatar asked Sep 01 '12 18:09

mikmikmik


1 Answers

Just assign the parent d3 wrapper to a variable:

var g = categorized.enter().append("g");
g.append("circle").style("fill", "#ddd");
g.append("text").text(function(d,i){return d.count});
like image 111
Felix Kling Avatar answered Oct 22 '22 06:10

Felix Kling