'd3-circle-text' for scalable package-circle
The 'd3-circle-text' plugin works fine on a static circular package (thanks a lot to musically-ut for the input https://github.com/musically-ut/d3-circle-text ). However, on a scalable ring package, the labels fly around the place (in the fiddle, they stay static without moving through the zoom).
Is it possible to enlarge circle-text using circles? (If the plugin doesn't scale, that's fine. I'll try a different labeling approach.)
Here's the section of code I'm working on:
////////////Circle text
var circleText = d3.circleText()
.radius(function(d) {
return d.r - 5;
})
.value(function(d) {
return d.key; //Get lables
})
.method('align')
.spacing('exact')
.precision(0.1)
.fontSize('100%');
var gTexts = svg.selectAll('g.label')
.data(pack.nodes) //Returns names
.enter()
.append('g')
.classed('label', true)
.attr('transform', function(d) {
return 'translate(' + d.x + ',' + d.y + ')';
});
/*.attr("x", function (d) { return d.x; })
.attr("y", function (d) { return d.y; }); */ An attempt - not working
/*.attr("cx", function (d) { return d.x; })
.attr("cy", function (d) { return d.y; }); */ An attempt - not working
//Only put on 'parent' circles
gTexts.filter(function(d) {
return !!d.children;
})
.call(circleText)
//.style('fill', 'white');
Here's the complete fiddle: http://jsfiddle.net/cajmcmahon/9a5xaovv/1/
Thanks for any help.
+3
source to share
1 answer
I updated the plugin to musically-ut / d3-circle-text
Layout generation has been simplified and now handles transitions correctly.
Updated fiddle: http://jsfiddle.net/nxmkoo95/
Notable changes
- Raised the definition
circleText
to the top level and removed the call.precision
. - A class is used
.leaf-label
to identify text labels that needed to be manually moved. - Added a call to update the radius function
circleText
and movedg.label
to the desired location.
function zoom(d, i) {
var k = r / d.r / 2;
x.domain([d.x - d.r, d.x + d.r]);
y.domain([d.y - d.r, d.y + d.r]);
var t = svg.transition()
.duration(d3.event.altKey ? 7500 : 750);
t.selectAll("circle")
.attr("cx", function(d) {
return x(d.x);
})
.attr("cy", function(d) {
return y(d.y);
})
.attr("r", function(d) {
return k * d.r;
});
circleText.radius(function (d) { return k * d.r - 5.0; });
t.selectAll('g.label')
.attr('transform', function (d) {
return "translate(" + x(d.x) + ',' + y(d.y) + ')';
})
.filter(function (d) { return !!d.children; })
.call(circleText);
t.selectAll(".leaf-label")
.attr("x", function(d) {
return x(d.x);
})
.attr("y", function(d) {
return y(d.y);
})
.style("opacity", function(d) { return k * d.r > 20 ? 1 : 0; });
node = d;
d3.event.stopPropagation();
}
+2
source to share