D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
emagee
Full window
Github gist
Fatal occupational injuries, 2003–2014
<!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(https://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–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!) SOURCE: <a href="https://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–2014 average</h3> <table> <tr> <td class="occupation computer"><span>◼ </span>Computer and mathematical</td> <td class="alignRight">9</td> </tr> <tr> <td class="occupation legal"><span>◼ </span>Legal</td> <td class="alignRight">12</td> </tr> <tr> <td class="occupation healthcare"><span>◼ </span>Healthcare support</td> <td class="alignRight">18</td> </tr> <tr> <td class="occupation life"><span>◼ </span>Life, physical, and social sciences</td> <td class="alignRight">22</td> </tr> <tr> <td class="occupation education"><span>◼ </span>Education, training, and library</td> <td class="alignRight">26</td> </tr> <tr> <td class="occupation business"><span>◼ </span>Business and financial operations</td> <td class="alignRight">28</td> </tr> <tr> <td class="occupation community"><span>◼ </span>Community and social services</td> <td class="alignRight">30</td> </tr> <tr> <td class="occupation architecture"><span>◼ </span>Architecture and engineering</td> <td class="alignRight">47</td> </tr> <tr> <td class="occupation arts"><span>◼ </span>Arts, design, sports, and media</td> <td class="alignRight">51</td> </tr> <tr> <td class="occupation healthcare2"><span>◼ </span>Healthcare practitioners and technical</td> <td class="alignRight">57</td> </tr> <tr> <td class="occupation personal"><span>◼ </span>Personal care and service</td> <td class="alignRight">59</td> </tr> <tr> <td class="occupation military"><span>◼ </span>Military</td> <td class="alignRight">60</td> </tr> <tr> <td class="occupation food"><span>◼ </span>Food preparation and serving-related</td> <td class="alignRight">61</td> </tr> <tr> <td class="occupation office"><span>◼ </span>Office and administrative support</td> <td class="alignRight">95</td> </tr> <tr> <td class="occupation production"><span>◼ </span>Production</td> <td class="alignRight">246</td> </tr> <tr> <td class="occupation building"><span>◼ </span>Building and grounds cleaning and maintenance</td> <td class="alignRight">256</td> </tr> <tr> <td class="occupation protective"><span>◼ </span>Protective service</td> <td class="alignRight">271</td> </tr> <tr> <td class="occupation farming"><span>◼ </span>Farming, fishing, and forestry</td> <td class="alignRight">273</td> </tr> <tr> <td class="occupation sales"><span>◼ </span>Sales and related</td> <td class="alignRight">286</td> </tr> <tr> <td class="occupation installation"><span>◼ </span>Installation, maintenance,and repair</td> <td class="alignRight">374</td> </tr> <tr> <td class="occupation management"><span>◼ </span>Management</td> <td class="alignRight">525</td> </tr> <tr> <td class="occupation construction"><span>◼ </span>Construction and extraction</td> <td class="alignRight">983</td> </tr> <tr> <td class="occupation transportation"><span>◼ </span>Transportation and material moving</td> <td class="alignRight">1340</td> </tr> <tr> <td class="occupation lastLine"><span>◼ </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("▸ 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("▸ 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>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js