block by almccon e47e8bab24d66e198b1737005664df26

country population barchart

Full Screen

Built with blockbuilder.org

index.html

<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <script src="https://d3js.org/d3.v4.min.js"></script>
  <style>
    body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
  </style>
</head>

<body>
  <script>
    // Feel free to change or delete any of the code you see in this editor!
    var svg = d3.select("body").append("svg")
      .attr("width", 960)
      .attr("height", 500)
    
    d3.csv("gapminder_avg.csv", function(data) {
      data.forEach(function(d) {
        
        d.population = +d.population;
        d.lifeexp = +d.lifeexp;
        d.gdp = +d.gdp;
        
      });

      var filteredData = data
      	.filter(function(d) {return +d.population > 100000000;})
      	.sort(function(a,b) {return a.population < b.population;})
        
      // Not using data-binding      
      filteredData
        .forEach(function(d,i) {
        	svg.append("text")
      			.text(d.country)
      			.attr("y", 200+40*i)
      			.attr("x", 100)
      			.attr("font-size", 20)
      			.attr("font-family", "sans-serif")
      });

      // Using data-binding (the d3 way)
      svg.selectAll("rect")
      		.data(filteredData)
      	.enter()
      		.append("rect")
      		.attr("y", function (d,i) {return 176+40*i;})
      		.attr("x", 100)
      		.attr("width", function (d) {return 500 * d.population / 1000000000;})
      		.attr("height",36)
        	.attr("opacity",0.2)

    });



  </script>
</body>

gapminder_avg.csv