A look at Queue.js and how you can load multiple files before running the rest of the code. Simple visualization of populated places in the USA. Files loaded:
- US States - topojson
- Populated Places - geojson
| <!DOCTYPE html> | |
| <meta http-equiv="content-type" content="text/html; charset=utf-8"> | |
| <style> | |
| .states { | |
| fill: #e5e5e5; | |
| stroke: #fff; | |
| stroke-width:1px; | |
| } | |
| .cities { | |
| fill: red; | |
| opacity:0.7; | |
| } | |
| </style> | |
| <body> | |
| <script src="http://d3js.org/d3.v3.min.js"></script> | |
| <script src="http://d3js.org/topojson.v1.min.js"></script> | |
| <script src="http://d3js.org/queue.v1.min.js"></script> | |
| <script> | |
| var width = 960, | |
| height = 500; | |
| var svg = d3.select('body').append('svg') | |
| .attr('width', width) | |
| .attr('height', height); | |
| var projection = d3.geo.albersUsa() | |
| .scale(1000) | |
| .translate([width / 2, height / 2]); | |
| var path = d3.geo.path() | |
| .projection(projection); | |
| queue() | |
| .defer(d3.json, 'states.json') | |
| .defer(d3.json, 'cities.json') | |
| .await(makeMyMap); | |
| function makeMyMap(error, states, cities) { | |
| svg.append('path') | |
| .datum(topojson.feature(states, states.objects.usStates)) | |
| .attr('d', path) | |
| .attr('class', 'states'); | |
| svg.selectAll('.cities') | |
| .data(cities.features) | |
| .enter() | |
| .append('path') | |
| .attr('d', path.pointRadius(5)) | |
| .attr('class', 'cities'); | |
| } | |
| </script> | |
| </body> | |
| </html> |