Skip to content

Instantly share code, notes, and snippets.

@js418
Last active February 9, 2017 20:31
Show Gist options
  • Save js418/6e7f8c57e78002cf821dcd3b3a3e9c93 to your computer and use it in GitHub Desktop.
Save js418/6e7f8c57e78002cf821dcd3b3a3e9c93 to your computer and use it in GitHub Desktop.
Employed Industry Pie Chart for high school graduate male
license: mit
Industry population
Agricultural, forestry, fishing, and hunting 628
Mining 305
Construction 3283
Manufacturing 3368
Wholesale and retail trade 3211
Transportation and utilities 2146
Information 251
Financial activities 593
Professional and business services 1620
Educational and health services 1022
Leisure and hospitality 1432
Other services 1031
Public administration 653
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>D3 Example</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/d3-legend/1.1.0/d3-legend.js"></script>
<link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<style>
.axis text {
font-family: 'Open Sans', sans-serif;
font-size: 20pt;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.color-legend text {
font-family: 'Open Sans', sans-serif;
font-size: 10pt;
}
</style>
</head>
<body>
<script>
var outerWidth = 960;
var outerHeight = 500;
var margin = { left: 11, top: 33, right: 377, bottom: 88 };
var radiusMax = 180;
var xColumn = "name";
var sliceSizeColumn = "population";
var colorColumn = "Industry";
var innerWidth = outerWidth - margin.left - margin.right;
var innerHeight = outerHeight - margin.top - margin.bottom;
var svg = d3.select("body").append("svg")
.attr("width", outerWidth)
.attr("height", outerHeight);
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var xAxisG = g.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + innerHeight + ")");
var pieG = g.append("g");
var colorLegendG = g.append("g")
.attr("class", "color-legend")
.attr("transform", "translate(595, 20)");
var xScale = d3.scale.ordinal().rangePoints([0, innerWidth]);
var colorScale = d3.scale.category10();
var xAxis = d3.svg.axis().scale(xScale).orient("bottom")
.outerTickSize(0);
var pie = d3.layout.pie();
var arc = d3.svg.arc();
arc.outerRadius(radiusMax);
arc.innerRadius(0);
var colorLegend = d3.legend.color()
.scale(colorScale)
.shapePadding(3)
.shapeWidth(20)
.shapeHeight(20)
.labelOffset(4);
function render(data){
xScale.domain(data.map( function (d){ return d[xColumn]; }));
colorScale.domain(data.map(function (d){ return d[colorColumn]; }));
pie.value(function(d) { return d[sliceSizeColumn]; });
xAxisG.call(xAxis);
var pieData = pie(data);
pieG.attr("transform", "translate(" + innerWidth / 2 + "," + innerHeight / 2 + ")");
var slices = pieG.selectAll("path").data(pieData);
slices.enter().append("path");
slices
.attr("d", arc)
.attr("fill", function (d){ return colorScale(d.data[colorColumn]); });
slices.exit().remove();
colorLegendG.call(colorLegend);
}
function type(d){
d.name = "high school graduate male";
d.population = +d.population;
return d;
}
d3.csv("highschool_industry_men.csv", type, render);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment