Built with blockbuilder.org
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>
<select></select>
<script>
var margin = {top: 10, right: 80, bottom: 70, left: 45},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
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 + ")");
var g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//this isn't working right now
var parseTime = d3.timeParse("%y");
var line = d3.line()
.x(function(d){return x(d[0])})
.y(function(d){return y(d[1])})
// .curve()
function filterJSON(json, location, dim){
var result = [];
json.forEach( function(val, idx, arr){
if(val.name == location){
result.push(val[dim])}
})
return result[0];
}
var x = d3.scaleLinear()
.range([margin.left,width])
var y = d3.scaleLinear()
.range([height,margin.bottom])
var data;
function drawGraph(data, dim){
x.domain(d3.extent(data, function(d){return d[0]}))
y.domain(d3.extent(data, function(d){return d[1]}))
//appends x axis
g.append("g")
.attr("class", "axis axis--x")
.attr("id", "xaxis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
// appends y axis
g.append("g")
.attr("class", "axis axis--y")
.attr("id", "yaxis")
.call(d3.axisLeft(y))
.append("text")
.attr("fill", "#000")
.attr("transform", "rotate(-90)")
// .attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text(dim);
// appends line
g.append("path")
.datum(data)
.attr("fill", "none")
.attr("class","line")
.attr("stroke", "steelblue")
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-width", 1.5)
.attr("d", line);
}
d3.json("nations.json", function(error,json){
if (error) throw error;
function changeIt(){
//get rid of everything and re-draw
d3.selectAll(".line").remove()
d3.selectAll("#yaxis").remove()
d3.selectAll("#xaxis").remove()
//get the value from the dropdown
var sect = document.getElementById("ddSel");
var country = sect.options[sect.selectedIndex].value;
data_inc = filterJSON(json, country, "income")
data_pop = filterJSON(json, country, "population")
data_le = filterJSON(json, country, "lifeExpectancy")
drawGraph(data_inc, form_val)
}
var dropdown = d3.select("select")
.attr("id", "ddSel");
var options = dropdown.selectAll("option")
.data(json)
options.enter()
.append("option")
.text(function(d){return d.name})
dropdown.on("change",changeIt)
// this builds one line for angola income, initial graph
data = filterJSON(json, "Angola", "income")
data_pop = filterJSON(json, "Angola", "population")
data_le = filterJSON(json, "Angola", "lifeExpectancy")
drawGraph(data, "income")
drawGraph(data_pop, "population")
drawGraph(data_le, "lifeExpectancy")
});
</script>
</body>
https://d3js.org/d3.v4.min.js