-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflows.html
More file actions
249 lines (183 loc) · 6.44 KB
/
flows.html
File metadata and controls
249 lines (183 loc) · 6.44 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.states {
fill: #ccc;
}
.state-borders {
fill: none;
stroke: #fff;
stroke-width: 1.5px;
stroke-linejoin: round;
stroke-linecap: round;
}
.county-borders {
fill: none;
stroke: #fff;
stroke-width: .25px;
stroke-linejoin: round;
stroke-linecap: round;
}
.county-arcs {
display: none;
fill: none;
stroke: #000;
}
.county-cell {
fill: black;
pointer-events: all;
stroke: #fff;
stroke-width: 0.25px;
stroke-linejoin: round;
stroke-linecap: round;
}
.counties circle {
fill: steelblue;
stroke: #fff;
pointer-events: none;
}
.county:hover .county-arcs {
display: inline;
}
.county-arcs2 {
display: inline;
stroke: rgba(255,255,255,.5);
stroke-width: 1.5;
fill: none;
stroke: #000;
}
.link {
stroke: rgba(255,255,255,.5);
fill: none;
stroke: #000;
}
svg:not(:hover) .county-cell {
stroke: #000;
stroke-opacity: .2;
}
svg .link_line {
stroke: rgba(255,255,255,.5);
stroke-width: 1.5;
}
</style>
<body>
<script src="scripts/d3.v3.min.js"></script>
<script src="scripts/queue.min.js"></script>
<script src="scripts/topojson.js"></script>
<script>
var width = 960,
height = 500;
var d3LineLinear = d3.svg.line().interpolate("linear");
var d3color = d3.interpolateRgb("#BAE4B3", "#006D2C"); /* color range for flow lines */
var projection = d3.geo.albers() /* map projection - ALBERS */
.translate([width / 2, height / 2])
.scale(1080);
var path = d3.geo.path()
.projection(projection);
var radius = d3.scale.sqrt() /* for scaling proportional symbols */
.domain([0, 1e6])
.range([0, 15]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
queue() /* load data, await ready */
.defer(d3.json, "data/us.json")
.defer(d3.csv, "data/nodes.csv")
.defer(d3.csv, "data/flowsB.csv")
.await(ready);
function ready(error, us, counties, flows) {
var countyById = d3.map(),
positions = [];
counties.forEach(function(d) {
countyById.set(d.GEOID, d);
d.outgoing = [];
d.incoming = [];
d.pop = d.pop /* population variable */
});
flows.forEach(function(flow) {
var source = countyById.get(flow.Orig_GEOID10),
target = countyById.get(flow.Dest_GEOID10),
magnitude = +flow.filings;
link = {source: source, target: target, magnitude: magnitude};
//console.log('no', link);
source.outgoing.push(link);
//console.log('te', flow);
target.incoming.push(link);
});
//GLOBAL STRENGTH SCALE
var strength_scale = d3.scale.sqrt().range([.2, 2]) /* thickness range for flow lines */
.domain([0, d3.max(flows, function(d) {
return d.filings;
})]);
//var color_scale = d3.scale.linear().range([0, 1]).domain([0, d3.max(flows, function(d) {
// return d.filings; /*scale arrow color based on filings */
//})]);
counties = counties.filter(function(d) {
if (d.count = Math.max(d.incoming.length, d.outgoing.length)) {
d[0] = +d.lon;
d[1] = +d.lat;
var position = projection(d);
d.x = position[0];
d.y = position[1];
return true;
}
});
//LAND from topoJSON
svg.append("path")
.datum(topojson.feature(us, us.objects.land))
.attr("class", "states")
.attr("d", path);
//COUNTIES from topoJSON
svg.append("path")
.datum(topojson.mesh(us, us.objects.counties, function(a, b) { return a !== b; }))
.attr("class", "county-borders")
.attr("d", path);
//STATES from topoJSON
svg.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "state-borders")
.attr("d", path);
var county = svg.append("g")
.attr("class", "counties")
.selectAll("g")
.data(counties.sort(function(a, b) { return b.count - a.count; }))
.enter().append("g");
/*county.append("path")
.attr("class", "county-cell")
.attr("d", function(d) { return d.cell.length ? "M" + d.cell.join("L") + "Z" : null; });*/
//Proportional Circles on Map based on population (d.pop)
county.append("circle")
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.attr("class", "circle")
.attr("id", "circle")
.attr("r", function(d) { return radius(d.pop); })
//Links between counties based on flows
county.append("g")
.attr("class", "link")
.selectAll("path")
.data(function(d) { return d.outgoing; })
.enter().append("path")
.attr("d", function(d) { return path({type: "LineString", coordinates: [d.source, d.target]})})
.style("stroke-width", function(d) { return strength_scale(d.magnitude); })
.attr("d", function(d) {
return drawCurve(d);
});
//function to replicated scaled lines and arrowheads
function drawCurve(d) {
var slope = Math.atan2((+d3.select('#circle' + d.target) - d3.select('#circle' + d.source)), (+d3.select('#circle' + d.target) - d3.select('#circle' + d.source)));
var slopePlus90 = Math.atan2((+d3.select('#circle' + d.target) - d3.select('#circle' + d.source)), (+d3.select('#circle' + d.target) - d3.select('#circle' + d.source))) + (Math.PI / 2);
var sourceX = +d3.select('#circle' + d.source).attr("cx");
var sourceY = +d3.select('#circle' + d.source).attr("cy");
var targetX = +d3.select('#circle' + d.target).attr("cx");
var targetY = +d3.select('#circle' + d.target).attr("cy");
var arrowOffset = 20;
var points = [];
points.push([sourceX + radius * Math.cos(slope) - strength_scale(d.magnitude) * Math.cos(slopePlus90), sourceY + radius * Math.sin(slope) - strength_scale(d.magnitude) * Math.sin(slopePlus90)]);
points.push([sourceX + radius * Math.cos(slope), sourceY + radius * Math.sin(slope)]);
points.push([targetX - radius * Math.cos(slope), targetY - radius * Math.sin(slope)]);
points.push([targetX - (radius + arrowOffset) * Math.cos(slope) - strength_scale(d.magnitude + (arrowOffset / 2)) * Math.cos(slopePlus90), targetY - (radius + arrowOffset) * Math.sin(slope) - strength_scale(d.magnitude + (arrowOffset / 2)) * Math.sin(slopePlus90)]);
points.push([targetX - (radius + arrowOffset) * Math.cos(slope) - strength_scale(d.magnitude) * Math.cos(slopePlus90), targetY - (radius + arrowOffset) * Math.sin(slope) - strength_scale(d.magnitude) * Math.sin(slopePlus90)]);
return d3LineLinear(points) + "Z";
}
}
</script>