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 8980139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:00:19+00:00 2026-06-15T20:00:19+00:00

I created a Tree in D3.js based on Mike Bostock’s Node-link Tree. The problem

  • 0

I created a Tree in D3.js based on Mike Bostock’s Node-link Tree. The problem I have and that I also see in Mike’s Tree is that the text label overlap/underlap the circle nodes when there isn’t enough space rather than extend the links to leave some space.

As a new user I’m not allowed to upload images, so here is a link to Mike’s Tree where you can see the labels of the preceding nodes overlapping the following nodes.

I tried various things to fix the problem by detecting the pixel length of the text with:

d3.select('.nodeText').node().getComputedTextLength();

However this only works after I rendered the page when I need the length of the longest text item before I render.

Getting the longest text item before I render with:

nodes = tree.nodes(root).reverse();

var longest = nodes.reduce(function (a, b) { 
  return a.label.length > b.label.length ? a : b; 
});

node = vis.selectAll('g.node').data(nodes, function(d, i){
  return d.id || (d.id = ++i); 
});

nodes.forEach(function(d) {
  d.y = (longest.label.length + 200);
});

only returns the string length, while using

d.y = (d.depth * 200);

makes every link a static length and doesn’t resize as beautiful when new nodes get opened or closed.

Is there a way to avoid this overlapping? If so, what would be the best way to do this and to keep the dynamic structure of the tree?

There are 3 possible solutions that I can come up with but aren’t that straightforward:

  1. Detecting label length and using an ellipsis where it overruns child nodes. (which would make the labels less readable)
  2. scaling the layout dynamically by detecting the label length and telling the links to adjust accordingly. (which would be best but seems really difficult
  3. scale the svg element and use a scroll bar when the labels start to run over. (not sure this is possible as I have been working on the assumption that the SVG needs to have a set height and width).
  • 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-15T20:00:19+00:00Added an answer on June 15, 2026 at 8:00 pm

    So the following approach can give different levels of the layout different “heights”. You have to take care that with a radial layout you risk not having enough spread for small circles to fan your text without overlaps, but let’s ignore that for now.

    The key is to realize that the tree layout simply maps things to an arbitrary space of width and height and that the diagonal projection maps width (x) to angle and height (y) to radius. Moreover the radius is a simple function of the depth of the tree.

    So here is a way to reassign the depths based on the text lengths:

    First of all, I use the following (jQuery) to compute maximum text sizes for:

    var computeMaxTextSize = function(data, fontSize, fontName){
        var maxH = 0, maxW = 0;
    
        var div = document.createElement('div');
        document.body.appendChild(div);
        $(div).css({
            position: 'absolute',
            left: -1000,
            top: -1000,
            display: 'none',
            margin:0, 
            padding:0
        });
    
        $(div).css("font", fontSize + 'px '+fontName);
    
        data.forEach(function(d) {
            $(div).html(d);
            maxH = Math.max(maxH, $(div).outerHeight());
            maxW = Math.max(maxW, $(div).outerWidth());
        });
    
        $(div).remove();
        return {maxH: maxH, maxW: maxW};
    }
    

    Now I will recursively build an array with an array of strings per level:

    var allStrings = [[]];
    var childStrings = function(level, n) {
        var a = allStrings[level];
        a.push(n.name);
    
        if(n.children && n.children.length > 0) {
            if(!allStrings[level+1]) {
                allStrings[level+1] = [];
            }
            n.children.forEach(function(d) {
                childStrings(level + 1, d);
            });
        }
    };
    childStrings(0, root);
    

    And then compute the maximum text length per level.

    var maxLevelSizes = [];
    allStrings.forEach(function(d, i) {
        maxLevelSizes.push(computeMaxTextSize(allStrings[i], '10', 'sans-serif'));
    });
    

    Then I compute the total text width for all the levels (adding spacing for the little circle icons and some padding to make it look nice). This will be the radius of the final layout. Note that I will use this same padding amount again later on.

    var padding = 25; // Width of the blue circle plus some spacing
    var totalRadius = d3.sum(maxLevelSizes, function(d) { return d.maxW + padding});
    
    var diameter = totalRadius * 2; // was 960;
    
    var tree = d3.layout.tree()
        .size([360, totalRadius])
        .separation(function(a, b) { return (a.parent == b.parent ? 1 : 2) / a.depth; });
    

    Now we can call the layout as usual. There is one last piece: to figure out the radius for the different levels we will need a cumulative sum of the radii of the previous levels. Once we have that we simply assign the new radii to the computed nodes.

    // Compute cummulative sums - these will be the ring radii
    var newDepths = maxLevelSizes.reduce(function(prev, curr, index) {
        prev.push(prev[index] + curr.maxW + padding);                 
        return prev;
    },[0]);                                                      
    
    var nodes = tree.nodes(root);
    
    // Assign new radius based on depth
    nodes.forEach(function(d) {
        d.y = newDepths[d.depth];
    });
    

    Eh voila! This is maybe not the cleanest solution, and perhaps does not address every concern, but it should get you started. Have fun!

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

Sidebar

Related Questions

I have a binary tree class that is created with a root node and
I created a simple parameterized tree structure, based on a root Node. Each node
I have an Excel VBA macro that creates a folder tree based on an
I am using ExtJS 4. I have created a tree panel as var treeStore
For a new project I have created a local folder tree with empty Visual
I have two tree, which contains nodes, after I've created them, I take all
I am trying to create an HQL query that will filter a tree based
I have created an online filesystem, based on php without using databases. It works
I have ultraTree (infragistics tree) which is created at design time and it has
I have an Application layout that is based on a treeView at the left,

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.