A minimalist donut chart rendered to Canvas using d3-shape.
-
-
Save akngs/f6ef1bb336e107a3d263 to your computer and use it in GitHub Desktop.
Canvas Donut for retina display
This file contains hidden or 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
age | population | |
---|---|---|
<5 | 2704659 | |
5-13 | 4499890 | |
14-17 | 2159981 | |
18-24 | 3853788 | |
25-44 | 14106543 | |
45-64 | 8819342 | |
≥65 | 612463 |
This file contains hidden or 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> | |
<meta charset="utf-8"> | |
<canvas width="1920" height="1000" style="width: 960px; height: 500px"></canvas> | |
<script src="//d3js.org/d3.v4.0.0-alpha.4.min.js"></script> | |
<script> | |
var canvas = document.querySelector("canvas"), | |
context = canvas.getContext("2d"); | |
var width = canvas.width, | |
height = canvas.height, | |
radius = Math.min(width, height) / 2; | |
var colors = ["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]; | |
var arc = d3.arc() | |
.outerRadius(radius - 10) | |
.innerRadius(radius - 70) | |
.context(context); | |
var labelArc = d3.arc() | |
.outerRadius(radius - 40) | |
.innerRadius(radius - 40) | |
.context(context); | |
var pie = d3.pie() | |
.sort(null) | |
.value(function(d) { return d.population; }); | |
context.translate(width / 2, height / 2); | |
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) { | |
context.beginPath(); | |
arc(d); | |
context.fillStyle = colors[i]; | |
context.fill(); | |
}); | |
context.beginPath(); | |
arcs.forEach(arc); | |
context.strokeStyle = "#fff"; | |
context.stroke(); | |
context.textAlign = "center"; | |
context.textBaseline = "middle"; | |
context.font = "24px arial"; | |
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