Skip to content

Instantly share code, notes, and snippets.

@d3indepth
Last active August 2, 2017 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d3indepth/2b928885e915471e5c12d2b8cea948c9 to your computer and use it in GitHub Desktop.
Save d3indepth/2b928885e915471e5c12d2b8cea948c9 to your computer and use it in GitHub Desktop.
Partition sunburst
license: gpl-3.0
height: 330
border: no
<!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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment