Created
March 20, 2014 07:39
-
-
Save anonymous/9659008 to your computer and use it in GitHub Desktop.
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
.arc path { | |
stroke: #F8F8F8; | |
stroke-width: 4px; | |
} |
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> | |
<html> | |
<head> | |
<meta name="description" content="D3js donut chart" /> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
</body> | |
</html> |
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
var width = 300, | |
height = 300, | |
radius = Math.min(width, height) / 2; | |
var color = d3.scale.ordinal() | |
.range(["#65A6BF", "#9AC4D5", "#CCE2EA"]); | |
var arc = d3.svg.arc() | |
.outerRadius(radius - 10) | |
.innerRadius(radius - 50); | |
// second arc for labels | |
var arc2 = d3.svg.arc() | |
.outerRadius(radius + 20) | |
.innerRadius(radius + 20); | |
var pie = d3.layout.pie() | |
.sort(null) | |
.value(function(d) { return d.songs; }); | |
var data = [ | |
{genre:'Otras', songs: 12}, | |
{genre: '2000s', songs: 42}, | |
{genre: '2010s', songs: 63} | |
]; | |
var svg = d3.select("body").append("svg") | |
.attr("width", width + 100) | |
.attr("height", height + 100) | |
.append("g") | |
.attr("transform", "translate(" + (width+100) / 2 + "," + (height + 100) / 2 + ")"); | |
data.forEach(function(d) { | |
d.songs = +d.songs; | |
}); | |
var g = svg.selectAll(".arc") | |
.data(pie(data)) | |
.enter().append("g") | |
.attr("class", "arc"); | |
g.append("path") | |
.attr("d", arc) | |
.style("fill", function(d) { return color(d.data.genre); }); | |
g.append("text") | |
.attr("transform", function(d) { return "translate(" + arc2.centroid(d) + ")"; }) | |
.attr("dy", ".35em") | |
.style("text-anchor", "middle") | |
.text(function(d) { return d.data.genre; }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment