Reading Notes for D3.js Part VIII (Chapter 12)
This is a Reading Note for Interactive Data Visualization for the Web - An Introduction to Designing with D3 by Scott Murray, pulished by O’Reilly, 2013
This post will contains concepts that I think it’s important for me to be familiar with in D3 book.
Note: All tips that is written by book will has a sign near it.
All code example in this post comes from book.
Chapter 12 : Map code example for Chapter 12 is here.
GeoJSON
is a special format for JSON
, which is used specially for geography. A typical GeoJSON
file is an object with two attributes: type
and features
. The features
attibute is an array of objects. Each objects have geography attributes, such as type
,id
,properties
,geometry
.geometry
is a vary useful attributes. It can include coordinate
attribue, which inludes longitude,latitude
pairs. Note longitude
is vertical on map, latitude
is horizontal on map. Here is a sample:
Step 1: transfer GeoJSON
to SVG path
code by using d3.geo.path()
. Then load GeoJSON
file and apply new path:
|
|
Step 2: create projection
to transfer 3D to 2D, then use d3.geo.path().projection()
to apply projection to path. You can also add scale()
to make map smaller or larger:
|
|
Use Color to Quantize Map
We can add color with different combination to map to show data difference in map. For example, the place that has more populatio with dark blue and less population with light green, etc.
Step1: create a quantize scale with color as output. A combination of color can be found in https://github.com/mbostock/d3/tree/master/lib/colorbrewer, from D3 library. Now we output discreate value as color. The number of colors decides how precise the map will be. Less color group will make map has less color combination:
|
|
Step2: use d3.json()
or d3.csv()
to load data. In data callback function, set up color input domain:
|
|
Step3: load data. We may need to combine data in two json/csv file. To do this, wrap another data callback function in current data callback function, then compare column with same data. if it fits, add an attribute in a dataset with the nessiary data from the other dataset. You can use nested for loop to compare data:
|
|
Step 4: create path and load data to path. You need to do this inside a callback loop since we need to use data variable in loop. We need to check if the value in second json/csv exist or not in first json/csv. If not, we need return a single color:
|
|
Step 5: create and add projection. Note you need to do this step outside of callback function so that your result will not be affected.
Add Circle as Data Point on Map
If we have json/csv file that have city names, value, and cities longitude, latitude data, we can make it on map.
Step1, Step2, Step3, Step4: Same as example above.
Step 5: load new data and add circle to map. You need create projection function before this step, buy don’t apply it to all map. Also, you need to put this part inside callback function, so that your circle will not be covered by map since map will be loaded later than outside functions:
|
|
Code For This Chapter
code example for Chapter 12 is here.
|
|