4

I am using this example http://bl.ocks.org/danielatkin/57ea2f55b79ae686dfc7 and I am trying to add some text in the circle.

This is how the circles are created and placed

    var nodes = svg.selectAll("circle")
       .data(data);

    nodes.enter().append("circle")
      .attr("class", function (d, i) { return "circle_"+d.sentiment+" circle_"+i})
      .attr("cx", function (d) { return d.x; })
      .attr("cy", function (d) { return d.x; })
      .attr("r", function (d) { return d.radius; })
      .style("fill", function (d, i) { return colors['default']; })
      .on("mouseover", function (d) { showPopover.call(this, d); })
          .on("mouseout", function (d) { removePopovers(this, true); })
          .call(pulse, function(d){ return d.radius+4; }, function(d){return d.radius; });

But I am getting no clue on how to add text to these circles.

I have already reffered this and this answer but couldn't succeed. The issue is that after following the above answers my circles stuck in a diagonal line.

Can someone tell me how to do this?

Community
  • 1
  • 1
void
  • 36,090
  • 8
  • 62
  • 107

2 Answers2

9

Another option, close to @cyril's is to use a g element and place both the circle and text inside. This saves you the double data-bind and double movement update:

var nodes = svg.selectAll("g")
    .data(data);

var g = nodes.enter().append("g")

g.append("circle")
  .attr("class", function(d) {
    return d.ratingCategory;
  })   
  .attr("r", 2)
  .attr("id", function(d){return d.objectName;})
  .on("mouseover", function(d) {
    showPopover.call(this, d);
  })
  .on("mouseout", function(d) {
    removePopovers();
  });

  g.append("text")
    .attr("dx",12)
    .attr("dy",".35em")
    .text(function(d){
        return d.objectName;
    });

And update the node position in the tick function as:

nodes.each(collide(.2))
  .attr("transform", function(d){ //<-- use transform it's not a g
    return "translate(" + d.x + "," + d.y + ")";
  });

Updated block.

Mark
  • 106,305
  • 20
  • 172
  • 230
  • Definitely better than mine :) – Cyril Cherian May 11 '16 at 04:36
  • Makes a hell lot of sense.. I want to ask one more question if you can help me with that. I want to make these bubbles at multiple places so I want to know which are the modular components/methods which can be reused to create the bubbles again? – void May 11 '16 at 05:57
4

Problem 1:

You are making the text within the circle dom, which is not correct.

<circle class="Low" cx="521.4462169807987" cy="183.39012004057906" r="10" id="171" data-original-title="" title="" aria-describedby="popover833687"><text dx="12" dy=".35em">171</text></circle>

It should have been this way:

<circle class="ratingCategory1" cx="343.02601945806816" cy="333.91072717787176" r="10" id="9" data-original-title="" title="" aria-describedby="popover766707"></circle>

<text x="317.17351217946583" y="310.10556778212015" dx="12" dy=".35em" class="All-Objects">4</text>

How to do this:

nodes.enter().append("circle") //make circle
    //.attr("class", "node")
     .attr("class", function(d) {
      return d.ratingCategory;
    })
    .attr("cx", function(d) {
      return d.x;
    })
    .attr("cy", function(d) {
      return d.y;
    })
    .attr("r", 2)
    .attr("id", function(d){return d.objectName;})
    .on("mouseover", function(d) {
      showPopover.call(this, d);
    })
    .on("mouseout", function(d) {
      removePopovers();
    })
    ;



var text = svg.selectAll("text")
        .data(data).enter().append("text")
     .attr("x", function(d) {
      return d.x;
    })
    .attr("y", function(d) {
      return d.y;
    })
        .attr("dx",12)
        .attr("dy",".35em")
        .text(function(d){
            return d.objectName;
        });

Problem 2

Inside the tick function you are only updating the circle but not the text, you should be updating both:

     //update circle
     d3.selectAll("circle").each(collide(.2))
       .attr("cx", function (d) { return d.x; })
       .attr("cy", function (d) { return d.y; });
  //update tick 
  d3.selectAll("text").each(collide(.2))
       .attr("x", function (d) { return d.x -20; })
       .attr("y", function (d) { return d.y; });

working code here

Cyril Cherian
  • 32,177
  • 7
  • 46
  • 55
  • Great, it worked amazing. I want to ask one more question if you can help me with that. I want to make these bubbles at multiple places so I want to know which are the modular components/methods which can be reused to create the bubbles again? – void May 11 '16 at 05:57