Sunburst in D3 v4.
Last active
May 30, 2017 17:04
-
-
Save oskwazir/ea54afbc150e529fa05b42eb49c30bae to your computer and use it in GitHub Desktop.
fresh block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit | |
border: no |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<style> | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
</style> | |
</head> | |
<body> | |
<svg></svg> | |
<script> | |
var nodeData = { | |
"name": "TOPICS", "children": [{ | |
"name": "Topic A", | |
"children": [{"name": "Sub A1", "size": 4}, {"name": "Sub A2", "size": 4}] | |
}, { | |
"name": "Topic B", | |
"children": [{"name": "Sub B1", "size": 3}, {"name": "Sub B2", "size": 3}, { | |
"name": "Sub B3", "size": 3}] | |
}, { | |
"name": "Topic C", | |
"children": [{"name": "Sub A1", "size": 4}, {"name": "Sub A2", "size": 4}] | |
}] | |
}; | |
// Variables | |
var width = 500; | |
var height = 500; | |
var radius = Math.min(width, height) / 2; | |
var color = d3.scaleOrdinal(d3.schemeCategory20b); | |
// Create primary <g> element | |
var g = d3.select('svg') | |
.attr('width', width) | |
.attr('height', height) | |
.append('g') | |
.attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')'); | |
// Data strucure | |
var partition = d3.partition() | |
.size([2 * Math.PI, radius]); | |
// Find data root | |
var root = d3.hierarchy(nodeData) | |
.sum(function (d) { return d.size}); | |
// Size arcs | |
partition(root); | |
var arc = 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 }); | |
// Put it all together | |
g.selectAll('path') | |
.data(root.descendants()) | |
.enter().append('path') | |
.attr("display", function (d) { return d.depth ? null : "none"; }) | |
.attr("d", arc) | |
.style('stroke', '#fff') | |
.style("fill", function (d) { return color((d.children ? d : d.parent).data.name); }); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment