Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8094983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:58:21+00:00 2026-06-05T20:58:21+00:00

I’ve been using the sample code from this d3 project to learn how to

  • 0

I’ve been using the sample code from this d3 project to learn how to display d3 graphs and I can’t seem to get text to show up in the middle of the circles (similar to this example and this example). I’ve looked at other examples and have tried adding

node.append("title").text("Node Name To Display")

and

node.append("text")
    .attr("text-anchor", "middle")
    .attr("dy", ".3em").text("Node Name To Display")

right after node is defined but the only results I see is “Node Name To Display” is showing up when I hover over each node. It’s not showing up as text inside the circle. Do I have to write my own svg text object and determine the coordinates of that it needs to be placed at based on the coordinates of radius of the circle? From the other two examples, it would seem like d3 already takes cares of this somehow. I just don’t know the right attribute to call/set.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:58:22+00:00Added an answer on June 5, 2026 at 8:58 pm

    There are lots of examples showing how to add labels to graph and tree visualizations, but I’d probably start with this one as the simplest:

    • http://bl.ocks.org/950642

    You haven’t posted a link to your code, but I’m guessing that node refers to a selection of SVG circle elements. You can’t add text elements to circle elements because circle elements are not containers; adding a text element to a circle will be ignored.

    Typically you use a G element to group a circle element (or an image element, as above) and a text element for each node. The resulting structure looks like this:

    <g class="node" transform="translate(130,492)">
      <circle r="4.5"/>
      <text dx="12" dy=".35em">Gavroche</text>
    </g>
    

    Use a data-join to create the G elements for each node, and then use selection.append to add a circle and a text element for each. Something like this:

    var node = svg.selectAll(".node")
        .data(nodes)
      .enter().append("g")
        .attr("class", "node")
        .call(force.drag);
    
    node.append("circle")
        .attr("r", 4.5);
    
    node.append("text")
        .attr("dx", 12)
        .attr("dy", ".35em")
        .text(function(d) { return d.name });
    

    One downside of this approach is that you may want the labels to be drawn on top of the circles. Since SVG does not yet support z-index, elements are drawn in document order; so, the above approach causes a label to be drawn above its circle, but it may be drawn under other circles. You can fix this by using two data-joins and creating separate groups for circles and labels, like so:

    <g class="nodes">
      <circle transform="translate(130,492)" r="4.5"/>
      <circle transform="translate(110,249)" r="4.5"/>
      …
    </g>
    <g class="labels">
      <text transform="translate(130,492)" dx="12" dy=".35em">Gavroche</text>
      <text transform="translate(110,249)" dx="12" dy=".35em">Valjean</text>
      …
    </g>
    

    And the corresponding JavaScript:

    var circle = svg.append("g")
        .attr("class", "nodes")
      .selectAll("circle")
        .data(nodes)
      .enter().append("circle")
        .attr("r", 4.5)
        .call(force.drag);
    
    var text = svg.append("g")
        .attr("class", "labels")
      .selectAll("text")
        .data(nodes)
      .enter().append("text")
        .attr("dx", 12)
        .attr("dy", ".35em")
        .text(function(d) { return d.name });
    

    This technique is used in the Mobile Patent Suits example (with an additional text element used to create a white shadow).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
Does anyone know how can I replace this 2 symbol below from the string
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.