Skip to content

Instantly share code, notes, and snippets.

@mostaphaRoudsari
Last active April 27, 2017 15:28
Show Gist options
  • Save mostaphaRoudsari/8c3425d937cb7a8f9ce34b5292f71278 to your computer and use it in GitHub Desktop.
Save mostaphaRoudsari/8c3425d937cb7a8f9ce34b5292f71278 to your computer and use it in GitHub Desktop.
simple pie chart for comfort
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v3.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
// doesn't look d3 fancy but this is all I need for this pie chart
var color = ['#b2182b','#ef8a62','#fddbc7','#f7f7f7','#d1e5f0','#67a9cf','#2166ac'];
var width = 90;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", width)
// add pie chart
var place_holder_data = [10, 10, 10, 10, 10, 10, 10];
var radius = width /2 ;
var pie = d3.layout.pie()(place_holder_data);
var arc = d3.svg.arc()
.innerRadius(radius / 2)
.outerRadius(radius - 5);
var pp = svg.append("g")
.attr("class", "pie")
.attr("transform", "translate(" + width / 2 +", " + width / 2 +")");
pp.selectAll("path")
.attr("class", "pie")
.data(pie).enter()
.append("path")
.attr("fill", function(d, i) { return color[i]; })
.attr("stroke", "black")
.attr("d", arc);
function update_pie(new_data) {
// in my case number of values are always the same
var pie = d3.layout.pie().value(function(d) { return d; })(new_data);
path = d3.select(".pie").selectAll("path").data(pie); // Compute the new angles
path.attr("d", arc); // redrawing the path
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment