Skip to content

Instantly share code, notes, and snippets.

@js418
Last active February 9, 2017 20:34
Show Gist options
  • Save js418/48b4f99c3e2f2a566c32249c9436bc63 to your computer and use it in GitHub Desktop.
Save js418/48b4f99c3e2f2a566c32249c9436bc63 to your computer and use it in GitHub Desktop.
Employed Industry Pie Chart for high school graduate female
license: mit
Industry population
Agricultural, forestry, fishing, and hunting 165
Mining 21
Construction 237
Manufacturing 1375
Wholesale and retail trade 2554
Transportation and utilities 502
Information 141
Financial activities 1082
Professional and business services 1182
Educational and health services 3985
Leisure and hospitality 1465
Other services 962
Public administration 531
<!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 female";
d.population = +d.population;
return d;
}
d3.csv("highschool_industry_women.csv", type, render);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment