D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
manolofrias
Full window
Github gist
The Baltic Sea is getting less polluted
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Stacked area chart</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: rgb(208, 208, 208); margin-left: 150px; margin-right: 50px; margin-bottom: 10px; margin-top:10px; } h1 { font-size: 34px; margin: 0; } p { font-size: 18px; margin: 10px 0 0 0; } .container { display: inline-block; margin: auto; width: 75%; vertical-align: top; } #phosphorus { float: left; width: 50%; height: 100%; margin: 1px; } #nitrogen { float: right; width: 40%; height: 100%; margin: 10px; } svgNitro { background-color: white; } svgPhos { 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> <div class="container"> <h1>The Baltic Sea is getting less polluted</h1> <p>Excessive inputs of nutrients – nitrogen and phosphorus – cause eutrophication which is the main threat to the Baltic Sea. These nutrients originate from anthropogenic sources like municipal waste water treatment plants, agriculture and industries. High levels of phosphorus and nitrogen cause serious problems in the Baltic Sea: algal and plant growth, increased turbidity, oxygen depletion and changes in biodiversity. Source: <a href="https://helcom.fi">HELCOM</a></p> <p>These charts show that the levels of Nitrogen and Phosphorus are decreasing in each Baltic Sea subbasin. </p> <div id="phosphorus"> <h2>Total phosphorus inputs (In tonnes)</h2> </div> <div id="nitrogen"> <h2>Total nitrogen inputs (In tonnes)</h2> </div> </div> <script type="text/javascript"> //Phosphorus //Set up stack method var stackPhos = d3.layout.stack() .values(function(d) { return d.phosphorus; }) .order("reverse"); //Width, height, padding var wPhos = 700; var hPhos = 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 xScalePhos = d3.time.scale() .range([ padding[3], wPhos - padding[1] - padding[3] ]); var yScalePhos = d3.scale.linear() .range([ padding[0], hPhos - padding[2] ]); //Configure axis generators var xAxisPhos = d3.svg.axis() .scale(xScalePhos) .orient("bottom") .ticks(15) .tickFormat(function(d) { return dateFormat(d); }); var yAxisPhos = d3.svg.axis() .scale(yScalePhos) .orient("left") .ticks(5); //Configure area generator var areaPhos = d3.svg.area() .x(function(d) { return xScalePhos(dateFormat.parse(d.x)); }) .y0(function(d) { return yScalePhos(d.y0); //Updated }) .y1(function(d) { return yScalePhos(d.y0 + d.y); //Updated }); //Easy colors accessible via a 10-step ordinal scale var color = d3.scale.category10(); //Create the empty SVG image var svgPhos = d3.select("#phosphorus") .append("svg") .attr("width", wPhos) .attr("height", hPhos); //Load data d3.csv("PInputsData.csv", function(data) { //console.log(data); //New array with all the years, for referencing later var yearsPhos = ["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 datasetPhos = []; //Loop once for each row in data for (var i = 0; i < data.length; i++) { //Create new object with this Subabsin's name and empty array datasetPhos[i] = { subbasin: data[i].Subbasin, phosphorus: [] }; //Loop through all the years for (var j = 0; j < yearsPhos.length; j++) { //Default value, used in case no value is present var amount = null; // If value is not empty if (data[i][yearsPhos[j]]) { amount = +data[i][yearsPhos[j]]; } //Add a new object to the phosphorus data array //for this Subabsin datasetPhos[i].phosphorus.push({ x: yearsPhos[j], y: amount }); } } //Stack the data! stackPhos(datasetPhos); //Uncomment to log the original data to the console //console.log(data); //Uncomment to log the newly restructured dataset to the console //console.log(datasetPhos); //Now that the data is ready, we can check its //min and max values to set our scales' domains! xScalePhos.domain([ d3.min(yearsPhos, function(d) { return dateFormat.parse(d); }), d3.max(yearsPhos, 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 < yearsPhos.length; i++) { totals[i] = 0; for (j = 0; j < datasetPhos.length; j++) { totals[i] += datasetPhos[j].phosphorus[i].y; } } yScalePhos.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 subbasin var paths = svgPhos.selectAll("path") .data(datasetPhos) .enter() .append("path") .attr("class", "areaPhos") .attr("d", function(d) { //Calculate path based on only d.phosphorus array, //not all of d (which would include the Subabsin name) return areaPhos(d.phosphorus); }) .attr("stroke", "none") .attr("fill", function(d, i) { return color(i); }); //Append a title with the Subabsin name (so we get easy tooltips) paths.append("title") .text(function(d) { return d.subbasin; }); //Create axes svgPhos.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h - padding[2]) + ")") .call(xAxisPhos); svgPhos.append("g") .attr("class", "y axis") .attr("transform", "translate(" + padding[3] + ",0)") .call(yAxisPhos); }); //Nitrogen //Set up stack method var stackNitro = d3.layout.stack() .values(function(d) { return d.nitrogen; }) .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 areaNitro = 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(); //Create the empty SVG image var svgNitro = d3.select("#nitrogen") .append("svg") .attr("width", w) .attr("height", h); //Load data d3.csv("NInputsData.csv", function(data) { console.log(data); //New array with all the years, for referencing later var years = ["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 Subabsin's name and empty array dataset[i] = { subbasin: data[i].Subbasin, nitrogen: [] }; //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 nitrogen data array //for this Subabsin dataset[i].nitrogen.push({ x: years[j], y: amount }); } } //Stack the data! stackNitro(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].nitrogen[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 subbasin var paths = svgNitro.selectAll("path") .data(dataset) .enter() .append("path") .attr("class", "areaNitro") .attr("d", function(d) { //Calculate path based on only d.nitrogen array, //not all of d (which would include the Subabsin name) return areaNitro(d.nitrogen); }) .attr("stroke", "none") .attr("fill", function(d, i) { return color(i); }); //Append a title with the Subabsin name (so we get easy tooltips) paths.append("title") .text(function(d) { return d.subbasin; }); //Create axes svgNitro.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h - padding[2]) + ")") .call(xAxis); svgNitro.append("g") .attr("class", "y axis") .attr("transform", "translate(" + padding[3] + ",0)") .call(yAxis); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js