D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
darrenjaworski
Full window
Github gist
thenmap api - us states
<!DOCTYPE html> <meta charset="utf-8"> <style> .state { fill: #d3d3d3; stroke: #f1f1f1; stroke-width: 1px; } svg { display: block; margin: auto; font: 14px sans-serif; } body { margin: 0; } svg, body { background: #f1f1f1; } </style> <body> <select name="" id=""> <option value="1780">1780</option> <option value="1800">1800</option> <option value="1820">1820</option> <option value="1850">1850</option> <option value="1865">1865</option> <option value="1880">1880</option> <option value="1900">1900</option> <option value="1930">1930</option> </select> <script src="//d3js.org/d3.v3.min.js"></script> <script src="//d3js.org/topojson.v1.min.js"></script> <script> var width = 960, height = 500; var projection = d3.geo.albersUsa() .scale(1000) .translate([width / 2, height / 2]); var path = d3.geo.path() .projection(projection); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height); function render(year) { d3.json("https://api.thenmap.net/v1/us4/geo|data/"+ year +"?data_props=sdate|edate|name&geo_type=topojson", function(error, us) { if (error) throw error; console.log(us) var statetopo = topojson.feature(us.geo, us.geo.objects.collection); var states = svg.selectAll('path') .data(statetopo.features, function(d) { return d.id; }); states.transition() .duration(1000) .attr('d', path); states.enter() .append("path") .attr('class', 'state') .attr("d", path) .append('title') .text(function(d) { return d.id; }); states.exit() .transition() .duration(1000) .attr('d', path) .remove(); }); } render(1780); d3.select('select').on('change', function(d) { render(this.value); }) </script>
https://d3js.org/d3.v3.min.js
https://d3js.org/topojson.v1.min.js