Is a donut chart graph visible on the webpage?

After successfully creating a bar chart, I decided to work on a donut chart using Angular and d3.js. However, despite creating the donut chart, I'm facing an issue with displaying it on the screen. The DOM shows that it is present, but for some reason, it's not visible. You can view the image of the issue https://i.sstatic.net/rG1Fq.png

Here is the data code that will be passed into the chart:

public data:any = 

 {a: 9, b: 20, c:30, d:8, e:12};

I've double-checked my code for parsing and drawing the donut chart, but I'm unsure why it's not working. I even tried using a debugger to pinpoint the problem. Here is the snippet of my code:

private drawDonut(dataSet: any[]): void{
 let svg = d3.select('figure#donutChart').select('svg')
.append('svg')
.attr("width", this.width)
.attr("height", this.height)
.append("g")
 .attr("transform", "translate(" + this.width / 2 + "," + 
 this.height / 2 + ")");
 console.log(this.data)

  //creating the colors in the donut chart for the 
  different data values
 var color = d3.scaleOrdinal()
  .domain(dataSet)
  .range(["#e7968b" , "#88b0ea" , "#66b447", "#744921 " , "#ffb02a"])

 const donut = d3.pie<any>()
  .value(d=>d[1]);
  //const sortedata =  Object.entries(dataSet).sort((a, b) 
=> Number(b[1]) - Number(a[1]))

 const dataInput = donut(Object.entries(dataSet))

// building the pie chart
  svg
  .selectAll('donutChart')
  .data(dataInput)
  .join('path')
  .attr('d', d3.arc<any>())
  .attr('fill', function(d: { data: string[]; }):any { 
   console.log("!!!!!!",d.data[1]); 
   return(color(d.data[1])) })
  .attr("stroke", "white")
  .style("stroke-width", "1px")
  

If anyone can provide assistance in identifying the issue, it would greatly benefit future projects.

Answer №1

When adding the SVG element to another selected SVG, make sure that the selected SVG actually exists:

let svg = d3.select('figure#donutChart').select('svg')
    .append('svg')

To avoid issues, simply remove the `select('svg') part:

let svg = d3.select('figure#donutChart').append('svg')

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

Ways to utilize a single node_modules folder across multiple projects

Is there a simple method to centralize and share one node_modules folder among all Angular projects, thereby preventing the need to download the same dependencies each time we start a new project? If so, is this approach recommended (what are the pros and ...

Why am I encountering the 'nonexistent type' error in my Vue 3 project that uses Typescript and Vuelidate?

Seeking assistance with a Vue 3 and Vuelidate issue. I followed the configuration guide provided at . <script lang="ts"> import { required, minLength, maxLength, numeric } from '@vuelidate/validators' import useVuelidate from &apo ...

Tips for dynamically displaying Angular Material tags within an Angular component using JSON data

I received a JSON response structured like this: { _id: '5dd0d0dc4db1cf9c77781aaa', picture: 'http://placehold.it/32x32', name: 'Graciela Mcmahon', guid: '98c0fcc2-1dfc-4974-bdae-d8263d783e0a&ap ...

Automatically convert TypeScript packages from another workspace in Turborepo with transpilation

I have set up a Turborepo-based monorepo with my primary TypeScript application named @myscope/tsapp. This application utilizes another TypeScript package within the same repository called @myscope/tspackage. For reference, you can view the example reposit ...

Maximizing the potential of Angular forms through native FormData

Currently, I am revisiting an older project that still uses traditional methods for form submission. The HTML includes a form element with defined method and action. My goal is to submit the form in the old-fashioned way using the action attribute to post ...

Preventing the display of the login page for an authenticated user in Angular

My application has different modules for authentication, dashboard, and root routing. The auth module contains routes for sign-in, the dashboard module has routes for home with authentication guard, and the root module redirects to home or a PageNotFound c ...

The combination of mat-icon-button and mat-raised-button is not rendering properly in Angular Material 15

After upgrading to Angular v15 + Angular Material v15, the previous code that used Angular v14 + Angular Material v14 looked like this: https://i.sstatic.net/GjC30.png The code for the icon button is shown below: <button *ngIf="admin" ...

How can you consistently emphasize data points in a line chart using NGRX?

I'm currently working with a line chart that displays line series. Is there any way to ensure all data points are highlighted at all times? Right now, the only time a data point is highlighted is when the mouse hovers over it. <ngx-charts-line ...

Struggling to retrieve the header in Angular 6

When setting headers from an Express server written in NodeJS, I use the following code: app.use('/routes', function(req, res, next) { res.setHeader('test', 'test'); next(); ); The headers are successfully sent to th ...

Creating multiple ngApps in Angular 4, 5, or 6

In our organization, we are considering a transition from Struts/Servlets to Angular 6 with a complete rewrite. However, since we have limited experience with Angular technology, I have some concerns. I am contemplating creating a shared Angular micro-s ...

The number entered will be incorporated into the API URL key value by passing the variable from page.html to services.ts

Recently diving into the world of Ionic, Angular, and Typescript, I've had a burning question. Can the number inputted be added to the API URL as one of the key values? I came across this helpful guide, specifically focusing on key event filtering (wi ...

Attempting to utilize a namespace-style import for calling or constructing purposes will result in a runtime failure

Using TypeScript 2.7.2 and VSCode version 1.21 with @types/express, I encountered an issue where in certain cases VSCode would report errors like: A namespace-style import cannot be called or constructed, and will cause a failure at runtime. Interestingly ...

Managing DOM elements within a Vue 3 template using Typescript

As I delve into the world of Vue 3 as a beginner, I encountered a challenge when it came to managing the DOM within Vue 3 templates. Let's take a look at the source code. MainContainer.vue <template> <div class="main-container" r ...

Continuous Updates to innerHtml in Angular2

While working on a project, I encountered an issue that seems to be more about style than anything else. The endpoint I am calling is returning an SVG image instead of the expected jpeg or png format, and I need to display it on the page. To address this, ...

Generating and setting an object property in TypeScript at runtime

In my code, I have defined an interface as follows: export interface OurHistory { ourHistory?: object; step1?:object; step2?:object; } Within the HistoryComponent class, I am doing the following: export class HistoryComponent implements OnInit, On ...

Looking for giphy link within a v-for loop (Vue.js)

I am fetching a list of movie characters from my backend using axios and rendering them in Bootstrap cards. My objective is to search for the character's name on Giphy and use the obtained URL as the image source for each card. However, when I attemp ...

Getting event properties in a React component using the rest operator: A comprehensive guide

Can someone please assist me? I am new to TypeScript and struggling with how to use event props in my component. I have defined two props and need all my events as rest props. I encountered an error when trying to use my component with onClick event. The ...

Node Package Manager (NPM): Easily Importing Files from a Package

Is there a way to customize the file import paths within a package? I am working with a UI kit package for our internal project and after building with Webpack, my project structure looks like this: - dist - components - index.d.ts - index.js Prior ...

Testing Angular components with Karma Server: An uncaught promise rejection occurred stating, "The code should be executed within the fakeAsync zone."

For my unit test development, I am utilizing karma. In some of my test cases, I have used fakeSync - tick() and promises. Although my tests are running successfully, there seems to be an issue with the karma server: my test.spec.ts : import { async, Co ...

The ng-bootstrap package is not fully compatible with Bootstrap 4 Alpha 6, particularly when it comes to collapsible elements such as forms

Just a heads up for everyone, the current version of ng-bootstrap is not compatible with the latest release of Bootstrap (Alpha 6). This includes collapsing elements like collapses, dropdowns, navs, etc. I haven't figured out how to make the package ...