Built with blockbuilder.org
forked from almccon's block: country population barchart
forked from almccon's block: country population barchart (step 1)
forked from almccon's block: country population barchart (step 2: scales)
xxxxxxxxxx
<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;})
var xScale = d3.scaleLinear()
.domain([0,1000000000])
.range([0,500]);
// Using data-binding (the d3 way) and store the selection as a variable
var svgElements = svg.selectAll("g")
.data(filteredData)
.enter()
.append("g");
// Add sub elements to each "g" element
svgElements
.append("text")
.text(function(d) { return d.country;})
.attr("y", function(d,i) { return 200+40*i;})
.attr("x", 100)
.attr("font-size", 20)
// Add more sub elements to each "g" element
svgElements
.append("rect")
.attr("y", function (d,i) {return 176+40*i;})
.attr("x", 100)
.attr("width", function (d) {return xScale(d.population);})
.attr("height",36)
.attr("opacity",0.2)
var bottomAxis = d3.axisBottom(xScale)
.ticks(5);
svg.append("g")
.attr("transform", "translate(100,450)")
.call(bottomAxis);
svg.append("text")
.text("population in 1980")
.attr("transform", "translate(100,440)")
.attr("font-size",10)
.attr("font-family","sans-serif")
});
</script>
</body>
https://d3js.org/d3.v4.min.js