D3 version 5 node labeling issue persists

I've been attempting to include labels within my node, but for some reason it's not working as expected.

//creating circles for the nodes
var node = g.append("g")
    .attr("class", "nodes")
    .selectAll("circle")
    .data(nodes_data)
    .enter()
    .append("circle")
    .attr("r", radius)
    .attr("fill", circleColour);
node.append("text")
    .attr("dx", 12)
    .attr("dy", ".35em")
    .text(function (d) { return d.name });

Upon inspecting the elements of the node, I can see that the text is indeed inside the node:

<circle r="15" fill="blue" style="touch-action: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);" cx="421.4930863434973" cy="221.26393165638473"><text dx="12" dy=".35em">some name</text></circle>

Despite this, the text is not displaying in the chart. Can anyone offer assistance with this issue? Here is the link to the full plunkr:

Answer №1

In a circle by definition, you can only have animation elements and descriptive elements as content. Therefore, text cannot be placed inside a circle.

The solution is to include multiple g elements for each node, and within them, have circle and text elements.

var node = g.selectAll('.nodes')
  .data(nodes_data)
  .enter().append('g')
  .attr('class', 'nodes')

node.append('circle')
    .attr("r", radius)
    .attr("fill", circleColour)

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

Then in the tick function:

node.attr('transform', d => `translate(${d.x},${d.y})`);

Here is a working forked version of the code on Plunker.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The console log is not being displayed in my Redux reducer and it is returning an undefined

I'm facing an issue with my Redux application after integrating JWT into my Nest API. Below is the code snippet from my reducer: export default function reducer(state = {}, action) { switch (action.type) { case 'USER_LOGIN_SUCCESS&apo ...

Is there a way to eliminate the default bootstrap stylings?

Currently working on a project where I am utilizing bootstrap scripts to simplify things. However, upon adding everything along with the CSS, bootstrap ends up changing parts of my page that I did not intend for it to do. Is there a way to remove these u ...

Managing cookies in PHP and JavaScript can present challenges due to variations in how different browsers

Our website utilizes ExpressionEngine as the CMS and Magento's cart for e-commerce. I am encountering challenges with cookies and their accessibility in various sections. A cookie is used for storing search selections, allowing users to return to our ...

Attempting to output properties from an Express/Mongo API by utilizing a React.js frontend

I am currently in the process of developing a simplistic fictional sneaker application with the MERN stack. While I wouldn't classify myself as a beginner, I'm also not an expert. I successfully created the backend and generated a json rest-api. ...

Trouble getting the ng-hide animation to work on the md-button element

I have implemented a basic CSS animation effect: .sample-show-hide { -webkit-transition: all linear 1.5s; transition: all linear 1.5s; } .sample-show-hide.ng-hide { opacity: 0; } .sample-show-hide.ng-show { opacity: 1; } My attempt to a ...

Order an array based on another array using the underscore library in Javascript

I'm trying to align the order of two arrays of objects. Here are the arrays: Array 1: var firstArray = [ {id: "1", value: 1}, {id: "5", value: 0.5}, {id: "3", value: 0.25}, {id: "4", value: 0.125}, {id: "2", value: 0.0625} ]; Array 2: var secondAr ...

Convert string IDs from a JSON object to numerical IDs in JavaScript

My goal is to convert the IDs in a JSON object received from PHP into numeric keys using JavaScript. The initial structure of my JSON object looks like this: let foo = {"66":"test","65":"footest"}; What I aim for is to transform it into this format: let f ...

Uploading files from a local directory to an API using node.js and multipart/form-data

I'm struggling to figure out how to upload a local file to a RESTful API service from my JavaScript application. The file I want to upload is located at "c:/folder/test.png" and I need to upload it to something like "localhost/uploads". I've sear ...

What is the best way to ensure that the angularjs ngChange handler is triggered only after the user has finished typing

Within my coding project, there is an input field that needs to have the ngChange variant applied. This particular input field is connected to an ajax call, which means when the user makes a change in the input, the server will process it. However, I want ...

Can we find a different way to address this issue with a "functional programming" mindset that doesn't involve utilizing the `forEach` method?

There has been a discussion about whether or not we still need to use forEach in JavaScript due to the availability of new language features. Some have suggested that this shift is an indirect push towards functional programming. I recently came across an ...

Update a map marker on Google Maps by changing the MySQL coordinate

I've been searching for assistance with this issue, but I haven't found any solutions that have worked for me. My goal is to plot the real-time GPS coordinates of a moving object on a Google Map. The GPS coordinates will be constantly updating i ...

Transforming jQuery script into AngularJS

I am currently working on a project with the Ionic framework that utilizes AngularJS. I am wondering if it is possible to rewrite the following code from jQuery to AngularJS? jQuery(function($) { var states = { 'USA': ['Arizona ...

Filtering an array of objects in real-time

var individuals = [ { Skin: "Bronze", Place: ["South"] }, { Skin: "Copper", Place: ["North", "South"] }, { Skin: "Copper", Place: ["North"] } ]; var requirements = [ { Field: "Skin", Values: ["Copper"] }, { Field: "Place", Values: ["North", "S ...

Creating a redirect button using React Material UI and React Router

I've been exploring how to use Material-UI with React and I'm struggling to figure out the best way to redirect to other pages or components. After reading various articles on different methods of redirection using react-router-dom, I still have ...

QuickFit, the jQuery plugin, automatically adjusts the size of text that is too large

I have incorporated the QuickFit library into my website to automatically resize text. However, I am running into an issue where the text is exceeding the boundaries of its containing div with a fixed size. This is something I need to rectify. This is ho ...

Transferring files to Amazon Web Services from Parse

I created an API using Node.js that enables users to upload data and media, whether it be photos or videos. AWS is configured to store the uploaded media and convert it from mp4 format to a streamable one. After completing much of the API development work ...

Each branch of the data structure is meticulously separated using recursion

I am struggling with a recursive data structure similar to the example provided below. My goal is for each branch, starting from null (parentTagId), to extend as far as the final one. I have no clue on how to achieve this, so any suggestions would be grea ...

How can you turn consecutive if statements into asynchronous functions in JavaScript?

In my code, I have a loop with a series of if statements structured like this: for( var i = 0; i < results_list.length; i++){ find = await results_list[i]; //result 1 if (find.Process == "one") { await stored_proc(38, find.Num, find ...

Creating unique reusable Angular components with dynamic attributes similar to React's props

I am looking to create a custom component that will allow me to insert content inside the opening and closing tags, which will be placed exactly where I want it within my HTML code. Here is an example of what I am trying to achieve: <my-custom-compo ...

What is the process for bundling a separate JavaScript file with Webpack5?

I am new to the world of webpack. I have 2 javascript files and I want to designate one as the main entry file. However, for the other file, I only want to include it in specific files. For example, looking at the file structure below, main.js is my entr ...