Skip to content

Instantly share code, notes, and snippets.

@tomgp
Last active January 4, 2016 20:19
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 tomgp/8673139 to your computer and use it in GitHub Desktop.
Save tomgp/8673139 to your computer and use it in GitHub Desktop.
Dot density circles

Distribute points in a circle at random and uniformly.

radius value
100 500
200 4000
75 1000
20 100
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.point{
fill-opacity:0.5;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 960, height = 700;
var color = d3.scale.category10();
function pointInCircle(r){
var theta = 2 * Math.PI * Math.random();
var rootR = r * Math.sqrt(Math.random());
return {
cx: rootR * Math.cos(theta),
cy: rootR * Math.sin(theta)
}
}
function drawDensityCircle(d,i){
var parent = d3.select(this);
console.log(i,color(i));
for(var dot = 0; dot < d.value; dot++){
var p = pointInCircle(parseInt(d.radius));
p.r = 2;
p['class'] = 'point';
p.fill = color(i);
parent.append('circle').attr(p);
}
}
d3.csv('data.csv',function(d){
d.map(function(d){
return {
radius: parseInt(d.radius),
value: parseInt(d.value)
}
})
d3.select('body')
.append('svg').attr({width:width, height:height})
.selectAll('.area')
.data(d)
.enter()
.append('g')
.attr( 'transform', function(d,i){
var xpos = (width - (d.radius * 2)) * Math.random() + d.radius;
var ypos = (height - (d.radius * 2)) * Math.random() + d.radius;
return 'translate(' + xpos + ',' + height * Math.random() + ')';
})
.each(drawDensityCircle);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment