Skip to content

Instantly share code, notes, and snippets.

@chazcheadle
Created February 17, 2016 12:25
Show Gist options
  • Select an option

  • Save chazcheadle/a9b595a898ed285d6d1e to your computer and use it in GitHub Desktop.

Select an option

Save chazcheadle/a9b595a898ed285d6d1e to your computer and use it in GitHub Desktop.
Alberti Cipher Disk in D3
<html>
<head>
<title>Alberti Cipher Disk</title>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body>
<div id="chart"></div>
<script>
var width = 300,
height = 300,
radius = Math.min(width, height) / 2;
var dataset = "ABCDEFGILMNOPQRSTUXZ1234".split("");
var color = d3.scale.category20b();
var svg = d3.select('#chart')
.append('svg')
.attr('width', width)
.attr('height', height)
.append('g')
.attr('transform', 'translate(' + (width / 2) + ',' + (height / 2) + ')');
var arc = d3.svg.arc()
.outerRadius(radius - 10)
.innerRadius(radius - 40);
var pie = d3.layout.pie()
.value(function(d) {return 1;})
.startAngle(-7.5 * Math.PI/180)
.endAngle(-7.5 * Math.PI/180 + 2*Math.PI)
.padAngle(.01)
.sort(null);
var arcs = svg.selectAll('g.slice')
.data(pie(dataset))
.enter()
.append('svg:g')
.attr('class', 'slice');
arcs.append("svg.path")
.attr('d', arc)
.attr('fill', function(d, i) {
console.log(d);
return color(d.data);
})
.attr('id', function(d, i) { return 'slice-' + i;});
arcs.append("text")
.attr("transform", function(d) {
d.innerRadius = 0;
d.outerRadius = radius;
return "translate(" + arc.centroid(d) + ")";
})
// .attr("dy", -13)
.attr("text-anchor", "middle")
.text(function(d) {
return d.data;
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment