Understood Understood - 1 year ago 161
Javascript Question

D3.js Auto font-sizing based on nodes individual radius/diameter

How can I have D3.js automatically adjust font-size for each node based on their individual radius/diameter?

I use a style that allows automatic increase insize

.attr("dy", ".3em")
.style("text-anchor", "middle")
.text(function(d) { return d.className.substring(0, d.r / 3); })
.style("font-size", "10px") // initial guess
//This is what gives it increased size...
.style("font-size", function(d) { return (2 * d.r - 10) / this.getComputedTextLength() * 10 + "px"; })

; * 10 + "px"; })

This effect removes the text from the smaller nodes. I also have a zoom function that I can increase a dot that originally cover 12 px to cover my entire screen.

.call(d3.behavior.zoom().scaleExtent([1, 200]).on("zoom", zoom))

Is there a way I can automatically format node-font individually; to write at appropriate sizes so when zoom-in the called node-font will appear proportionate to node-size vs a single font-size fits all?

enter image description here

The Right Lists Circles: NAME(SIZE)
I would love a working examples to learn from. So at the image size the little green dot north of driving circle next to the P would have black unreadable words until we zoom in to see what is written on the circle. The goal is to have proportionate readable font when zoomed in..?

Answer Source

You can do this by dynamically setting the text size based on the size of the container. For this, you have to add the text, get its bounding box, get the bounding box of the container element and derive the correct font size based on the current font size and those bounding boxes.

The code would look something like this.

// ...
  .style("font-size", "1px")
  .style("font-size", function(d) { return d.scale + "px"; });

function getSize(d) {
  var bbox = this.getBBox(),
      cbbox = this.parentNode.getBBox(),
      scale = Math.min(cbbox.width/bbox.width, cbbox.height/bbox.height);
  d.scale = scale;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download