I'm unable to see the D3.js text within the center of the SVG circle

I am working on visualizing a sunburst and facing an issue with placing text inside the SVG circle at the center of the sunburst. The circle renders perfectly, but I can't seem to get any text to display in the middle. I have created a demonstration of my attempt on jsfiddle, along with the JavaScript code provided below. Despite looking at other solutions on similar posts, nothing seems to solve this specific problem. I even tried wrapping the element inside a g tag as recommended for rendering text, but still no luck. Please let me know if you need more information. Any assistance would be greatly appreciated.

Thank you!

<html lang="en">

<head>

    <title>Sunburst</title>

    <!-- external css -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">

</head>

<!-- external javascript-->
<script src="https://code.jquery.com/jquery-3.3.1.min.js">          </script>
<script src="https://d3js.org/d3.v5.min.js">                        </script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>


<div class="container-fluid">

  <div class="row">

    <div id="sunburst-container" class="col-9"></div>

  </div>

</div>

const svg = d3.select("#sunburst-container")
              .append("svg")
              .attr("width", "200px")
              .attr("height", "200px");


const g = svg.append("g")
                       .attr("transform", "translate(100, 100)");

const parent = g.append("circle")
                                .attr("r", "50px")
                .attr("fill", "#ddddbb");

parent.append("g")
      .append("text")
      .text("hello world")
      .attr('text-anchor', 'middle')
      .attr('alignment-baseline', 'middle')
      .style('font-size', '12px')
      .attr('fill', 'white');

Answer №1

While working on adding a group and text to the "parent" circle, it should be noted that SVG primitives may not have the ability to contain other elements. To witness this distinction, run the code snippet provided below.

For troubleshooting similar issues, try utilizing browser developer tools to analyze where items are being added or modified.

https://i.sstatic.net/t0jOC.png

const svg = d3.select("#sunburst-container")
  .append("svg")
  .attr("width", "200px")
  .attr("height", "200px");


const g = svg.append("g")
  .attr("transform", "translate(100, 100)");

g.append("circle")
  .attr("r", "50px")
  .attr("fill", "#ff0000");


g.append("text")
  .text("hello world")
  .attr('text-anchor', 'middle')
  .attr('alignment-baseline', 'middle')
  .style('font-size', '12px')
  .attr('fill', 'white');
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

<div id="sunburst-container" class="col-9"></div>

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

Utilizing Next.js routing to accommodate two distinct subdomains on a single website

I am looking to develop a new platform using Next.js (React.js and React-router). The platform will have two distinct spaces - one for users and another for the owner to manage all users. To achieve this, I plan on splitting both areas into two subdomains: ...

Toggle display of fields based on radio button selection in ASP.NET

I am new to C# asp.net and I am working on creating an online registration form. I am facing a challenge with the following issue. Could someone assist me, please? There are 5 fields in this problem: 1) Category: radio button with options for St ...

Delivering information to personalized components using React Bootstrap

I am working with a custom styled checkbox that is part of a mapped data array. {nfcArray && nfcArray.map((item, key) => { return ( <tr class="hover"> <td className="cell_style pl-3 pl-lg-5"> ...

Troubleshooting: Unable to get the Bootstrap active navigation code to function

I've been struggling to make this code work in order to add the active class to my navigation list item, but it just won't cooperate. Home <li id="orange"> <a href="hotels.php"><span class="glyphicon glyphico ...

What is the most efficient way to perform multiple socket writes using a single active socket connection to an Express web server?

Attempting to perform multiple write requests using a single active socket connection to an express server running on localhost. This is done by making an HTTP request to an express web server on localhost with the following message: GET /temp?sensorId=1& ...

Using AngularJS to dynamically apply a CSS class to message text depending on the HTTP post response

I am looking to implement a method for adding a CSS class to the text color of a message based on the response. Currently, I achieve this by: if (response.data.status == 201) { angular.element(document.getElementById("msg")).addClass("text-green"); ...

Fill every empty element with elements from another array

I'm struggling to grasp the concept of arrays. I have two arrays and I want to replace the null elements in one with the elements from the other. Here is what I have currently: var arr1 = [1,2,3,4] var arr2 = [null, 99, null, null] arr2.map((item) = ...

Data is not appearing as expected in the React component when using the data

I'm currently facing an issue while working with MUI. I am able to retrieve the list in console.log, but nothing is being displayed on the screen - no errors or data, just the console.log output. Here is a snippet of the data that I am receiving: ...

I simply aim to remove a single piece of news at a time, yet somehow I manage to delete more than intended

Do you have a PHP file containing a list of news items fetched from the database? Each news item has a delete link, which looks like this: echo '<a id="'.$result_news['id_news'].'" class="j_newsdelete" href="#">Delete</a ...

Tips for specifying the "url" parameter in xmlhttp.open() for utilizing Ajax in communication with node.js

server.js has the ability to generate a random number. I am trying to retrieve a random number from the server and utilize xmlhttp to send a request. However, the string value remains unchanged when I visit http://localhost:3000/index.html. What could be c ...

What determines the priority of execution in the execution context stack?

Check out this insightful tutorial on execution context in JavaScript here. It's interesting how the order of invoking functionA() and console.log("GlobalContext") differs in terms of writing code versus the execution context stack. I'm curious, ...

What is the best way to incorporate products as components into the cart component?

ProductCard import React from 'react'; import { Card, Container, Row, Col, Button} from 'react-bootstrap'; import Cart from './Cart'; import './ItemCard.css'; function ProductCard(props){ return( <Car ...

JS, Express: Encounter issues - unable to find view in directory despite following provided instructions

Currently, I am following the steps outlined in this guide: Despite conducting extensive research, I am facing difficulty in getting it to work as expected. Here is the folder structure I am working with: https://i.sstatic.net/jCEdO.png Below is a snipp ...

transferring the information through the $scope parameter

After retrieving data from my web service, I am storing it in my scope variable like this: then(function (Tlist) { $scope.Tlist=Tlist.data; }) I then display this data in a table, where I can select rows using checkbox ...

What could be the reason that the auto complete feature in a jQuery Textbox is failing to work properly when utilized within

When implementing an auto complete jQuery in a Textbox on my web form, I encountered an issue. The jQuery works perfectly fine when used in a normal HTML page below the title tag and within the head tags, but it does not work when I try to use it within a ...

In order for the JavaScript function to properly execute, it requires the page to be

Check out this code snippet: <script> function scaleDown() { $('.work > figure').removeClass('current').addClass('not-current'); $('nav > ul > li').removeClass('current-li'); ...

Can a function be passed as props in a scenario where both a Parent and Child component are functional components?

I have a component called ParentComponent where I am trying to pass a function named toggleDrawer to another component called ChildComponent in the following way: const ParentComponent = () { const [drawerState, setDrawerState] = useState(false); ...

The operation of executing `mongodb - find()` does not exist as a function

I am having trouble printing all documents from the "members" collection. I attempted to use the find() function, but encountered an error stating that find() is not a function. Here is a snippet from member_model.js located in the models/admin folder: v ...

Is there a way to retrieve the content of an element using JavaScript?

I'm currently working on retrieving the value and content of an option element. I've managed to obtain the value using this.value as shown in the code snippet below: <select name='name' id='name' onchange='someFunctio ...

The functionality of Angular bootstrap scrollspy is hindered when dealing with dynamically changing image content

I recently came across an answer that guided me to fork an example for implementing scrollspy in Angular.js. My goal is to populate dynamic content using a template that includes images. You can find the example here: http://plnkr.co/edit/OKrzSr Here are ...