Adding text to d3 circle

void

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?

Mark

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Adding text in a circle border

From Dev

Adding an image within a circle object in d3 javascript?

From Dev

Adding hyeperlinks to zoomable d3 circle pack

From Dev

D3: adding a line when clicking on a circle in scatter plot

From Dev

d3 + adding text labels + formatting

From Dev

Adding text to d3 bars

From Dev

Adding html code to D3 text

From Dev

Text along circles in a D3 circle pack layout

From Dev

Breaking text into two lines inside circle in d3

From Dev

d3 treat text and circle as one object

From Dev

Place text on the radii extending from the center of a circle D3

From Dev

set both title and text of circle on d3 not working

From Dev

Adding text to the center of a D3 Donut Graph

From Dev

d3 foreignObjects, adding checkbox and text from data

From Dev

Adding more text to d3 pie chart on mouseover event

From Dev

D3 adding text lables to horizontal stacked barchart

From Dev

Adding more text to d3 pie chart on mouseover event

From Dev

Adding text to collapsible d3 tree links

From Dev

'd3-circle-text' on zoomable circle-pack

From Dev

D3: Simple Interaction - Circle with a click

From Dev

darken rect/circle on mouseover in d3

From Dev

D3 add zoom to circle pack

From Dev

d3 image in circle not rendering properly

From Dev

D3 circle object has no properties

From Dev

d3 line/circle chart

From Dev

Different colors for each circle - D3 circle pack

From Dev

OpenLayers3 add Text over Circle

From Dev

Zoomable Circle Packing with Automatic Text Sizing in D3.js

From Dev

Text blocking circle click method d3js