Skip to content

Instantly share code, notes, and snippets.

@aaizemberg
Created November 4, 2015 18:40
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 aaizemberg/381f050ef4296db37737 to your computer and use it in GitHub Desktop.
Save aaizemberg/381f050ef4296db37737 to your computer and use it in GitHub Desktop.
square pie chart ( d3.js )
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
</head>
<body>
<script>
var width = 200, height = 200, radius = Math.min(width, height) / 1.34;
// var width = 200, height = 200, radius = Math.min(width, height) / 2;
var c10 = d3.scale.category10();
var arc = d3.svg.arc()
.outerRadius(radius - 10)
.innerRadius(0);
var data = [
["de",1968],
["la",1407],
["el",1246],
["en",1066],
["a",1044],
["y",687]
/* ["un",580],
["del",504],
["por",471],
["los",437] */
];
var pie = d3.layout.pie()
.sort(null)
.value(function(d) { return d[1]; });
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var g = svg.selectAll(".arc")
.data(pie(data))
.enter().append("g")
.attr("class", "arc");
g.append("path")
.attr("d", arc)
.attr("stroke", "#fff")
.style("fill", function(d,i) { return c10(i); });
g.append("text")
.attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
.attr("dy", ".35em")
.style("text-anchor", "middle")
.attr("fill","white")
.text( function(d) {return d.data[0];} );
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment