D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Contrastat
Full window
Github gist
Registered unemployed by Spanish Province
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Stacked area chart</title> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script> <style type="text/css"> body { background-color: white; font-family: Helvetica, Arial, sans-serif; } h1 { font-size: 24px; margin: 0; } p { font-size: 14px; margin: 10px 0 0 0; } svg { background-color: white; } path:hover { fill: yellow; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } </style> </head> <body> <h1>Number of unemployed by spanish province. In 1,000 people.</h1> <p>Registerered unemployed from Q1 2008 to Q3 2015. Source: <a href="https://www.ine.es">I.N.E.</a>, 2015</p> <script type="text/javascript"> //Set up stack method var stack = d3.layout.stack() .values(function(d) { return d.aturats; }) .order("reverse"); //Width, height, padding var w = 700; var h = 600; var padding = [ 20, 10, 50, 100 ]; //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.category10(); //var dateParsed = quarters.map(function(d, i){ // var splitted = d.split('T'); // var quarterMonth = splitted[1].charAt(0) * 3 - 3; // return [d3.time.format('%m %Y').parse(quarterMonth + ' ' + splitted[0])]; //}); //Create the empty SVG image var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); //Load data d3.csv("aturats_provincia2.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 quarters = ["2008T1", "2008T2", "2008T3", "2008T4", "2009T1", "2009T2", "2009T3", "2009T4", "2010T1", "2010T2", "2010T3", "2010T4", "2011T1", "2011T2", "2011T2", "2011T3", "2011T4", "2012T1", "2012T2", "2012T3", "2012T4", "2013T1", "2013T2", "2013T3", "2013T4", "2014T1", "2014T2", "2014T3", "2014T4", "2015T1", "2015T2", "2015T3"]; var quarters = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"]; //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 province name and empty array dataset[i] = { provincia: data[i].Provincia, aturats: [] }; //Loop through all the quarters for (var j = 0; j < quarters.length; j++) { //Default value, used in case no value is present var amount = null; // If value is not empty if (data[i][quarters[j]]) { amount = +data[i][quarters[j]]; } //Add a new object to the aturats data array //for this province dataset[i].aturats.push({ x: quarters[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(quarters, function(d) { return dateFormat.parse(d); }), d3.max(quarters, function(d) { return dateFormat.parse(d); }) ]); //Need to recalculate the max value for yScale //differently, now that everything is stacked. //Loop once for each quarter, and get the total value //of unemployed for that quarter. var totals = []; for (i = 0; i < quarters.length; i++) { totals[i] = 0; for (j = 0; j < dataset.length; j++) { totals[i] += dataset[j].aturats[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.aturats); }) .attr("stroke", "none") .attr("fill", function(d, i) { return color(i); }); //Append a title with the country name (so we get easy tooltips) paths.append("title") .text(function(d) { return d.provincia; }); //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