Skip to content

Instantly share code, notes, and snippets.

@varqasim
Last active December 24, 2019 10:25
Show Gist options
  • Save varqasim/06681bcb7559a616366002f6ac2db009 to your computer and use it in GitHub Desktop.
Save varqasim/06681bcb7559a616366002f6ac2db009 to your computer and use it in GitHub Desktop.
Pie Chart
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
svg {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<svg></svg>
<script>
var colors = d3.scaleOrdinal(d3.schemeCategory10);
// var colors = d3.scaleOrdinal()
// .range(d3.schemeCategory10);
var data = [14, 23, 5, 13, 2, 20];
var svg = d3.select('svg')
.append('g')
.attr('transform', 'translate(200, 200)');
var pies = d3.pie()(data);
var arc = d3.arc()
.innerRadius(0)
.outerRadius(100)
.startAngle((d) => d.startAngle)
.endAngle((d) => d.endAngle);
svg.selectAll('path')
.data(pies).enter().append('path')
.attr('d', function(data){ return arc(data); })
.attr('fill', (d, i) => colors(d.value))
.attr('stroke', '#FFF');
console.log(svg)
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment