Using D3, I show a bunch of circles of different sizes, each of which is filled with text. I'm stuck in finding the right font size so that the text matches the correct one in a circle, depending on its size and the length of the text. Long text should be divided into several lines. Here is my code:
var data = { "name": "", "children": [ { "name": "This is a tag", "value": 242 }, { "name": "Circle", "value": 162 }, { "name": "Tree", "value": 80 }, { "name": "My sentence is very long and needs breaks", "value": 80 }, ] } var diameter = 300, format = d3.format(",d"); var bubble = d3.layout.pack() .sort(null) .size([diameter, diameter]) .padding(1.5); var svg = d3.select("body").append("svg") .attr("width", diameter) .attr("height", diameter) .attr("class", "bubble"); d3.json(data, function(error, root) { var node = svg.selectAll(".node") .data(bubble.nodes(data) .filter(function(d) { return !d.children; })) .enter().append("g") .attr("class", "node") .attr("transform", function(d) { return "translate(" + dx + "," + dy + ")"; }); node.append("circle") .attr("r", function(d) { return dr; }) .style("fill", function(d) { return '#f88' });
I also created a fiddle at http://jsfiddle.net/L4nMx/ I think I should calculate the width of the text and change the font size until it matches the size of the circle or something like that. Or is there a stretch function to do this in a simple way?
quape source share