Created
January 25, 2018 23:41
-
-
Save NathanLawrence/3fdf8c87b6d88ad6706337a1832b5d84 to your computer and use it in GitHub Desktop.
Multi-Ring Synchronized Value Pie
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<div id="chart"></div> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script> | |
var data = [ | |
{name: "USA", value: 40, secondary_value: 37, tertiary_value: 12}, | |
{name: "UK", value: 20, secondary_value: 96, tertiary_value: 16}, | |
{name: "Canada", value: 30, secondary_value: 64, tertiary_value: 18}, | |
{name: "Mexico", value: 10, secondary_value: 5, tertiary_value: 19}, | |
]; | |
var width = 400, | |
height = width, | |
radius = width/2; | |
var pie = d3.pie() | |
.value(function(d){ | |
return d.value; | |
}); | |
var circlesToMake = 3, | |
circleThickness = 50, | |
circleBuffer = 5, | |
circleDataLabels = ["value", "secondary_value", "tertiary_value"]; | |
var colorScale = d3.scaleOrdinal(d3.schemeCategory10); | |
var svg = d3.select("#chart").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var arcs = [], | |
canvases = [], | |
circles = [], | |
slicesets = [], | |
textsets = []; | |
for (var iter = 0; iter < circlesToMake; iter++){ | |
innerRadius = radius - (iter * (circleThickness + circleBuffer)) - circleThickness; | |
outerRadius = radius - (iter * (circleThickness + circleBuffer)); | |
arcs[iter] = d3.arc() | |
.outerRadius(outerRadius) | |
.innerRadius(innerRadius); | |
canvases[iter] = svg.append("g") | |
.attr("transform", `translate(${width-radius}, ${height-radius})`); | |
circles[iter] = canvases[iter].selectAll("path") | |
.data(pie(data)) | |
.enter() | |
.append("g") | |
.attr("class", "slice") | |
slicesets[iter] = canvases[iter].selectAll("g.slice") | |
.append("path") | |
.attr("fill", function(d, i){ | |
return colorScale(i); | |
}) | |
.attr("d", arcs[iter]); | |
textsets[iter] = canvases[iter].selectAll("g.slice") | |
.append("text") | |
.text(function(d, i){ | |
return d.data[circleDataLabels[iter]]; | |
}) | |
.attr("text-anchor", "middle") | |
.attr("fill", "white") | |
.attr("transform", function(d){ | |
d.innerRadius = innerRadius; | |
d.outerRadius = outerRadius; | |
return `translate(${arcs[iter].centroid(d)})`; | |
}); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment