forked from taylorchasewhite/US-Travel-Map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
states.js
186 lines (148 loc) · 4.86 KB
/
states.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* This visualization was made possible by modifying code provided by:
Scott Murray, Choropleth example from "Interactive Data Visualization for the Web"
https://github.com/alignedleft/d3-book/blob/master/chapter_12/05_choropleth.html
Malcolm Maclean, tooltips example tutorial
http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html
Mike Bostock, Pie Chart Legend
http://bl.ocks.org/mbostock/3888852 */
//Width and height of map
var width = 960;
var height = 500;
// D3 Projection
var projection = d3.geoAlbersUsa()
.translate([width/2, height/2]) // translate to center of screen
.scale([1000]); // scale things down so see entire US
// Define path generator
var path = d3.geoPath() // path generator that will convert GeoJSON to SVG paths
.projection(projection); // tell path generator to use albersUsa projection
// Define linear scale for output
var color = d3.scaleLinear()
.range(["rgb(213,222,217)","rgb(69,173,168)","rgb(84,36,55)","rgb(217,91,67)"]);
var legendText = ["Cities Lived", "States Lived", "States Visited", "Nada"];
//Create SVG element and append map to the SVG
var svg = d3.select("body").append("svg")
.attr("class","map")
.attr("width", width)
.attr("height", height);
// Append Div for tooltip to SVG
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
// Load in my states data!
d3.tsv("StatesLived.tsv", stateType,function(data) {
color.domain([0,1,2,3]); // setting the range of the input data
// Load GeoJSON data and merge with states data
d3.json("US-States.json", function(json) {
// Loop through each state data value in the .tsv file
for (var i = 0; i < data.length; i++) {
// Grab State Name
var dataState = data[i].State;
// Grab data value
var dataValue = data[i].Visited;
// Find the corresponding state inside the GeoJSON
for (var j = 0; j < json.features.length; j++) {
var jsonState = json.features[j].properties.name;
if (dataState == jsonState) {
// Copy the data value into the JSON
json.features[j].properties.Visited = dataValue;
// Stop looking through the JSON
break;
}
}
}
// Bind the data to the SVG and create one path per GeoJSON feature
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
//.style("stroke", "#fff")
.style("stroke-width", "1");
/*.style("fill", function(d) {
// Get data value
var value = d.properties.Visited;
if (value) {
//If value exists…
return color(value);
}
else {
//If value is undefined…
return "rgb(213,222,217)";
}
});*/
// Map the cities I have lived in!
d3.tsv("CitiesLived.tsv", cityType, function(data) {
svg.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("cx", function(d) {
return projection([d.Longitude, d.Latitude])[0];
})
.attr("cy", function(d) {
return projection([d.Longitude, d.Latitude])[1];
})
.attr("r", function(d) {
return Math.sqrt(d.YearsLived) * 4;
})
.style("fill", "rgb(217,91,67)")
.style("opacity", 0.85)
// Modification of custom tooltip code provided by Malcolm Maclean, "D3 Tips and Tricks"
// http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div.text(d.place)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
// fade out tooltip on mouse out
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
});
// Modified Legend Code from Mike Bostock: http://bl.ocks.org/mbostock/3888852
var legend = d3.select("body").append("svg")
.attr("class", "legend")
.attr("width", 140)
.attr("height", 200)
.selectAll("g")
.data(color.domain().slice().reverse())
.enter()
.append("g")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.data(legendText)
.attr("x", 24)
.attr("y", 9)
.attr("dy", ".35em")
.text(function(d) { return d; });
});
});
function cityType(type) {
d = new Object();
d.State = type["State"];
d.YearsLived = type["Years Lived"];
d.Latitude = type["Latitude"];
d.Longitude = type["Longitude"];
return d;
}
function stateType(type) {
d = new Object();
d.State = type["State"];
d.Status = type["Status"];
if (type["Status"]="Visited") {
d.Visited=1;
}
else {
d.Visited=0;
}
return d;
}