forked from PereiraM/data-mapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatamapper.js
executable file
·338 lines (309 loc) · 11.1 KB
/
datamapper.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
function datamapper(mapContainer, mapType, data) {
var selectedField;
var selectedSeq;
var choropleth = [];
choropleth["null"] = "#fff";
choropleth["q0-9"] = "#eee";
choropleth["q1-9"] = "#ccc";
choropleth["q2-9"] = "#b2b2b2";
choropleth["q3-9"] = "#999";
choropleth["q4-9"] = "#808080";
choropleth["q5-9"] = "#666";
choropleth["q6-9"] = "#4d4d4d";
choropleth["q7-9"] = "#333";
choropleth["q8-9"] = "#000";
// clear map div
d3.select(mapContainer).html("");
// set content vars
var id;
var width = 960,
height = 450;
var formatNumber = d3.format(",.0f");
var projection = d3.geo.conicConformal()
.rotate([102, 0])
.center([0, 24])
.parallels([17.5, 29.5])
.scale(1200)
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
var ttwidth = 220,
ttheight = 100;
// append map svg
var svg = d3.select(mapContainer).append("svg")
.attr("width", width)
.attr("height", height);
// append legend div
var legendContainer = d3.select(mapContainer).append("div")
.attr("id", "dm-legend-container");
// append button stuff
var buttonContainer = d3.select(mapContainer).append("div")
.attr("class","dm-button-container");
buttonContainer.append("h4").html("Generated fields");
buttonContainer.append("p").html("The buttons generated here represent fields from the CSV data below.");
buttonContainer.append("div")
.attr("id","dm-sequence-btns")
.attr("class","btn-group");
buttonContainer.append("div")
.attr("id","dm-fld-btns")
.attr("class","btn-group");
// append tooltip
var tooltipContainer = d3.select(mapContainer).append("div")
.attr("class", "dm-tooltip")
.style("opacity", 0)
.style("width",ttwidth+"px")
.style("min-height",ttheight+"px");
queue()
.defer(d3.json, "mexico.json")
.defer(d3.json, "counties.json")
.await(ready);
function ready(error, mexico, counties) {
// get field names for values
var keys = Object.keys(data[0]);
var valFields = [];
keys.forEach(function(d,i) {
if (d !== "sequence" && d !== "fips") {
valFields.push(d);
}
});
// set selected field to first in array
selectedField = valFields[0];
// get range of sequence field, then sort, then get min and max
var seqs = [];
data.forEach(function(d) {
d.fips = d.fips;
if (seqs.indexOf(d.sequence) == -1) {
seqs.push(d.sequence);
}
});
selectedSeq = seqs[0];
seqs.sort(function(a,b) { return a-b; });
var startSeq = d3.min(seqs);
var endSeq = d3.max(seqs);
// nest up the inputted data
var nestedData = d3.nest()
.key(function(d) { return d.fips; })
.key(function(d) { return d.sequence; })
.map(data);
// add buttons for sequences, init mapData
var mapData = [];
if (typeof seqs[0] !== "undefined") {
seqs.forEach(function(seq) {
mapData[seq] = [];
var btnactive = (seq == startSeq) ? "active" : "";
d3.select("#dm-sequence-btns").append("button")
.attr("class","dm-sequence-btn btn "+btnactive)
.attr("id","e"+seq)
.html(seq)
});
} else {
mapData["undefined"] = [];
d3.select("#dm-sequence-btns").html("")
}
// create fipstocounty
var fipstocounty = [];
topojson.feature(mexico, mexico.objects.mex_counties).features.forEach(function(d) {
fipstocounty[d.id] = d;
});
// create node for each county in mapData, create fipstoname
var fipstoname = [];
counties.forEach(function(county) {
fipstoname[county.fips] = county.county;
seqs.forEach(function(seq) {
if (typeof nestedData[county.fips] !== "undefined" && typeof nestedData[county.fips][seq] !== "undefined") {
mapData[seq].push(nestedData[county.fips][seq][0]);
} else {
var str = "{";
keys.forEach(function(key,i) {
var comma = (i == keys.length-1) ? "" : ","
var val = 0;
if (key == "fips") {
val = county.fips;
} else if (key == "sequence") {
val = seq;
}
str += '"'+key+'":"'+val+'"'+comma;
});
str += "}";
// push the empty array if there's no inputted data for this county
mapData[seq].push(JSON.parse(str));
}
});
});
// create array of min,max,median for all value fields
// add buttons for value fields
var minmax = [];
valFields.forEach(function(d) {
minmax[d] = [];
seqs.forEach(function(seq) {
minmax[d][seq] = [
d3.min(data, function(d0) { return +d0[d];}),
d3.max(data, function(d0) { return +d0[d];}),
d3.median(data, function(d0) { return +d0[d];})
];
})
var btnactive = (d == selectedField) ? "active" : "";
d3.select("#dm-fld-btns").append("button")
.attr("class","dm-fld-btn btn "+btnactive)
.attr("id","e"+d)
.html(d)
});
// dynamic radius
var radius = d3.scale.sqrt()
.domain([0, minmax[selectedField][endSeq][1]])
.range([0, 15]);
// setup legend
var lwidth = 160,
lheight = 50;
// append legend svg
var legendSvg = legendContainer.append("svg")
.attr("width", lwidth)
.attr("height", lheight);
if (mapType == "bubble") {
// bubbles
legendSvg.selectAll(".dm-legend-dots")
.data([1,minmax[selectedField][endSeq][1]])
.enter().append("circle")
.attr("class","dm-legend-dots")
.attr("cx",function(d) { return radius(minmax[selectedField][endSeq][1]) })
.attr("cy",function(d,i) { return (radius(d)*2) })
.attr("r",function(d) { return radius(d); });
legendSvg.selectAll(".dm-legend-text")
.data([1,minmax[selectedField][endSeq][1]])
.enter().append("text")
.attr("class","dm-legend-text")
.attr("x",function(d) { return (radius(minmax[selectedField][endSeq][1])*2)+5 })
.attr("y",function(d,i) { return (radius(d)*2)+5 })
.attr("text-anchor","start")
.text(function(d) { return d+" "+selectedField });
svg.append("g")
.selectAll(".dm-dots")
.data(mapData[startSeq])
.enter().append("circle")
.attr("class","dm-dots")
.attr("id",function(d) { return d.fips })
.attr("transform", function(d) {
var coord = (path.centroid(fipstocounty[d.fips])[0] > 0 && path.centroid(fipstocounty[d.fips])[1] > 0) ? path.centroid(fipstocounty[d.fips]) : [-1000,-1000];
return "translate(" + coord + ")";
})
.attr("r", function(d) {
var dotsize = (radius(d[selectedField]) >= 0) ? radius(d[selectedField]) : 0;
return dotsize;
});
} else {
// choropleth
var quantize = d3.scale.quantize()
.domain([0, minmax[selectedField][selectedSeq][1]/2])
.range(d3.range(9).map(function(i) { return "q" + i + "-9"; }));
svg.append("g")
.attr("class", "dm-counties")
.selectAll("path")
.data(topojson.feature(mexico, mexico.objects.mex_counties).features)
.enter().append("path")
.attr("class","dm-county")
.attr("id",function(d) { return d.id })
.attr("d", path)
.call(getFill);
}
svg.append("path")
.datum(topojson.feature(mexico, mexico.objects.nation))
.attr("class", "dm-nation")
.attr("id","dm-nation-shape")
.attr("d", path);
// tooltip on mouseover
d3.selectAll(".dm-dots,.dm-county")
.on("mouseover",function(d){
if (mapType == "choropleth") {
d3.select(this).style("fill","steelblue");
}
var fips = (parseInt(d.fips) > 0) ? d.fips : d.id;
var coord = (path.centroid(fipstocounty[fips])[0] > 0 && path.centroid(fipstocounty[fips])[1] > 0) ? path.centroid(fipstocounty[fips]) : [0,0];
var tiptext = "<p style='border-bottom:1px solid #ddd'><span class='bold'>"+fipstoname[fips]+"</span></p>";
keys.forEach(function(key) {
if (typeof nestedData[fips] !== "undefined" && typeof nestedData[fips][selectedSeq] !== "undefined") {
tiptext += key+": "+nestedData[fips][selectedSeq][0][key]+"<br>";
}
});
tooltipContainer
.html(tiptext)
.style("left", coord[0]+10+"px")
.style("top", coord[1]+10+"px")
.style("opacity", 1);
})
.on("mouseout",function() {
if (mapType == "choropleth") {
d3.select(this)
.style("fill",function(d) {
var fixedfips = parseInt(d.id);
if (typeof nestedData[fixedfips] === "undefined" || typeof nestedData[fixedfips][selectedSeq] === "undefined") {
return choropleth["null"];
} else {
return choropleth[quantize(nestedData[fixedfips][selectedSeq][0][selectedField])];
}
})
}
tooltipContainer
.style("left","-1000px")
.style("opacity", 0);
});
d3.selectAll(".dm-sequence-btn").on("click",function() {
id = this.id.substr(1);
selectedSeq = id;
d3.selectAll(".dm-sequence-btn").classed("active",false);
d3.select("#"+this.id).classed("active",true);
if (mapType == "bubble") {
d3.selectAll(".dm-dots")
.data(mapData[id])
.call(changeSeq);
} else {
d3.selectAll(".dm-county").call(getFill);
}
});
d3.selectAll(".dm-fld-btn").on("click",function() {
id = this.id.substr(1);
selectedField = id;
d3.selectAll(".dm-fld-btn").classed("active",false);
d3.select("#"+this.id).classed("active",true);
if (mapType == "bubble") {
radius.domain([0, minmax[selectedField][endSeq][1]])
d3.selectAll(".dm-dots").call(changeSeq);
legendSvg.selectAll(".dm-legend-text")
.data([1,minmax[selectedField][endSeq][1]])
.call(changeLegendText);
} else {
quantize
.domain([0, minmax[selectedField][selectedSeq][1]/2])
.range(d3.range(9).map(function(i) { return "q" + i + "-9"; }));
d3.selectAll(".dm-county").call(getFill);
}
});
function getFill(selection) {
selection
.transition()
.duration(1000)
.style("fill",function(d) {
var fixedfips = parseInt(d.id);
if (typeof nestedData[fixedfips] === "undefined" || typeof nestedData[fixedfips][selectedSeq] === "undefined") {
return choropleth["null"];
} else {
return choropleth[quantize(nestedData[fixedfips][selectedSeq][0][selectedField])];
}
})
}
function changeLegendText(selection) {
selection
.transition()
.duration(1000)
.text(function(d) { return d+" "+selectedField });
}
function changeSeq(selection) {
selection
.transition()
.duration(1000)
.attr("r", function(d) {
var dotsize = (radius(d[selectedField]) >= 0) ? radius(d[selectedField]) : 0;
return dotsize;
});
}
}
}