Skip to content

Instantly share code, notes, and snippets.

@emagee
Created November 7, 2015 16:37
Show Gist options
  • Save emagee/c6f350062d6bedef01ea to your computer and use it in GitHub Desktop.
Save emagee/c6f350062d6bedef01ea to your computer and use it in GitHub Desktop.
Fatal occupational injuries, 2003–2014
occupation code occupation 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
15-000 Computer and mathematical 8 7 6 16 4 7 9 14 5 8 12 16
23-000 Legal 15 3 17 11 11 16 13 9 14 8 16 7
31-000 Healthcare support 20 12 19 23 14 19 25 17 23 12 18 14
19-000 Life - physical - and social science 20 25 17 28 20 23 17 27 23 21 25 18
21-000 Community and social services 29 43 25 33 28 34 32 25 23 39 20 31
25-000 Education - training - and library 30 27 26 23 22 27 22 30 37 25 20 27
13-000 Business and financial operations 32 27 36 39 31 24 26 26 30 22 21 25
29-000 Healthcare practitioners and technical 49 72 42 56 66 61 56 67 55 48 60 53
39-000 Personal care and service 53 56 62 57 63 57 48 66 71 65 48 61
17-000 Architecture and engineering 54 69 53 55 51 42 42 53 38 35 37 33
27-000 Arts - design - sports - and media 58 51 53 46 52 57 43 56 52 43 52 47
55-000 Military 62 71 48 57 64 57 75 46 57 50 71 55
35-000 Food preparation and serving-related 72 52 59 86 61 65 59 61 47 57 68 53
43-000 Office and administrative support 112 91 110 88 134 89 93 70 91 89 75 98
37-000 Building and grounds cleaning and maintenance 247 278 264 282 255 234 258 233 269 255 249 245
51-000 Production 283 290 272 285 266 267 197 225 222 221 213 206
33-000 Protective service 312 273 258 284 346 306 244 261 282 231 249 211
45-000 Farming - fishing - and forestry 305 285 325 297 258 286 239 276 261 260 232 253
41-000 Sales and related 348 355 324 316 327 275 290 280 240 225 220 234
49-000 Installation - maintenance - and repair 394 384 397 423 380 354 326 363 354 348 370 391
11-000 Management 635 637 570 550 525 553 538 545 467 445 408 427
47-000 Construction and extraction 1038 1138 1184 1273 1172 977 838 780 798 870 845 885
53-000 Transportation and material moving 1393 1511 1551 1501 1503 1376 1059 1160 1233 1247 1255 1289
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Marcia's Module 2 Stacked Area Chart</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
<link href='https://fonts.googleapis.com/css?family=Special+Elite' rel='stylesheet' type='text/css'>
<style type="text/css">
body {
margin: 0;
background-color: lightGray;
background-image: url(http://www.marciagray.net/images/px_by_Gre3g.png);
font-family: Helvetica, Arial, sans-serif;
font-size: 100%
}
header {
width: 700px;
margin-left: auto;
margin-right: auto;
margin-top: 20px;
padding: 10px 50px 5px 50px;
}
#container1 {
position: relative;
width: 805px;
height: auto;
margin-left: auto;
margin-right: auto;
margin-top: 20px;
padding: 20px 15px 10px 20px;
background-color: white;
opacity: 0.9;
/* box-shadow: 3px 3px 5px 6px #ccc; */
/* rounded corner assistance from cssround.com */
-webkit-border-radius: 22px 20px 20px 20px;
-moz-border-radius: 22px 20px 20px 20px;
border-radius: 22px 20px 20px 20px;
border:1px solid #000000;
}
#sideTable {
position: absolute;
width: 330px;
left: 485px;
top: 140px;
}
h1 {
font-family: 'Special Elite', cursive;
font-size: 36px;
margin: 0;
color: white;
opacity: 0.95;
font-weight: normal;
}
h2, h3, td, p, .tooltip {
color: black;
font-family: Frutiger, "Frutiger Linotype", Univers, Calibri, "Gill Sans", "Gill Sans MT", "Myriad Pro", Myriad, "DejaVu Sans Condensed", "Liberation Sans", "Nimbus Sans L", Tahoma, Geneva, "Helvetica Neue", Helvetica, Arial, sans-serif;
}
h2 {
font-size: 24px;
margin: 0;
}
h3 {
font-size: 14px;
margin-bottom: 2px;
padding: 3px;
line-height: 15px;
}
p {
/*font-family: Palatino Linotype’, Palatino, Palladio, ‘URW Palladio L’, ‘Book Antiqua’, Baskerville, ‘Bookman Old Style’, ‘Bitstream Charter’, ‘Nimbus Roman No9 L’, Garamond, ‘Apple Garamond’, ‘ITC Garamond Narrow’, ‘New Century Schoolbook’, ‘Century Schoolbook’, ‘Century Schoolbook L’, Georgia, serif; */
font-size: 13px;
line-height: 18px;
margin: 15px 0 10px 0;
}
a:link {
text-decoration: none;
color: #475140;
font-weight: bold;
}
a:hover {
text-decoration: underline;
color: #475140;
}
a:visited {
color:#475140;
}
a:active {
color: steelBlue;
}
svg {
background-color: white;
cursor: pointer;
margin-top: 0px;
}
g.bar text {
fill: white;
font-size: 10px;
font-weight: normal;
text-anchor: end;
opacity: 0;
letter-spacing: 1;
}
g.bar:hover rect {
fill: #b051ba;
}
g.bar:hover text {
opacity: 1;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
.x path, .x line {
stroke: lightgray;
fill: none;
}
.x text {
fill: gray;
}
path:hover {
fill: purple;
}
.axis path,
.axis line {
fill: none;
stroke: gray;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
fill: gray;
}
div.tooltip {
position: absolute;
text-align: center;
color: purple;
height: auto;
padding: 5px 6px 3px 6px;
font-size: 15px;
line-height: 15px;
background: white;
/*border: 2px solid purple;
border-radius: 8px; */
pointer-events: none;
top: 150px;
left: 245px;
width: 190px;
background: none;
}
tr {
height: 10px;
}
td {
font-size: 12px;
line-height: 14px;
border-collapse: collapse;
border-spacing: 0px;
height: 10px;
}
td.lastLine {
border-top: 1px solid gray;
padding-top: 3px;
margin-top: 1px;
}
.alignRight {
text-align: right;
}
.occupation::first-letter, .occupation span{
font-size: 13px;
vertical-align: baseline;
}
.computer::first-letter, .computer span {
color: #3182bd;
}
.legal::first-letter, .legal span {
color: #6baed6;
}
.healthcare::first-letter, .healthcare span {
color: #9ecae1;
}
.life::first-letter, .life span {
color: #c6dbef;
}
.education::first-letter, .education span {
color: #e6550d;
}
.business::first-letter, .business span {
color: #fd8d3c;
}
.community::first-letter, .community span {
color: #fdae6b;
}
.architecture::first-letter, .architecture span {
color: #fdd0a2;
}
.arts::first-letter, .arts span {
color: #31a354;
}
.healthcare2::first-letter, .healthcare2 span {
color: #74c476;
}
.personal::first-letter, .personal span {
color: #a1d99b;
}
.military::first-letter, .military span {
color: #c7e9c0;
}
.food::first-letter, .food span {
color: #756bb1;
}
.office::first-letter, .office span {
color: #9e9ac8;
}
.production::first-letter, .production span {
color: #bcbddc;
}
.building::first-letter, .building span {
color: #dadaeb;
}
.protective::first-letter, .protective span {
color: #636363;
}
.farming::first-letter, .farming span {
color: #969696;
}
.sales::first-letter, .sales span {
color: #bdbdbd;
}
.installation::first-letter, .installation span {
color: #d9d9d9;
}
.management::first-letter, .management span {
color: #3182bd;
}
.construction::first-letter, .construction span {
color: #6baed6;
}
.transportation::first-letter, .transportation span {
color: #9ecae1;
}
</style>
</head>
<body>
<header>
<h1>Dabble & Dawdle, Partners in Data</h1>
</header>
<div id="container1">
<h2>Fatal US occupational injuries by major occupational group, 2003&ndash;2014</h2>
<p>Each occupation catalogued by the US government is placed within one of 23 major groups in its Standard Occupational Classification. All 23 of these major groups are represented below. (Note that this chart should NOT be used to determine the most dangerous occupations!) &nbsp;&nbsp;SOURCE: <a href="http://www.bls.gov/iif/oshcfoi1.htm">US Bureau of Labor Statistics Census of Fatal Occupational Injuries</a></p>
<div id="sideTable">
<h3>Fatal US occupational injuries per year, <br /> 2003&ndash;2014 average</h3>
<table>
<tr>
<td class="occupation computer"><span>&#9724;&nbsp;</span>Computer and mathematical</td>
<td class="alignRight">9</td>
</tr>
<tr>
<td class="occupation legal"><span>&#9724;&nbsp;</span>Legal</td>
<td class="alignRight">12</td>
</tr>
<tr>
<td class="occupation healthcare"><span>&#9724;&nbsp;</span>Healthcare support</td>
<td class="alignRight">18</td>
</tr>
<tr>
<td class="occupation life"><span>&#9724;&nbsp;</span>Life, physical, and social sciences</td>
<td class="alignRight">22</td>
</tr>
<tr>
<td class="occupation education"><span>&#9724;&nbsp;</span>Education, training, and library</td>
<td class="alignRight">26</td>
</tr>
<tr>
<td class="occupation business"><span>&#9724;&nbsp;</span>Business and financial operations</td>
<td class="alignRight">28</td>
</tr>
<tr>
<td class="occupation community"><span>&#9724;&nbsp;</span>Community and social services</td>
<td class="alignRight">30</td>
</tr>
<tr>
<td class="occupation architecture"><span>&#9724;&nbsp;</span>Architecture and engineering</td>
<td class="alignRight">47</td>
</tr>
<tr>
<td class="occupation arts"><span>&#9724;&nbsp;</span>Arts, design, sports, and media</td>
<td class="alignRight">51</td>
</tr>
<tr>
<td class="occupation healthcare2"><span>&#9724;&nbsp;</span>Healthcare practitioners and technical</td>
<td class="alignRight">57</td>
</tr>
<tr>
<td class="occupation personal"><span>&#9724;&nbsp;</span>Personal care and service</td>
<td class="alignRight">59</td>
</tr>
<tr>
<td class="occupation military"><span>&#9724;&nbsp;</span>Military</td>
<td class="alignRight">60</td>
</tr>
<tr>
<td class="occupation food"><span>&#9724;&nbsp;</span>Food preparation and serving-related</td>
<td class="alignRight">61</td>
</tr>
<tr>
<td class="occupation office"><span>&#9724;&nbsp;</span>Office and administrative support</td>
<td class="alignRight">95</td>
</tr>
<tr>
<td class="occupation production"><span>&#9724;&nbsp;</span>Production</td>
<td class="alignRight">246</td>
</tr>
<tr>
<td class="occupation building"><span>&#9724;&nbsp;</span>Building and grounds cleaning and maintenance</td>
<td class="alignRight">256</td>
</tr>
<tr>
<td class="occupation protective"><span>&#9724;&nbsp;</span>Protective service</td>
<td class="alignRight">271</td>
</tr>
<tr>
<td class="occupation farming"><span>&#9724;&nbsp;</span>Farming, fishing, and forestry</td>
<td class="alignRight">273</td>
</tr>
<tr>
<td class="occupation sales"><span>&#9724;&nbsp;</span>Sales and related</td>
<td class="alignRight">286</td>
</tr>
<tr>
<td class="occupation installation"><span>&#9724;&nbsp;</span>Installation, maintenance,and repair</td>
<td class="alignRight">374</td>
</tr>
<tr>
<td class="occupation management"><span>&#9724;&nbsp;</span>Management</td>
<td class="alignRight">525</td>
</tr>
<tr>
<td class="occupation construction"><span>&#9724;&nbsp;</span>Construction and extraction</td>
<td class="alignRight">983</td>
</tr>
<tr>
<td class="occupation transportation"><span>&#9724;&nbsp;</span>Transportation and material moving</td>
<td class="alignRight">1340</td>
</tr>
<tr>
<td class="occupation lastLine"><span>&#9724;&nbsp;</span><strong>Total average fatalities, all occupations</strong></td>
<td class="alignRight lastLine"><strong>5129</strong></td>
</tr>
</table>
</div>
</div>
<script type="text/javascript">
//Set up stack method
var stack = d3.layout.stack()
.values(function(d) {
return d.fatalities;
})
.order("reverse");
//Width, height, padding
var w = 485;
var h = 550;
var padding = [ 20, 10, 50, 40 ]; //Top, right, bottom, left
//Set up date format function (years)
var dateFormat = d3.time.format("%Y");
//Set up scales
var xScale = d3.time.scale()
.range([ padding[3], w - padding[1] - padding[3] ]);
var yScale = d3.scale.linear()
.range([ padding[0], h - padding[2] ]);
//Configure axis generators
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(15)
.tickFormat(function(d) {
return dateFormat(d);
});
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(5);
//Configure area generator
var area = d3.svg.area()
.x(function(d) {
return xScale(dateFormat.parse(d.x));
})
.y0(function(d) {
return yScale(d.y0); //Updated
})
.y1(function(d) {
return yScale(d.y0 + d.y); //Updated
});
//Easy colors accessible via a 10-step ordinal scale
var color = d3.scale.category20c();
/*var color = d3.scale.ordinal().range(["#b87228","#b1712c","#ab7131","#a57035","#9f703a","#a996f3e","#8d6e47","#876e4c","#816d50","#7b6d55","#756c59","#56f6c5e","#6f6c5e","#6f6c5e","#696b63","#636b67","#5d6a6c","#576a70","#516975", "#4b6979", "#45687e","#3f6882","#396787","#33678b", "#2d6690", "#276695"]);*/
//Create the empty SVG image
var svg = d3.select("#container1")
.append("svg")
.attr("width", w)
.attr("height", h);
//Load data
d3.csv("fatalitites_by_occupation-d2.csv", function(data) {
//Uncomment to log the newly loaded data to the console
//console.log(data);
//Data is loaded in, but we need to restructure it.
//Remember, each line requires an array of x/y pairs;
//that is, an array of arrays, like so:
//
// [ [x: 1, y: 1], [x: 2, y: 2], [x: 3, y: 3] ]
//
//Our x value will be the year, and y will be the amount
//of CO2. We also need to know which country belongs to
//each line, so we will build an array of objects that is
//structured like this:
//
/*
[
{
country: "Australia",
emissions: [
{ x: "1961", y: 90589.568 },
{ x: "1962", y: 94912.961 },
{ x: "1963", y: 101029.517 },
]
},
{
country: "Bermuda",
emissions: [
{ x: "1961", y: 176.016 },
{ x: "1962", y: 157.681 },
{ x: "1963", y: 150.347 },
]
},
]
*/
//
//Note that this is an array of objects. Each object
//contains two values, 'country' and 'emissions'.
//The 'emissions' value is itself an array, containing
//more objects, each one holding x and y values.
//
//The x (year) values have to be strings in this case,
//because the date format function expects a string
//to parse into a Date object.
//New array with all the years, for referencing later
var years = ["2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014"];
//Create a new, empty array to hold our restructured dataset
var dataset = [];
//Loop once for each row in data
for (var i = 0; i < data.length; i++) {
//Create new object with this country's name and empty array
dataset[i] = {
occupationName: data[i].occupation,
fatalities: []
};
//Loop through all the years
for (var j = 0; j < years.length; j++) {
//Default value, used in case no value is present
var amount = null;
// If value is not empty
if (data[i][years[j]]) {
amount = +data[i][years[j]];
}
//Add a new object to the emissions data array
//for this country
dataset[i].fatalities.push({
x: years[j],
y: amount
});
}
}
//Stack the data!
stack(dataset);
//Uncomment to log the original data to the console
//console.log(data);
//Uncomment to log the newly restructured dataset to the console
//console.log(dataset);
//Now that the data is ready, we can check its
//min and max values to set our scales' domains!
xScale.domain([
d3.min(years, function(d) {
return dateFormat.parse(d);
}),
d3.max(years, function(d) {
return dateFormat.parse(d);
})
]);
//Need to recalcluate the max value for yScale
//differently, now that everything is stacked.
//Loop once for each year, and get the total value
//of CO2 for that year.
var totals = [];
for (i = 0; i < years.length; i++) {
totals[i] = 0;
for (j = 0; j < dataset.length; j++) {
totals[i] += dataset[j].fatalities[i].y;
}
}
yScale.domain([ d3.max(totals), 0 ]);
//Areas
//
//Now that we are creating multiple paths, we can use the
//selectAll/data/enter/append pattern to generate as many
//as needed.
//Make a path for each country
var paths = svg.selectAll("path")
.data(dataset)
.enter()
.append("path")
.attr("class", "area")
.attr("d", function(d) {
//Calculate path based on only d.emissions array,
//not all of d (which would include the country name)
return area(d.fatalities);
})
.attr("stroke", "none")
.attr("fill", function(d, i) {
return color(i);
});
// Create tooltips
var toolTipDiv = d3.select("#container1").append("div")
.attr("class", "tooltip")
//.style("opacity", 0)
.html("&rtrif;&nbsp;Hover over an area to see which occupational group it represents.");
paths.on("mouseover", function(s) {
toolTipDiv.transition()
.duration(150)
.style("opacity", .95)
toolTipDiv.html(s.occupationName)
//.style("left", (d3.event.pageX -60) + "px")
//.style("top", (d3.event.pageY -60) + "px");
})
.on("mouseout", function(d) {
toolTipDiv
.transition()
.duration(300);
toolTipDiv.html("&rtrif;&nbsp;Hover over an area to see which occupational group it represents.")
.style("opacity", 1)
;
});
//Create axes
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + padding[3] + ",0)")
.call(yAxis);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment