Arun AK
Arun AK

Reputation: 4370

Difficulty in displaying text in Force layout ? (D3js)

I am using force layout and i have some problem in displaying text in the layout. Here is the screenshot :enter image description here

and my code is :

svg.selectAll("circle")
    .data(data)
    .enter().append("circle")
        .attr("class", "node")
        .attr("fill", "blue")
        .attr("r", 4.5)
        .attr("dx", ".10em")
        .attr("dy", ".10em")
        .text(function(d){ return d.name});

The text is there in the code, but its not showing in the browser. I even changed the color but nothing helps.

Upvotes: 1

Views: 44

Answers (1)

MR Zamani
MR Zamani

Reputation: 1423

As @Lars Kotthoff mentioned circles don't support text(). you should change your code to sample like this:

 svg.selectAll("circle")
     .data(data)
    .enter().append("circle")
    .attr("class", "node")
    .attr("fill", "blue")
    .attr("r", 4.5)
    .attr("cx", ".10em")
    .attr("cy", ".10em");

 svg.selectAll("text")
    .data(data)
    .enter().append("text")
    .attr("class", "node")
    .attr("fill", "red")
    .attr("dx", ".10em")
    .attr("dy", ".10em")
    .text(function(d){ return d.name});

Jsfiddle code here.

Upvotes: 1

Related Questions