Built with blockbuilder.org
Created
March 15, 2018 14:33
-
-
Save yanhann10/e32015457cc83500cb4323edbe11afa7 to your computer and use it in GitHub Desktop.
donut
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 |
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.v3.min.js"></script> | |
<style> | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
</style> | |
</head> | |
<body> | |
<script> | |
// Feel free to change or delete any of the code you see in this editor! | |
var w = 300; | |
var h = 300; | |
var dataset = [ 5, 10, 20, 45, 6, 25 ]; | |
var outerRadius = w / 2; | |
var innerRadius = 45; | |
var arc = d3.svg.arc() | |
.innerRadius(innerRadius) | |
.outerRadius(outerRadius); | |
var pie = d3.layout.pie(); | |
//Easy colors accessible via a 10-step ordinal scale | |
var color = d3.scale.category10(); | |
//Create SVG element | |
var svg = d3.select("body") | |
.append("svg") | |
.attr("width", w) | |
.attr("height", h); | |
//Set up groups | |
var arcs = svg.selectAll("g.arc") | |
.data(pie(dataset)) | |
.enter() | |
.append("g") | |
.attr("class", "arc") | |
.attr("transform", "translate(" + outerRadius + "," + outerRadius + ")"); | |
//Draw arc paths | |
arcs.append("path") | |
.attr("fill", function(d, i) { | |
return color(i); | |
}) | |
.attr("d", arc); | |
//Labels | |
arcs.append("text") | |
.attr("transform", function(d) { | |
return "translate(" + arc.centroid(d) + ")"; | |
}) | |
.attr("text-anchor", "middle") | |
.text(function(d) { | |
return d.value; | |
}); | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment