Partition sunburst.
From D3 in Depth book by Peter Cook.
license: gpl-3.0 | |
height: 330 | |
border: no |
Partition sunburst.
From D3 in Depth book by Peter Cook.
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<head> | |
<title>Sunburst partition</title> | |
</head> | |
<style> | |
path { | |
fill: #333; | |
opacity: 0.3; | |
stroke: white; | |
} | |
</style> | |
<body> | |
<svg width="320" height="320"> | |
<g transform="translate(160, 160)"></g> | |
</svg> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script> | |
<script> | |
var data = { | |
"name": "A1", | |
"children": [ | |
{ | |
"name": "B1", | |
"children": [ | |
{ | |
"name": "C1", | |
"value": 100 | |
}, | |
{ | |
"name": "C2", | |
"value": 300 | |
}, | |
{ | |
"name": "C3", | |
"value": 200 | |
} | |
] | |
}, | |
{ | |
"name": "B2", | |
"value": 200 | |
} | |
] | |
}; | |
var radius = 150; | |
var partitionLayout = d3.partition() | |
.size([2 * Math.PI, radius]); | |
var arcGenerator = d3.arc() | |
.startAngle(function(d) { return d.x0; }) | |
.endAngle(function(d) { return d.x1; }) | |
.innerRadius(function(d) { return d.y0; }) | |
.outerRadius(function(d) { return d.y1; }); | |
var rootNode = d3.hierarchy(data) | |
rootNode.sum(function(d) { | |
return d.value; | |
}); | |
partitionLayout(rootNode); | |
d3.select('svg g') | |
.selectAll('path') | |
.data(rootNode.descendants()) | |
.enter() | |
.append('path') | |
.attr('d', arcGenerator); | |
</script> | |
</body> | |
</html> |