D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
heathermkrause
Full window
Github gist
ChildrenOOS Version 2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Stacked area chart</title> <script type="text/javascript" src="//d3js.org/d3.v3.min.js"></script> <style type="text/css"> body { background-color: #d1be91; font-family: Helvetica, Arial, sans-serif; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .line { fill: none; stroke: steelblue; stroke-width: 1.5px; } #container { width:475px; margin-left: 10px; margin-right: 5px; margin-top: 20px; padding: 10px; float: left; background-color: white; box-shadow: 1px 1px 2px 2px #4d462f; } #container2 { width:400px; margin-left: 10px; margin-right: 5px; margin-top: 20px; padding: 10px; background-color: white; float: left; box-shadow: 1px 1px 2px 2px #4d462f; } #container3 { width:900px; margin-left: 10px; margin-right: 5px; margin-top: 20px; padding: 10px; background-color: #c7c378; box-shadow: 1px 1px 2px 2px #4d462f; } #container4 { width:900px; margin-left: 10px; margin-right: 5px; margin-top: 20px; padding: 10px; background-color: white; float: left; box-shadow: 1px 1px 2px 2px #4d462f; } h1 { font-size: 18px; margin: 0; } p { font-size: 10px; margin: 10px 0 0 0; } p2 { font-size: 12px; margin: 10px 0 0 0; } svg { background-color: white; } path:hover { fill: orange; } path.line:hover { fill: white; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 9px; } .city text { font-family: 'Arial', Arial, sans-serif; font-size: 12px; } </style> </head> <body> <div id="container3"> <h1>There are still too many young children who don't attend school</h1> <p2>Universal Primary Education is considered to be a human right by UNICEF. Much work has been done towards reducing out of school children in the past 20 years. Substantial reduction in both the number and the gender gap has been achieved. However, there is a long way to go. Girls still significantly outnumber boys in vulnerable and conflict areas around the world. As well, the accuracy of data collection itself must be improved. Data Source: <a href="https://data.worldbank.org/indicator/SE.PRM.UNER">World Bank</a>, 2014</p2> </div> <div id="container"> <h1>Primary Aged Children Out of School by Country</h1> <p>Total count of primary aged school children who do not attend school, 1980-2010. Data not available for the entire period for all countries. </p> </div> <div id="container2"> <h1>Children Out of School by Gender</h1> <p>In the past 30 years, the greatest reduction in the counts of our primary aged children not in school has been among girls. Data not available for the entire period for all countries.</p> </div> <div id="container4"> <h1>Children Out of School by Selected Regions</h1> <p>Primary aged children out of school are most common in highly vulnerable and conflicted regions around the globe. It's important to see that while gender parity has been achieved in high income states. Among the most vulnerable children, girls are still more likely to not attend primary school than boys.</p> </div> <script type="text/javascript"> //Set up stack method var stack = d3.layout.stack() .values(function(d) { return d.kids; }) .order("reverse"); //Width, height, padding var w = 470; var h = 250; var padding = [ 20, -10, 20, 65 ]; //Top, right, bottom, left var margin = {top: 30, right: 0, bottom: 30, left: 50}; //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 }); var color2 = d3.scale.ordinal() .domain(["foo", "bar", "baz"]) .range(["#ffbc67","#da727e","#ac6c82"]); var color3 = d3.scale.ordinal() .domain(["Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Antigua and Barbuda", "Argentina", "Armenia", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cabo Verde", "Cambodia", "Cameroon", "Canada", "Cayman Islands", "Central African Republic", "Chad", "Channel Islands", "Chile", "China", "Colombia", "Comoros", "Costa Rica", "Cote Ivoire", "Croatia", "Cuba", "Curacao", "Cyprus", "Czech Republic", "Democratic Republic of Congo", "Democratic Republic of Korea", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Euro area", "Faeroe Islands", "Fiji", "Finland", "France", "French Polynesia", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Greenland", "Grenada", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran ", "Iraq", "Ireland", "Isle of Man", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea", "Kosovo", "Kuwait", "Kyrgyz Republic", "Lao PDR", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macao SAR, China", "Macedonia, FYR", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mexico", "Micronesia", "Missing Data", "Moldova", "Monaco", "Mongolia", "Montenegro", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nepal", "Netherlands", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Northern Mariana Islands", "Norway", "Oman", "Pacific island small states", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Puerto Rico", "Qatar", "Republic of Congo", "Romania", "Russian Federation", "Rwanda", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", "Sint Maarten Dutch", "Slovak Republic", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Sudan", "Spain", "Sri Lanka", "St. Kitts and Nevis", "St. Lucia", "St. Martin", "St. Vincent and the Grenadines", "Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan, China", "Tajikistan", "Tanzania", "Thailand", "Timor-Leste", "Togo", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela, RB", "Vietnam", "West Bank and Gaza", "Yemen", "Zambia", "Zimbabwe"]) .range(["#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c","#808080","#810f7c", "#88419d" , "#8c6bb1","#8c96c6" , "#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6" , "#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6" , "#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6" , "#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6" , "#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6" , "#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6" , "#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6" , "#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6" , "#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c", "#88419d" , "#8c6bb1","#8c96c6" , "#810f7c", "#88419d" , "#8c6bb1","#8c96c6","#810f7c","##88419d","#810f7c"]) //Easy colors accessible via a 10-step ordinal scale var color = d3.scale.category20b(); //Create the empty SVG image var svg = d3.select("#container") .append("svg") .attr("width", w) .attr("height", h); //Load data d3.csv("ChildrenOOS.csv", function(data) { //Uncomment to log the newly loaded data to the console //console.log(data); //New array with all the years, for referencing later var years = [ "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010"]; //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] = { country: data[i].countryName, kids: [] }; //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].kids.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].kids[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.kids array, //not all of d (which would include the country name) return area(d.kids); }) .attr("stroke", "none") .attr("fill", function(d, i) { return color3(i); }); //Append a title with the country name (so we get easy tooltips) paths.append("title") .text(function(d) { return d.country; }); //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) .append("text") .attr("y", 60) .attr("dy", ".71em") .style("text-anchor", "end") .text("Children"); }); //Second chart starts here var w2 = 375; var h2 = 250; var color2 = d3.scale.ordinal() .domain(["foo", "bar", "baz"]) .range(["#605e3a","#523631","#4d462f"]); //Easy colors accessible via a 10-step ordinal scale var color = d3.scale.category20b(); //Create the empty SVG image var svg2 = d3.select("#container2") .append("svg") .attr("width", w2) .attr("height", h2); //Load data d3.csv("ChildrenOOSWorld.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: // // //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 = [ "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010"]; //Create a new, empty array to hold our restructured dataset var dataset2 = []; //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 dataset2[i] = { country: data[i].countryName, kids: [] }; //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 dataset2[i].kids.push({ x: years[j], y: amount }); } } //Stack the data! stack(dataset2); //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 < dataset2.length; j++) { totals[i] += dataset2[j].kids[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 = svg2.selectAll("path") .data(dataset2) .enter() .append("path") .attr("class", "area") .attr("d", function(d) { //Calculate path based on only d.kids array, //not all of d (which would include the country name) return area(d.kids); }) .attr("stroke", "none") .attr("fill", function(d, i) { return color2(i); }); //Append a title with the country name (so we get easy tooltips) paths.append("title") .text(function(d) { return d.country; }); //Create axes svg2.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h2 - padding[2]) + ")") .call(xAxis); svg2.append("g") .attr("class", "y axis") .attr("transform", "translate(" + padding[3] + ",2)") .call(yAxis) .append("text") .attr("y", 60) .attr("dy", ".71em") .style("text-anchor", "end") .text("Children");; }); //Third Chart var margin3 = {top: 20, right: 250, bottom: 30, left: 90}, width3 = 860 - margin3.left - margin3.right, height3 = 300 - margin3.top - margin3.bottom; var parseDate = d3.time.format("%Y").parse; var x = d3.time.scale() .range([0, width3]); var y = d3.scale.linear() .range([height3, 0]); var color4 = d3.scale.category10(); var xAxis2 = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis2 = d3.svg.axis() .scale(y) .orient("left"); var line = d3.svg.line() .interpolate("basis") .x(function(d) { return x(d.date); }) .y(function(d) { return y(d.level); }); var svg5 = d3.select("#container4").append("svg") .attr("width", width3 + margin3.left + margin3.right) .attr("height", height3 + margin3.top + margin3.bottom) .append("g") .attr("transform", "translate(" + margin3.left + "," + margin3.top + ")"); d3.csv("ChildrenOOSLine2.csv", function(error, data) { if (error) throw error; color4.domain(d3.keys(data[0]).filter(function(key) { return key !== "date"; })); data.forEach(function(d) { d.date = parseDate(d.date); }); var categories = color4.domain().map(function(name) { return { name: name, values: data.map(function(d) { return {date: d.date, level: +d[name]}; }) }; }); x.domain(d3.extent(data, function(d) { return d.date; })); y.domain([ d3.min(categories, function(c) { return d3.min(c.values, function(v) { return v.level; }); }), d3.max(categories, function(c) { return d3.max(c.values, function(v) { return v.level; }); }) ]); svg5.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height3 + ")") .call(xAxis2); svg5.append("g") .attr("class", "y axis") .call(yAxis2) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Children"); var city = svg5.selectAll(".city") .data(categories) .enter().append("g") .attr("class", "city"); city.append("path") .attr("class", "line") .attr("d", function(d) { return line(d.values); }) .style("stroke", function(d) { return color4(d.name); }) .on('hover',function(d){d3.select(this).style("fill", "white");}); city.append("text") .datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; }) .attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.level) + ")"; }) .attr("x", 3) .style("stroke", function(d) { return color4(d.name); }) .text(function(d) { return d.name; }); }); </script> </body> </html>
https://d3js.org/d3.v3.min.js