Skip to content

Instantly share code, notes, and snippets.

@x3ro
Forked from mbostock/.block
Created November 3, 2012 00: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 x3ro/4005265 to your computer and use it in GitHub Desktop.
Save x3ro/4005265 to your computer and use it in GitHub Desktop.
Donut Chart

This donut chart is constructed from a CSV file storing the populations of various age groups. The chart employs a number of D3 features:

age population
javascript 227054
php 242085
c 62980
c# 319080
c++ 133238
ruby 48683
python 115992
java 262888
delphi 15613
io 2936
shell 9876
coldfusion 4074
powershell 6008
erlang 2471
perl 19140
assembly 5629
groovy 4328
matlab 10133
scheme 1685
lua 2332
f# 3633
applescript 1528
actionscript 5571
ocaml 1024
xquery 823
fortran 1209
haskell 7273
boo 95
autohotkey 242
scala 9633
smalltalk 430
verilog 337
eiffel 27
d 707
haxe 156
r 13936
tcl 953
rebol 208
arduino 808
ada 327
clojure 3370
objective-c 89915
objective-j 67
parrot 17
max 634
self 265
prolog 1758
puppet 130
vhdl 413
elixir 74
nemerle 43
arc 74
turing 17
scilab 45
go 942
factor 102
ioke 3
ecl 14
vala 147
coq 60
coffeescript 1643
racket 589
rust 15
nu 4
mirah 11
supercollider 9
fantom 4
opa 121
dart 122
kotlin 12
dylan 5
ceylon 5
gosu 4
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.arc path {
stroke: #fff;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 1000,
height = 1000,
radius = Math.min(width, height) / 2;
var color = d3.scale.ordinal()
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
//.range(["#ff0000", "#00ff00", "#0000ff", "#ffff00", "#ff00ff"]);
/*var arc = d3.svg.arc()
.outerRadius(radius - 10)
.innerRadius(radius - 70);
var arc2 = d3.svg.arc()
.outerRadius(radius - radius * (1.0/5))
.innerRadius(radius - radius * (1.0/5) - 10);
*/
var pie = d3.layout.pie()
.sort(null)
.value(function(d) { return d.population; });
var _svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
/*var svg = _svg
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var svg2 = _svg
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
*/
/*d3.csv("data.csv", function(error, data) {
data.forEach(function(d) {
d.population = +d.population;
});
var g = svg.selectAll(".arc")
.data(pie(data))
.enter().append("g")
.attr("class", "arc");
var g2 = svg2.selectAll(".arc")
.data(pie(data))
.enter().append("g")
.attr("class", "arc");
g2.append("path")
.attr("d", arc2)
.style("fill", function(d) { return color(d.data.age); });
g.append("path")
.attr("d", arc)
.style("fill", function(d) { return color(d.data.age); });
g.append("text")
.attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
.attr("dy", ".35em")
.style("text-anchor", "middle")
.text(function(d) { return d.data.age; });
});*/
function draw_pie(svg, data, radius, thickness, gap, limit, step, questionCounts, callback) {
if(data.length === 0) {
callback(questionCounts);
return;
}
if(radius < thickness) {
thickness = radius;
}
var next_total = 0;
var current_total_count = 0;
var current = data.filter(function(el) {
if(el.percentage >= limit) {
current_total_count += el.population;
return true;
} else {
next_total += el.percentage;
return false;
}
});
questionCounts.push(current_total_count);
var next = data.filter(function(el) {
if(el.percentage < limit) {
el.percentage = el.percentage / next_total;
return true;
} else {
return false;
}
});
//percentages.push(1 - next_total);
var arc = d3.svg.arc()
.outerRadius(radius)
.innerRadius(radius - thickness);
// Outer <g>
var og = svg
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
// Inner <g>
var ig = og.selectAll(".arc")
.data(pie(current))
.enter().append("g")
.attr("class", "arc");
ig.append("path")
.attr("d", arc)
.style("fill", function(d) {
var hsl = d3.hsl(color(d.data.age));
hsl.s = hsl.s * (1 - (0.11 * step))
return hsl;
});
ig.append("text")
.attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
.attr("dy", ".35em")
.style("text-anchor", "middle")
.text(function(d) { return d.data.age; });
draw_pie(svg, next, radius - thickness - gap, thickness, gap, limit, step + 1, questionCounts, callback)
}
d3.csv("data.csv", function(error, data) {
var total = 0;
data.forEach(function(d) {
d.population = +d.population;
total += d.population;
});
data.forEach(function(d) {
d.percentage = d.population / total;
});
draw_pie(_svg, data, radius, 50, 5, 0.04, 0, [], function(counts) {
var total = counts.reduce(function(a,b) { return a + b });
var percentages = counts.map(function(a) { return a / total; });
console.log(percentages);
percentages.forEach(function(a, i) {
var val = (a*100).toFixed(4) + "%";
if(a < 0.001) {
val = (a*1000).toFixed(4) + "‰";
}
_svg.append("text")
.attr("transform", function(d) { return "translate(0,0)"; })
.attr("dy", i+1 + "em")
.style("text-anchor", "left")
.text("Circle " + (i+1) + ": " + val);
});
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment