Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3 force layout text label overlapping

I have a hard time positioning text label on my force chart. They are overlapping each other, and I could not figure out how to fix it. And I've tried many solutions from online, none of them works well. Could you please help me take a look?

Here are the code for my text labels:

    var node_text = node_textNew.append("svg:text")
    .attr("class", "text_note")
    .attr("dx", 0)
    .attr("dy", -0.5)
    .attr('background-color', '#fff')
    .attr("x",    function(d, i) { return circleWidth + 5; })
    .attr("y",  function(d, i) { if (i>0) { return circleWidth + 0 } else { return 8 } })
    .text(function(d) { return d.name});

Here is how it looks right now: enter image description here

Thank you so much for your help!

like image 628
Ellie Zhu Avatar asked Oct 19 '22 22:10

Ellie Zhu


2 Answers

Try this one http://bl.ocks.org/MoritzStefaner/1377729. Here the author introduces a way when a label is placed near a node using another force layout.

like image 102
kashesandr Avatar answered Dec 06 '22 04:12

kashesandr


One easy solution I have found us to use the center of a nodes Voronoi cell as the anchor for a label. This gives you the optimal spacing provided by your graph.

An example of this is seen in: https://bl.ocks.org/mbostock/6909318

like image 37
user2589273 Avatar answered Dec 06 '22 04:12

user2589273