D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
BenHeubl
Full window
Github gist
Water Footprints on Consumption
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>press index</title> <style type="text/css"> body { background-color: #e4e8e7; } h1 { font: 30px Futura; fill: black; } p {font: 14px Futura; fill: black; } li { font: 10px Futura; fill: black; } text { font: 12px Futura; fill: black; stroke: #grey; } rect.background { fill: #e4e8e7; stoke: #9D7917; } .axis { shape-rendering: crispEdges; } .axis path, .axis line { fill: #638ea3; stroke: none; } </style> </head> <body> <h1> WATER FOOTPRINTS</h1> <p>Water footprints for consumption, measured in m3 /yr per capita.</p> <p>Key takeaways: </p> <li> 1. UAE is having one of the largest water footprints (a city in the dessert, dah).</li> <br></br> <li> 2. The global average water footprint related to consumption is 1385 m3 /yr per capita over the period 1996-2005. Consumption of agricultural products largely determines the global water footprint related to consumption, contributing 92% to the total water footprint.</li> <br></br> <li> 3.Much lower, the consumption of industrial products and domestic water use contribute only 4.7% and 3.8% respectively. </li> <br></br> <li> 4.In terms of product categories, cereals consumption contribute the largest share to the global water footprint (27%), followed by meat (22%) and milk products (7%). So to contribute to the your national water footprint, it helps to eat less meat (as most of the cereals productions is to grow the meat anyway. </li> <br></br> <br></br> <script src="https://d3js.org/d3.v3.min.js"></script> <script> var margin = {top: 20, right: 150, bottom: 0, left: 300}, width = 900 - margin.left - margin.right, height = 800 - margin.top - margin.bottom; var x = d3.scale.linear() .range([0, width]); var barHeight = 65; var color = d3.scale.ordinal() .range(["#dcb444", "#638ea3"]); var duration = 2000, delay = 500; var partition = d3.layout.partition() .value(function(d) { return d.size; }); var xAxis = d3.svg.axis() .scale(x) .orient("top"); var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); svg.append("rect") .attr("class", "background") .attr("width", width) .attr("height", height) .on("click", up); svg.append("g") .attr("class", "x axis"); svg.append("g") .attr("class", "y axis") .append("line") .attr("y1", "100%"); d3.json("readme2.json", function(error, root) { partition.nodes(root); x.domain([0, root.value]).nice(); down(root, 0); }); function down(d, i) { if (!d.children || this.__transition__) return; var end = duration + d.children.length * delay; // Mark any currently-displayed bars as exiting. var exit = svg.selectAll(".enter") .attr("class", "exit"); // Entering nodes immediately obscure the clicked-on bar, so hide it. exit.selectAll("rect").filter(function(p) { return p === d; }) .style("fill-opacity", 1e-6); // Enter the new bars for the clicked-on data. // Per above, entering bars are immediately visible. var enter = bar(d) .attr("transform", stack(i)) .style("opacity", 1); // Have the text fade-in, even though the bars are visible. // Color the bars as parents; they will fade to children if appropriate. enter.select("text").style("fill-opacity", 1e-6); enter.select("rect").style("fill", color(true)); // Update the x-scale domain. x.domain([0, d3.max(d.children, function(d) { return d.value; })]).nice(); // Update the x-axis. svg.selectAll(".x.axis").transition() .duration(duration) .call(xAxis); // Transition entering bars to their new position. var enterTransition = enter.transition() .duration(duration) .delay(function(d, i) { return i * delay; }) .attr("transform", function(d, i) { return "translate(0," + barHeight * i * 1.2 + ")"; }); // Transition entering text. enterTransition.select("text") .style("fill-opacity", 1); // Transition entering rects to the new x-scale. enterTransition.select("rect") .attr("width", function(d) { return x(d.value); }) .style("fill", function(d) { return color(!!d.children); }); // Transition exiting bars to fade out. var exitTransition = exit.transition() .duration(duration) .style("opacity", 1e-6) .remove(); // Transition exiting bars to the new x-scale. exitTransition.selectAll("rect") .attr("width", function(d) { return x(d.value); }); // Rebind the current node to the background. svg.select(".background") .datum(d) .transition() .duration(end); d.index = i; } function up(d) { if (!d.parent || this.__transition__) return; var end = duration + d.children.length * delay; // Mark any currently-displayed bars as exiting. var exit = svg.selectAll(".enter") .attr("class", "exit"); // Enter the new bars for the clicked-on data's parent. var enter = bar(d.parent) .attr("transform", function(d, i) { return "translate(0," + barHeight * i * 1.2 + ")"; }) .style("opacity", 1e-6); // Color the bars as appropriate. // Exiting nodes will obscure the parent bar, so hide it. enter.select("rect") .style("fill", function(d) { return color(!!d.children); }) .filter(function(p) { return p === d; }) .style("fill-opacity", 1e-6); // Update the x-scale domain. x.domain([0, d3.max(d.parent.children, function(d) { return d.value; })]).nice(); // Update the x-axis. svg.selectAll(".x.axis").transition() .duration(duration) .call(xAxis); // Transition entering bars to fade in over the full duration. var enterTransition = enter.transition() .duration(end) .style("opacity", 1); // Transition entering rects to the new x-scale. // When the entering parent rect is done, make it visible! enterTransition.select("rect") .attr("width", function(d) { return x(d.value); }) .each("end", function(p) { if (p === d) d3.select(this).style("fill-opacity", null); }); // Transition exiting bars to the parent's position. var exitTransition = exit.selectAll("g").transition() .duration(duration) .delay(function(d, i) { return i * delay; }) .attr("transform", stack(d.index)); // Transition exiting text to fade out. exitTransition.select("text") .style("fill-opacity", 1e-6); // Transition exiting rects to the new scale and fade to parent color. exitTransition.select("rect") .attr("width", function(d) { return x(d.value); }) .style("fill", color(true)); // Remove exiting nodes when the last child has finished transitioning. exit.transition() .duration(end) .remove(); // Rebind the current parent to the background. svg.select(".background") .datum(d.parent) .transition() .duration(end); } // Creates a set of bars for the given data node, at the specified index. function bar(d) { var bar = svg.insert("g", ".y.axis") .attr("class", "enter") .attr("transform", "translate(0,5)") .selectAll("g") .data(d.children) .enter().append("g") .style("cursor", function(d) { return !d.children ? null : "pointer"; }) .on("click", down); bar.append("text") .attr("x", -6) .attr("y", barHeight / 2) .attr("dy", ".35em") .style("text-anchor", "end") .text(function(d) { return d.name; }); bar.append("rect") .attr("width", function(d) { return x(d.value); }) .attr("height", barHeight); return bar; } // A stateful closure for stacking bars horizontally. function stack(i) { var x0 = 0; return function(d) { var tx = "translate(" + x0 + "," + barHeight * i * 1.2 + ")"; x0 += x(d.value); return tx; }; } </script> <p>Sources:<p> <li><stong>https://waterfootprint.org/media/downloads/Report50-NationalWaterFootprints-Vol1.pdf</stong></li> <br></br> <li><stong>https://waterfootprint.org/en/resources/water-footprint-statistics/#CP1</stong></li> <br></br> <li><stong>https://environment.nationalgeographic.com/environment/freshwater/water-calculator-methodology/</stong></li> <br></br> </body> </html>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js