Skip to content

Instantly share code, notes, and snippets.

@johangithub
Created February 26, 2016 01:39
Show Gist options
  • Save johangithub/2a799c2d9c6abe17a04c to your computer and use it in GitHub Desktop.
Save johangithub/2a799c2d9c6abe17a04c to your computer and use it in GitHub Desktop.
Degree breakdown by AFSC using Hierarchical Barchart
{ "name": "ALL (1129)", "size":1129, "children": [
{ "name": "13M (15)", "size":15, "children": [
{ "name": "Finance (1)", "size":1},
{ "name": "Health Care Sciences (1)", "size":1},
{ "name": "History (1)", "size":1},
{ "name": "International Relations (1)", "size":1},
{ "name": "Management (8)", "size":8},
{ "name": "Sociology (1)", "size":1},
{ "name": "Technology, Other (2)", "size":2}]},
{ "name": "13N (145)", "size":145, "children": [
{ "name": "Aeronautical Engineering (1)", "size":1},
{ "name": "Aerospace Engineering (4)", "size":4},
{ "name": "Anthropology (1)", "size":1},
{ "name": "Behavioral Science (4)", "size":4},
{ "name": "Biochemistry (7)", "size":7},
{ "name": "Biology (10)", "size":10},
{ "name": "Chemical Engineering (1)", "size":1},
{ "name": "Civil Engineering (4)", "size":4},
{ "name": "Computer Engineering (2)", "size":2},
{ "name": "Computer Science (7)", "size":7},
{ "name": "Criminal Justice (10)", "size":10},
{ "name": "Economics (2)", "size":2},
{ "name": "Electrical Engineering (1)", "size":1},
{ "name": "Engineering, Other (1)", "size":1},
{ "name": "English (2)", "size":2},
{ "name": "Finance (2)", "size":2},
{ "name": "Foreign Area Studies, Language (10)", "size":10},
{ "name": "General/Liberal Studies (8)", "size":8},
{ "name": "Geospatial Science (1)", "size":1},
{ "name": "Health Care Science (1)", "size":1},
{ "name": "Health Care Sciences (5)", "size":5},
{ "name": "History (5)", "size":5},
{ "name": "International Relations (4)", "size":4},
{ "name": "Legal Studies (2)", "size":2},
{ "name": "Management (10)", "size":10},
{ "name": "Mathematics (5)", "size":5},
{ "name": "Mechanical Engineering (10)", "size":10},
{ "name": "Meteorology (1)", "size":1},
{ "name": "Military Strategic Studies (2)", "size":2},
{ "name": "Physics (2)", "size":2},
{ "name": "Political Science (6)", "size":6},
{ "name": "Psychology (3)", "size":3},
{ "name": "Public Affairs, Communication (2)", "size":2},
{ "name": "Social Sciences (2)", "size":2},
{ "name": "Sociology (3)", "size":3},
{ "name": "Speech Pathology (1)", "size":1},
{ "name": "Systems Engineering (1)", "size":1},
{ "name": "Technology, Other (2)", "size":2}]},
{ "name": "13S (88)", "size":88, "children": [
{ "name": "Aerospace Engineering (12)", "size":12},
{ "name": "Astronomy (3)", "size":3},
{ "name": "Behavioral Science (1)", "size":1},
{ "name": "Biology (2)", "size":2},
{ "name": "Chemistry (1)", "size":1},
{ "name": "Civil Engineering (4)", "size":4},
{ "name": "Economics (1)", "size":1},
{ "name": "Electrical Engineering (1)", "size":1},
{ "name": "Engineering, Other (1)", "size":1},
{ "name": "Foreign Area Studies, Language (1)", "size":1},
{ "name": "General/Liberal Studies (1)", "size":1},
{ "name": "Geospatial Science (4)", "size":4},
{ "name": "Legal Studies (3)", "size":3},
{ "name": "Management (19)", "size":19},
{ "name": "Mathematics (4)", "size":4},
{ "name": "Mechanical Engineering (16)", "size":16},
{ "name": "Operations Research (2)", "size":2},
{ "name": "Physics (5)", "size":5},
{ "name": "Political Science (1)", "size":1},
{ "name": "Social Sciences (1)", "size":1},
{ "name": "Systems Engineering (5)", "size":5}]},
{ "name": "14N (144)", "size":144, "children": [
{ "name": "Aerospace Engineering (3)", "size":3},
{ "name": "Anthropology (1)", "size":1},
{ "name": "Behavioral Science (2)", "size":2},
{ "name": "Biology (3)", "size":3},
{ "name": "Chemistry (3)", "size":3},
{ "name": "Civil Engineering (1)", "size":1},
{ "name": "Criminal Justice (9)", "size":9},
{ "name": "Economics (2)", "size":2},
{ "name": "English (3)", "size":3},
{ "name": "Foreign Area Studies, Language (32)", "size":32},
{ "name": "General/Liberal Studies (4)", "size":4},
{ "name": "Geospatial Science (10)", "size":10},
{ "name": "Health Care Sciences (3)", "size":3},
{ "name": "History (12)", "size":12},
{ "name": "International Relations (12)", "size":12},
{ "name": "Management (3)", "size":3},
{ "name": "Mathematics (2)", "size":2},
{ "name": "Mechanical Engineering (4)", "size":4},
{ "name": "Meteorology (2)", "size":2},
{ "name": "Political Science (21)", "size":21},
{ "name": "Psychology (5)", "size":5},
{ "name": "Public Affairs, Communication (2)", "size":2},
{ "name": "Sociology (4)", "size":4},
{ "name": "Systems Engineering (1)", "size":1}]},
{ "name": "15W (25)", "size":25, "children": [
{ "name": "Chemistry (1)", "size":1},
{ "name": "Meteorology (24)", "size":24}]},
{ "name": "17D (202)", "size":202, "children": [
{ "name": "Aeronautical Engineering (1)", "size":1},
{ "name": "Aerospace Engineering (2)", "size":2},
{ "name": "Anthropology (1)", "size":1},
{ "name": "Behavioral Science (2)", "size":2},
{ "name": "Biochemistry (5)", "size":5},
{ "name": "Biology (9)", "size":9},
{ "name": "Chemistry (3)", "size":3},
{ "name": "Computer Engineering (21)", "size":21},
{ "name": "Computer Science (48)", "size":48},
{ "name": "Criminal Justice (6)", "size":6},
{ "name": "Economics (2)", "size":2},
{ "name": "Electrical Engineering (16)", "size":16},
{ "name": "Engineering, Other (3)", "size":3},
{ "name": "English (2)", "size":2},
{ "name": "Environmental Engineering (1)", "size":1},
{ "name": "Foreign Area Studies, Language (8)", "size":8},
{ "name": "General/Liberal Studies (6)", "size":6},
{ "name": "Health Care Science (4)", "size":4},
{ "name": "Health Care Sciences (4)", "size":4},
{ "name": "History (2)", "size":2},
{ "name": "Industrial Engineering (7)", "size":7},
{ "name": "International Relations (2)", "size":2},
{ "name": "Legal Studies (1)", "size":1},
{ "name": "Management (13)", "size":13},
{ "name": "Mathematics (6)", "size":6},
{ "name": "Mechanical Engineering (8)", "size":8},
{ "name": "Military Strategic Studies (1)", "size":1},
{ "name": "Physics (2)", "size":2},
{ "name": "Political Science (4)", "size":4},
{ "name": "Psychology (3)", "size":3},
{ "name": "Public Administration (1)", "size":1},
{ "name": "Public Affairs, Communication (1)", "size":1},
{ "name": "Social Sciences (2)", "size":2},
{ "name": "Sociology (1)", "size":1},
{ "name": "Speech Pathology (1)", "size":1},
{ "name": "Systems Engineering (2)", "size":2},
{ "name": "Technology, Other (1)", "size":1}]},
{ "name": "21X (72)", "size":72, "children": [
{ "name": "Aerospace Engineering (2)", "size":2},
{ "name": "Behavioral Science (3)", "size":3},
{ "name": "Biochemistry (1)", "size":1},
{ "name": "Biology (4)", "size":4},
{ "name": "Chemistry (1)", "size":1},
{ "name": "Criminal Justice (3)", "size":3},
{ "name": "Economics (4)", "size":4},
{ "name": "Engineering, Other (1)", "size":1},
{ "name": "English (2)", "size":2},
{ "name": "Environmental Sciences (1)", "size":1},
{ "name": "Finance (1)", "size":1},
{ "name": "Geospatial Science (1)", "size":1},
{ "name": "Health Care Science (1)", "size":1},
{ "name": "Health Care Sciences (2)", "size":2},
{ "name": "History (3)", "size":3},
{ "name": "International Relations (2)", "size":2},
{ "name": "Management (28)", "size":28},
{ "name": "Mathematics (2)", "size":2},
{ "name": "Mechanical Engineering (4)", "size":4},
{ "name": "Meteorology (1)", "size":1},
{ "name": "Operations Research (1)", "size":1},
{ "name": "Political Science (1)", "size":1},
{ "name": "Public Administration (1)", "size":1},
{ "name": "Systems Engineering (1)", "size":1},
{ "name": "Technology, Other (1)", "size":1}]},
{ "name": "31P (14)", "size":14, "children": [
{ "name": "Biology (1)", "size":1},
{ "name": "Criminal Justice (4)", "size":4},
{ "name": "Foreign Area Studies, Language (1)", "size":1},
{ "name": "General/Liberal Studies (1)", "size":1},
{ "name": "Health Care Science (1)", "size":1},
{ "name": "History (1)", "size":1},
{ "name": "Legal Studies (1)", "size":1},
{ "name": "Management (2)", "size":2},
{ "name": "Psychology (1)", "size":1},
{ "name": "Sociology (1)", "size":1}]},
{ "name": "32E (58)", "size":58, "children": [
{ "name": "Architectural Engineering (1)", "size":1},
{ "name": "Civil Engineering (29)", "size":29},
{ "name": "Electrical Engineering (2)", "size":2},
{ "name": "Environmental Engineering (8)", "size":8},
{ "name": "Industrial Engineering (1)", "size":1},
{ "name": "Mechanical Engineering (17)", "size":17}]},
{ "name": "35P (12)", "size":12, "children": [
{ "name": "Criminal Justice (1)", "size":1},
{ "name": "Foreign Area Studies, Language (1)", "size":1},
{ "name": "Health Care Science (1)", "size":1},
{ "name": "Political Science (2)", "size":2},
{ "name": "Public Affairs, Communication (7)", "size":7}]},
{ "name": "38P (27)", "size":27, "children": [
{ "name": "Behavioral Science (7)", "size":7},
{ "name": "Biology (1)", "size":1},
{ "name": "English (1)", "size":1},
{ "name": "Finance (1)", "size":1},
{ "name": "Foreign Area Studies, Language (4)", "size":4},
{ "name": "General/Liberal Studies (2)", "size":2},
{ "name": "Legal Studies (3)", "size":3},
{ "name": "Management (1)", "size":1},
{ "name": "Mathematics (4)", "size":4},
{ "name": "Political Science (1)", "size":1},
{ "name": "Public Administration (1)", "size":1},
{ "name": "Speech Pathology (1)", "size":1}]},
{ "name": "61X (46)", "size":46, "children": [
{ "name": "Aerospace Engineering (1)", "size":1},
{ "name": "Behavioral Science (5)", "size":5},
{ "name": "Biochemistry (4)", "size":4},
{ "name": "Chemistry (2)", "size":2},
{ "name": "Economics (1)", "size":1},
{ "name": "Engineering, Other (1)", "size":1},
{ "name": "Mathematics (6)", "size":6},
{ "name": "Mechanical Engineering (2)", "size":2},
{ "name": "Operations Research (9)", "size":9},
{ "name": "Physics (10)", "size":10},
{ "name": "Psychology (4)", "size":4},
{ "name": "Systems Engineering (1)", "size":1}]},
{ "name": "62E (137)", "size":137, "children": [
{ "name": "Aeronautical Engineering (11)", "size":11},
{ "name": "Aerospace Engineering (19)", "size":19},
{ "name": "Astronautical Engineering (6)", "size":6},
{ "name": "Chemical Engineering (5)", "size":5},
{ "name": "Chemistry (1)", "size":1},
{ "name": "Civil Engineering (5)", "size":5},
{ "name": "Computer Engineering (6)", "size":6},
{ "name": "Electrical Engineering (25)", "size":25},
{ "name": "Engineering, Other (2)", "size":2},
{ "name": "Industrial Engineering (1)", "size":1},
{ "name": "Mathematics (1)", "size":1},
{ "name": "Mechanical Engineering (46)", "size":46},
{ "name": "Systems Engineering (9)", "size":9}]},
{ "name": "63A (93)", "size":93, "children": [
{ "name": "Aerospace Engineering (6)", "size":6},
{ "name": "Astronomy (1)", "size":1},
{ "name": "Biochemistry (1)", "size":1},
{ "name": "Biology (3)", "size":3},
{ "name": "Chemical Engineering (1)", "size":1},
{ "name": "Chemistry (5)", "size":5},
{ "name": "Civil Engineering (4)", "size":4},
{ "name": "Criminal Justice (4)", "size":4},
{ "name": "Economics (5)", "size":5},
{ "name": "Engineering, Other (2)", "size":2},
{ "name": "Finance (1)", "size":1},
{ "name": "General/Liberal Studies (1)", "size":1},
{ "name": "Geospatial Science (3)", "size":3},
{ "name": "Health Care Sciences (1)", "size":1},
{ "name": "Industrial Engineering (2)", "size":2},
{ "name": "International Relations (1)", "size":1},
{ "name": "Management (27)", "size":27},
{ "name": "Marketing (3)", "size":3},
{ "name": "Mathematics (1)", "size":1},
{ "name": "Mechanical Engineering (9)", "size":9},
{ "name": "Meteorology (3)", "size":3},
{ "name": "Operations Research (4)", "size":4},
{ "name": "Physics (2)", "size":2},
{ "name": "Psychology (1)", "size":1},
{ "name": "Systems Engineering (2)", "size":2}]},
{ "name": "64P (27)", "size":27, "children": [
{ "name": "Anthropology (1)", "size":1},
{ "name": "Biochemistry (1)", "size":1},
{ "name": "Biology (6)", "size":6},
{ "name": "Civil Engineering (1)", "size":1},
{ "name": "Criminal Justice (1)", "size":1},
{ "name": "Foreign Area Studies, Language (1)", "size":1},
{ "name": "Geospatial Science (1)", "size":1},
{ "name": "History (2)", "size":2},
{ "name": "International Relations (3)", "size":3},
{ "name": "Legal Studies (2)", "size":2},
{ "name": "Management (1)", "size":1},
{ "name": "Marketing (1)", "size":1},
{ "name": "Political Science (1)", "size":1},
{ "name": "Psychology (3)", "size":3},
{ "name": "Public Administration (1)", "size":1},
{ "name": "Social Sciences (1)", "size":1}]},
{ "name": "65F (24)", "size":24, "children": [
{ "name": "Aerospace Engineering (1)", "size":1},
{ "name": "Economics (3)", "size":3},
{ "name": "Finance (3)", "size":3},
{ "name": "Industrial Engineering (1)", "size":1},
{ "name": "Management (13)", "size":13},
{ "name": "Marketing (1)", "size":1},
{ "name": "Mathematics (1)", "size":1},
{ "name": "Political Science (1)", "size":1}]}]}
<html>
<!DOCTYPE html>
<meta charset="utf-8">
<style>
text {
font: 10px sans-serif;
}
rect.background {
fill: white;
}
.axis {
shape-rendering: crispEdges;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 30, right: 120, bottom: 0, left: 200},
width = 1200 - margin.left - margin.right,
height = 1000 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var barHeight = 20;
var color = d3.scale.ordinal()
.range(["steelblue", "#ccc"]);
var duration = 750,
delay = 25;
var partition = d3.layout.partition()
.value(function(d) { return d.size; });
var xAxis = d3.svg.axis()
.scale(x)
.orient("top");
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", up);
svg.append("g")
.attr("class", "x axis");
svg.append("g")
.attr("class", "y axis")
.append("line")
.attr("y1", "100%");
d3.json("afsc_deg_data.json", function(error, root) {
if (error) throw error;
partition.nodes(root);
x.domain([0, root.value]).nice();
down(root, 0);
});
function down(d, i) {
if (!d.children || this.__transition__) return;
var end = duration + d.children.length * delay;
// Mark any currently-displayed bars as exiting.
var exit = svg.selectAll(".enter")
.attr("class", "exit");
// Entering nodes immediately obscure the clicked-on bar, so hide it.
exit.selectAll("rect").filter(function(p) { return p === d; })
.style("fill-opacity", 1e-6);
// Enter the new bars for the clicked-on data.
// Per above, entering bars are immediately visible.
var enter = bar(d)
.attr("transform", stack(i))
.style("opacity", 1);
// Have the text fade-in, even though the bars are visible.
// Color the bars as parents; they will fade to children if appropriate.
enter.select("text").style("fill-opacity", 1e-6);
enter.select("rect").style("fill", color(true));
// Update the x-scale domain.
x.domain([0, d3.max(d.children, function(d) { return d.value; })]).nice();
// Update the x-axis.
svg.selectAll(".x.axis").transition()
.duration(duration)
.call(xAxis);
// Transition entering bars to their new position.
var enterTransition = enter.transition()
.duration(duration)
.delay(function(d, i) { return i * delay; })
.attr("transform", function(d, i) { return "translate(0," + barHeight * i * 1.2 + ")"; });
// Transition entering text.
enterTransition.select("text")
.style("fill-opacity", 1);
// Transition entering rects to the new x-scale.
enterTransition.select("rect")
.attr("width", function(d) { return x(d.value); })
.style("fill", function(d) { return color(!!d.children); });
// Transition exiting bars to fade out.
var exitTransition = exit.transition()
.duration(duration)
.style("opacity", 1e-6)
.remove();
// Transition exiting bars to the new x-scale.
exitTransition.selectAll("rect")
.attr("width", function(d) { return x(d.value); });
// Rebind the current node to the background.
svg.select(".background")
.datum(d)
.transition()
.duration(end);
d.index = i;
}
function up(d) {
if (!d.parent || this.__transition__) return;
var end = duration + d.children.length * delay;
// Mark any currently-displayed bars as exiting.
var exit = svg.selectAll(".enter")
.attr("class", "exit");
// Enter the new bars for the clicked-on data's parent.
var enter = bar(d.parent)
.attr("transform", function(d, i) { return "translate(0," + barHeight * i * 1.2 + ")"; })
.style("opacity", 1e-6);
// Color the bars as appropriate.
// Exiting nodes will obscure the parent bar, so hide it.
enter.select("rect")
.style("fill", function(d) { return color(!!d.children); })
.filter(function(p) { return p === d; })
.style("fill-opacity", 1e-6);
// Update the x-scale domain.
x.domain([0, d3.max(d.parent.children, function(d) { return d.value; })]).nice();
// Update the x-axis.
svg.selectAll(".x.axis").transition()
.duration(duration)
.call(xAxis);
// Transition entering bars to fade in over the full duration.
var enterTransition = enter.transition()
.duration(end)
.style("opacity", 1);
// Transition entering rects to the new x-scale.
// When the entering parent rect is done, make it visible!
enterTransition.select("rect")
.attr("width", function(d) { return x(d.value); })
.each("end", function(p) { if (p === d) d3.select(this).style("fill-opacity", null); });
// Transition exiting bars to the parent's position.
var exitTransition = exit.selectAll("g").transition()
.duration(duration)
.delay(function(d, i) { return i * delay; })
.attr("transform", stack(d.index));
// Transition exiting text to fade out.
exitTransition.select("text")
.style("fill-opacity", 1e-6);
// Transition exiting rects to the new scale and fade to parent color.
exitTransition.select("rect")
.attr("width", function(d) { return x(d.value); })
.style("fill", color(true));
// Remove exiting nodes when the last child has finished transitioning.
exit.transition()
.duration(end)
.remove();
// Rebind the current parent to the background.
svg.select(".background")
.datum(d.parent)
.transition()
.duration(end);
}
// Creates a set of bars for the given data node, at the specified index.
function bar(d) {
var bar = svg.insert("g", ".y.axis")
.attr("class", "enter")
.attr("transform", "translate(0,5)")
.selectAll("g")
.data(d.children)
.enter().append("g")
.style("cursor", function(d) { return !d.children ? null : "pointer"; })
.on("click", down);
bar.append("text")
.attr("x", -6)
.attr("y", barHeight / 2)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d.name; });
bar.append("rect")
.attr("width", function(d) { return x(d.value); })
.attr("height", barHeight);
return bar;
}
// A stateful closure for stacking bars horizontally.
function stack(i) {
var x0 = 0;
return function(d) {
var tx = "translate(" + x0 + "," + barHeight * i * 1.2 + ")";
x0 += x(d.value);
return tx;
};
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment