D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
kmix27
Full window
Github gist
dropdown_noTransition_oneLine
Built with
blockbuilder.org
<!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> <select></select> <form id="dimensions"> <input type='radio' id="income" name="mode" checked>Income</input> <input type='radio' id="population" name="mode">Population</input> <input type='radio' id="lifeExpectancy" name="mode">Life Expectancy</input> </form> <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; var form = document.getElementById("dimensions") var form_val; for(var i=0; i<form.length; i++){ if(form[i].checked){ form_val = form[i].id;}} data = filterJSON(json, country, form_val) drawGraph(data, 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) var dataDim = d3.select("#dimensions") dataDim.on("change", changeIt) // this builds one line for angola income, initial graph data = filterJSON(json, "Angola", "income") console.log(line(data)) drawGraph(data, "income") }); </script> </body>
https://d3js.org/d3.v4.min.js