Create a gist now

Instantly share code, notes, and snippets.

@pshihn /README.md
Last active Feb 14, 2017

What would you like to do?
Sketchy Pie Chart
age population
<5 2704659
5-13 4499890
14-17 2159981
18-24 3853788
25-44 14106543
45-64 8819342
≥65 612463
<!DOCTYPE html>
<meta charset="utf-8">
<canvas></canvas>
<script src="//d3js.org/d3.v4.0.0-alpha.4.min.js"></script>
<script src="//roughjs.com/builds/rough.min.js"></script>
<script>
var canvas = document.querySelector("canvas"),
context = canvas.getContext("2d");
rough = new RoughCanvas(canvas, 960, 500);
var width = canvas.width,
height = canvas.height,
radius = Math.min(width, height) / 2;
rough.hachureGap = 1;
rough.strokeWidth = 0.5;
rough.fillWeight = 1;
var colors = ["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"];
var labelArc = d3.arc()
.outerRadius(radius - 40)
.innerRadius(radius - 40)
.context(context);
var pie = d3.pie()
.sort(null)
.value(function(d) { return d.population; });
d3.requestTsv("data.tsv", function(d) {
d.population = +d.population;
return d;
}, function(error, data) {
if (error) throw error;
var arcs = pie(data);
arcs.forEach(function(d, i) {
var a = rough.arc(width/2, height/2, radius * 2, radius * 2, d.startAngle - Math.PI/2, d.endAngle - Math.PI/2, true);
a.fill = colors[i];
a.hachureAngle = Math.random() * 360;
});
// draw labels
window.requestAnimationFrame(function() {
context.translate(width / 2, height / 2);
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = "#000";
arcs.forEach(function(d) {
var c = labelArc.centroid(d);
context.fillText(d.data.age, c[0], c[1]);
});
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment