Step 5: Add styling to the map.
By default, each of the path elements will inherent a black ("#000000") fill and stroke. To update each of our US states, we can group all of the states together by appending the SVG
"g" group element and give this the class attribute land.
CSS:
.land {
fill: #eee;
stroke: #777777;
}
JavaScript:
svg.append("g")
.attr("class", "land")
.selectAll("path")
.data(topojson.feature(us, us.objects.state_pol).features)
.enter().append("path")
.attr("d", path)
Step 6: Add Data to the Map
Now, let's add some data to this map! Download this CSV file listing the location of the top 150 ports in the United States, which was taken from the National Transportation Atlas Database. I converted the original file from .shp to .csv using ogr2ogr
because this is a more familiar data format among beginners.
As data files are requested asynchronously by JavaScript, the order in which different data layers are loaded is unpredictable. For instance, you may call for a map to have a set of countries as the bottom layer and the names of each country layered on top of this. You would first load the "country" path elements and then load the "country name" text elements. However, if your browser first is able to load the text before the paths due to discrepancies in file sizes (e.g., path is WAY bigger than text), the text will render first.
To address this issue, I recommend using d3's queue
library and defer manipulating the imported data until ALL of the data is loaded to the web browser. Once the code is ready, a callback fuction is triggered, passing an error variable and a variable for the data packets, in order respectively.
<script src="http://d3js.org/queue.v1.min.js" charset="utf-8"></script>
// Load all our data at once using queue.js
queue()
.defer(d3.json, "state_pol.json")
.defer(d3.csv, "ports_major.csv")
.await(ready);
function ready(error, us, ports){
Copyright © Mike Taptich 2015... JK. Take what you want from here.