javascript - Zoomable Circle Packing with Automatic Text Sizing in D3.js -
i'm trying merge 2 of mike's examples: zoomable circle packing + automatic text sizing.
it works when displayed @ top-level. however, if zoom in next level, fonts not sized correctly.
i'm not sure if need modify transform, or modify part calculates font size.
here's codepen: http://codepen.io/anon/pen/gjwqrl
var circlefill = function(d) { if (d['color']) { return d.color; } else { return d.children ? color(d.depth) : '#fff'; } } var calculatetextfontsize = function(d) { return math.min(2 * d.r, (2 * d.r - 8) / this.getcomputedtextlength() * 11) + "px"; } var margin = 20, diameter = 960; var color = d3.scale.linear() .domain([-1, 18]) .range(["hsl(0,0%,100%)", "hsl(228,30%,40%)"]) .interpolate(d3.interpolatehcl); var pack = d3.layout.pack() .padding(2) .size([diameter - margin, diameter - margin]) .value(function(d) { return d.size; }) var svg = d3.select("body").append("svg") .attr("width", window.innerwidth) .attr("height", window.innerheight) .append("g") .attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")"); var focus = root, nodes = pack.nodes(root), view; var circle = svg.selectall("circle") .data(nodes) .enter().append("circle") .attr("class", function(d) { return d.parent ? d.children ? "node" : "node node--leaf" : "node node--root"; }) .style("fill", circlefill) .on("click", function(d) { if (focus !== d) zoom(d), d3.event.stoppropagation(); }); circle.append("svg:title") .text(function(d) { return d.name; }) var text = svg.selectall("text") .data(nodes) .enter().append("text") .attr("class", "label") .style("fill-opacity", function(d) { return d.parent === root ? 1 : 0; }) .style("display", function(d) { return d.parent === root ? null : "none"; }) .text(function(d) { return d.name; }) .style("font-size", calculatetextfontsize) .attr("dy", ".35em"); var node = svg.selectall("circle,text"); d3.select("body") .style("background", color(-1)) .on("click", function() { zoom(root); }); zoomto([root.x, root.y, root.r * 2 + margin]); function zoom(d) { var focus0 = focus; focus = d; var transition = d3.transition() .duration(d3.event.altkey ? 7500 : 750) .tween("zoom", function(d) { var = d3.interpolatezoom(view, [focus.x, focus.y, focus.r * 2 + margin]); return function(t) { zoomto(i(t)); }; }); transition.selectall("text") .filter(function(d) { return d.parent === focus || this.style.display === "inline"; }) .style("fill-opacity", function(d) { return d.parent === focus ? 1 : 0; }) .each("start", function(d) { if (d.parent === focus) this.style.display = "inline"; }) .each("end", function(d) { if (d.parent !== focus) this.style.display = "none"; }); } function zoomto(v) { var k = diameter / v[2]; view = v; node.attr("transform", function(d) { return "translate(" + (d.x - v[0]) * k + "," + (d.y - v[1]) * k + ")"; }); circle.attr("r", function(d) { return d.r * k; }); } d3.select(self.frameelement).style("height", diameter + "px");
clicking largest sub-circle in "vis" circle illustrates problem.
first give id circle, here giving text name circle id can link text , circle via text name.
var circle = svg.selectall("circle") .data(nodes) .enter().append("circle") .attr("class", function(d) { return d.parent ? d.children ? "node" : "node node--leaf" : "node node--root"; }) .style("fill", circlefill) .attr("r", function(d) { return d.r; }) .attr("id", function(d) { return d.name;//setting text name id }) .on("click", function(d) { if (focus !== d) zoom(d), d3.event.stoppropagation(); });
on transition complete of zoom(d) function
(i.e when click on circle , zooms) add timeout function recalculate text font size based on zoom.
settimeout(function() { d3.selectall("text").filter(function(d) { return d.parent === focus || this.style.display === "inline"; }).style("font-size", calculatetextfontsize);//calculate font }, 500)
your calculatetextfontsize
function this(i using real dom radius calculate font size):
var calculatetextfontsize = function(d) { var id = d3.select(this).text(); var radius = 0; if (d.fontsize){ //if fontsize calculated use that. return d.fontsize; } if (!d.computed ) { //if computed not present & store getcomputedtextlength() of text field d.computed = this.getcomputedtextlength(); if(d.computed != 0){ //if computed not 0 visual radius of dom var r = d3.selectall("#" + id).attr("r"); //if radius present in dom use if (r) { radius = r; } //calculate font size , store in object future d.fontsize = (2 * radius - 8) / d.computed * 24 + "px"; return d.fontsize; } } }
working code here
Comments
Post a Comment