JSFiddle - React, Tailwind, and code Playground
JavaScript
var w = 560,
h = 500,
r = 230,
color = d3.scale.category20c();
var partition = d3.layout.partition()
.size([2 * Math.PI, r])
.value(function (d) {
return d.size;
});
var arc = d3.svg.arc()
.startAngle(function (d) {
return d.x;
})
.endAngle(function (d) {
return d.x + d.dx;
})
.innerRadius(function (d) {
return d.y;
})
.outerRadius(function (d) {
return d.y + d.dy;
});
var svg = d3.select("body").append("svg:svg")
.attr("width", w)
.attr("height", h)
.append("svg:g")
.attr("transform", "translate(" + w / 2 + "," + h / 2 + ")");
group = svg.data([getData()])
.selectAll("path")
.data(partition.nodes)
.enter()
.append('svg:g');
path = group.append("svg:path")
.attr("d", arc)
.style("fill", function (d) {
if (d.children)
return color(d.name)
else
return "grey"
})
.attr("display", function (d) {
return d.depth ? null : "none";
}) // hide inner
.each(stash);
group.append("svg:text")
.attr("transform", function (d) {
return "rotate(" +
(d.x + d.dx / 2 - Math.PI / 2) / Math.PI * 180 + ")";
})
.attr("x", function (d) {
return d.y;
})
.attr("dx", "6") // margin
.attr("dy", ".35em") // vertical-align
.attr("display", function (d) {
return d.depth ? null : "none";
}) // hide inner
.text(function (d) {
return d.name;
});
// Stash the old values for transition.
function stash(d) {
d.x0 = d.x;
d.dx0 = d.dx;
}
function getData2() {
return {
"name": "Total",
"children": [{
"name": "Enrolled in school:",
"children": [{
"name": "Enrolled in nursery school, preschool",
"size": 86
}, {
"name": "Enrolled in kindergarten",
"size": 100
}, {
...