Is there a way to generate a perfectly proportioned rectangular treemap using d3.js?

When attempting to generate a rectangular treemap in d3.js, I encountered an issue where the resulting shape was not as desired. There were instances of missing data and empty spaces within the rectangular treemap.

Upon investigation, I found that removing .sort() from my d3.hiearchy() almost resolved the issue, but it left an empty space in the top left corner:

https://i.sstatic.net/ocI2H.jpg

let root = d3.hierarchy(data)
  .sum(d => d.value)

In another scenario, adding the .sort() back to the d3.hiearchy() caused the first data point for "Wii Sports" to be removed:

https://i.sstatic.net/ZZMgY.jpg https://i.sstatic.net/S4NRR.jpg

let root = d3.hierarchy(data)
  .sum(d => d.value)
  .sort((a,b) => {return b.value - a.value;});

I need help identifying the underlying issue here. You can view the code on my CodePen

// Your JavaScript functions and rendering logic...
// CSS styling for tooltip and body...
// Required scripts for D3 and external libraries...

Answer №1

After following Andrew Reid's advice to utilize treemap.selectAll(null) instead of treemap.selectAll('g') and incorporating

.sort(function(a, b) { return b.height - a.height || b.value - a.value; });
, the structure of the treemap has been successfully optimized.

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 input group addon is not displaying properly in the Mozilla browser

I am presenting the following data in a table: <table class="neo-table"> <tr> <td>Day of final discharge home</td> <td>{{ form_widget(form.mHomeDischargeDay, {'id': 'M_Home_discharge_day', ' ...

In Java, use an HttpRequest to retrieve the webpage content and ensure that the process waits until the JavaScript has finished

Is there a way in Java to wait for partial content of a webpage to be loaded by JavaScript before initiating the rest of the process? The webpage contains script that loads additional content once the page is fully loaded, and I need to ensure that the p ...

What is the best way to make a canvas element fit the entire browser window without causing scroll bars to appear?

My window size isn't working properly, despite attempting the following code: Javascript var game; function game() { this.canvas = document.getElementById('canvas'); this.canvasWidth = window.innerWidth; this.canvasHeight = wi ...

"Use jQuery to select all the cells in a row except for the

I found a visually appealing table layout that looks like this : https://i.stack.imgur.com/oRxYP.png What caught my attention is how clicking on a row selects the entire row. Take the example in the image above, where the second row is highlighted upon s ...

What is the reason that server.js is always excluded from the Webpack build process?

I am currently utilizing Vue.js 3 for the front end of my application, while employing Node/Express for the back-end. My goal is to implement server side rendering, but I have encountered some challenges along the way. As far as I can tell, the client-sid ...

Steps for incorporating the count() value in Django for web developmentTo embed the count() value while creating

I'm currently working on a web visual board project using Django. Here is the specific HTML section where I need to populate values from the database: <tbody> {% for obj in data_list %} <tr> <th>{{ ob ...

Creating child rows with forms in Datatables

Currently, I am utilizing this particular example where I have a form embedded within the child rows. However, I have encountered an issue - the input from the child rows is not being submitted if the child rows are closed. Upon further examination, I noti ...

Troubleshooting Issues with Passing Values in jQuery AJAX POST Requests

Currently, I am working on two basic PHP pages: notification.php <html> <head><title></title> <meta charset="UTF-8"> <script src="https://cdn.firebase.com/js/client/2.4.2/firebase.js"></script> <script src="ht ...

Combining NPM Script Commands: A Comprehensive Guide

I am aware that NPM scripts can be chained using &&, pre-, and post- hooks. However, I am wondering if there is a way to simply break down lengthy script lines into a single, concatenated line? For instance, can I convert the following: "script": ...

Cease the continuous scrolling of the display element now

I'm having trouble with my progressbar animation when scrolling. It seems to run multiple times instead of just once. I apologize if there's an error in my code. Could someone please assist me? This is the code I am using: <div class="demo- ...

The comparison between exposing and creating objects in a Nodejs router file

Recently, I began using expressjs 4.0.0 and was impressed by the express.Router() object. However, a dilemma arose when I moved all my routes to another file - how do I expose an object to the routes file? In my server.js file: ... var passport = ...

Retrieve the difference in time from the current moment using moment.js

I am trying to implement a functionality where I can track when my data was last updated. - After hitting the 'Update' button, it should display 'Update now' - If it has been n minutes since the update, it should show 'n mins ago& ...

Encountering a clash with AJAX while attempting to incorporate PayPal

I am currently facing an issue while trying to integrate PayPal payment into my website. The problem arises when I attempt to use Ajax to send shopping cart data to create an order, resulting in the following alert message: Failed to load Response to pref ...

What is the best way to transfer a JS variable into a MySql database?

I have a JavaScript variable named count that I want to store in my MySQL database. let count = 0; countup.addEventListener("click", function() { count = count + 1; counter.innerText = count; let avgberechnung = count / 365; avg.innerText ...

Include brand logo to the Bootstrap navigation bar

Following the method provided in Bootstrap documentation, I included the following code: <div class="container"> <a class="navbar-brand" href="#"> <img src="/img/hangers.jpg" alt= ...

Dealing with CORS, IIS7, and PHP - Overcoming the Access-Control-Allow-Origin obstacle

I am attempting to enable another local host (such as javascript.dev) to make a xhr request to this particular host, which operates on an IIS7 server. When I perform a curl -I command, the headers I receive are as follows: HTTP/1.1 200 OK Content-Length: ...

What is the reason behind not requiring to invoke the next function in a Sails.js controller method, even when it includes an asynchronous database query?

Sample controller function: fetchArticles: function(req, res) { Articles.find({}).exec(function(err, articles) { res.json(articles) // It appears this part is asynchronous // Is next() required here? }) } In my experience, I typicall ...

The code is throwing an error indicating that an import statement is being used outside of a module specifically in relation

I've been working on building an HTML and JavaScript application that utilizes the jspdf module to export PDF files. However, each time I try to execute the code below, I encounter the error message Uncaught SyntaxError: Cannot use import statement o ...

Creating HTML inputs dynamically using jQuery in an ASP.NET application

I have written this JavaScript code to dynamically generate and add HTML elements within my asp.net webform: var i = 0; function addFields() { i++; var quantity = '<td class="auto-style2"><input type="text" size="3" name= ...

conflict between ng-content and textarea elements

My custom component called my-textarea has a unique template structure: <textarea> <ng-content></ng-content> </textarea> However, when I try to input text into the component using the same method as a regular textarea HTML eleme ...