-
Notifications
You must be signed in to change notification settings - Fork 1
/
new-index.js
411 lines (343 loc) · 13.9 KB
/
new-index.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//gets width of container div and makes that the map width
var width = parseInt(d3.select('#container').style('width')),
height = 700;
//add svg element to container div
var svg = d3.select("#container")
.append("svg")
.attr("width", width)
.attr("height", height);
//geojsons we are loading in
var files = [
"geojsons/europeWrussia2.geojson",
"geojsons/bubbleChart.geojson",
"geojsons/mapBubbles29-1.geojson",
"geojsons/movingBubbles26-1.geojson",
];
//empty array for promise handling
var promises = [];
files.forEach(function (url) {
promises.push(d3.json(url))
});
Promise.all(promises).then(function (values) {
addBaseMap(values[0]);
addBubbleChartBubbles(values[1]);
addMapBubbles(values[2]);
addMovingBubbles(values[3]);
});
//create separate items for each geojson
var basemapG = svg.append("g"),
bubbleChartG = svg.append("g"),
mapBubblesG = svg.append("g"),
movingBubblesG = svg.append("g");
//set projection for map
var projection = d3.geoMercator();
var geoPath = d3.geoPath().projection(projection);
//function to scale the bubble chart circles
var radius = d3.scaleLog();
/************************** ADD EUROPE BASEMAP ON THE RIGHT SIDE **********************/
function addBaseMap(basemap) {
//project europe and have it fit within the div
// +660 to push it over to the right side
projection.fitSize([width + 660, height], basemap);
basemapG
.selectAll("path")
.data(basemap.features)
.enter()
.append("path")
.attr("d", geoPath)
.attr("stroke", "#323232ff") //stroke color black
.attr("stroke-width", 0.5)
.attr("fill", "#0f0f0f") //fill color black
.attr("class", "europe"); //set the class of this element to europe
}
/************************** ADD BUBBLES ON THE BUBBLE CHART SIDE **********************/
function addBubbleChartBubbles(bubbleChartBubbles) {
//move all the bubbles up by 100 to center them more
for (var b in bubbleChartBubbles.features) {
bubbleChartBubbles.features[b].geometry.coordinates[1] = bubbleChartBubbles.features[b].geometry.coordinates[1] - 100;
}
bubbleChartG.selectAll("circle")
.data(bubbleChartBubbles.features, function (d) {
return d;
}).enter().append("circle")
.attr('r', function (d) {
return radius(d.properties.speakers) * 2 //make the radius of the bubbles the log of # of speakers *2
})
.attr('cx', function (d) {
return d.geometry.coordinates[0] //don't project bubble chart circles
})
.attr('cy', function (d) {
return d.geometry.coordinates[1] //don't project bubble chart circles
})
.attr('stroke', function (d) {
return d.properties.color //color each bubble chart bubble
})
//.attr('stroke-width', 0.2)
.attr('fill', '#323232ff') //set fill color same as background to it looks empty
.attr("class", "bubbleChartBubbles") //set class to bubbleChartBubbles
.attr("id", function (d) {
return "bubbleChart-" + d.properties.ID // set id to wals_code
})
}
/************************** ADD INVISIBLE BUBBLES ON RIGHT SIDE **********************/
function addMapBubbles(mapBubbles) {
mapBubblesG.selectAll("circle")
.data(mapBubbles.features.sort(function (a, b) {
return b.properties.speakers - a.properties.speakers;
}), function (d) {
return d;
}).enter().append("circle")
.attr('r', function (d) {
return radius(d.properties.speakers) * 2
})
.attr('cx', function (d) {
return projection(d.geometry.coordinates)[0]
})
.attr('cy', function (d) {
return projection(d.geometry.coordinates)[1]
})
.attr('fill-opacity', 0) //make them invisible
.attr("class", "mapBubbles")
.attr("id", function (d) {
return "mapBubbles-" + d.properties.wals_code //give each bubble wals code as id
})
}
/************************** ADD MOVING BUBBLES **********************/
function addMovingBubbles(movingBubbles) {
//initialize popup div
var div = d3.select("body").append("div")
.attr("class", "popup")
.style("opacity", 0)
.style("display", "none")
//remove popup when you click anywhere
d3.select('body').on('click', function (d) {
div.style("opacity", 0)
.style("padding", 10)
.style("display", "none")
})
//move all the bubbles up by 100 to center them more
for (var b in movingBubbles.features) {
movingBubbles.features[b].properties.coords = [movingBubbles.features[b].properties.x, movingBubbles.features[b].properties.y]
movingBubbles.features[b].properties.bubbley = movingBubbles.features[b].properties.bubbley - 100;
}
movingBubblesG.selectAll("circle")
.data(movingBubbles.features.sort(function (a, b) {
return b.properties.speakers - a.properties.speakers;
}), function (d) {
return d;
})
.enter().append("circle")
.attr('r', function (d) {
return radius(d.properties.speakers) * 2
})
.attr('cx', function (d) {
return projection(d.geometry.coordinates)[0]
})
.attr('cy', function (d) {
return projection(d.geometry.coordinates)[1]
})
.attr('fill', function (d) {
return d.properties.color
})
.attr('opacity', 1)
.attr("class", function (d) {
return "movingBubbles " + d.properties.family + " " + d.properties.Genus_CAPS.toLowerCase()
})
.style("cursor", "pointer")
.attr("id", function (d) {
return "movingBubbles" + d.properties.wals_code_move;
})
.on("mouseover", function (d) {
tooltip.style("display", null);
})
.on("mouseout", function () {
tooltip.style("display", "none");
})
.on("mousemove", function (d) {
var xPosition = d3.mouse(this)[0] - 15;
var yPosition = d3.mouse(this)[1] - 40;
tooltip.attr("transform", "translate(" + xPosition + "," + yPosition + ")");
tooltip.select('text').html(d.properties.Name)
})
.on("touchstart", nozoom)
.on("touchmove", nozoom)
.on("click", clicked)
.call(d3.drag()
.on("start", dragStart)
.on("drag", dragged)
.on("end", dragEnd));
function nozoom() {
d3.event.preventDefault();
}
function clicked(d) {
if (d3.event.defaultPrevented) return;
div.transition()
.duration(200)
.style("opacity", .8)
.style("display", null)
.style("padding", 10);
var languageLink = '<a href="https://wals.info/languoid/lect/wals_code_' + d.properties.wals_code_move + '" target="_blank" style="color:'+ d.properties.color +'">' + d.properties.Name + '</a>';
var familyLink = '<a href="https://wals.info/languoid/family/' + d.properties.family.replace(/\W/g, '').toLowerCase() + '" target="_blank" style="color:'+ d.properties.color +'">' + d.properties.family + '</a>';
var genusLink = '<a href="https://wals.info/languoid/genus/' + d.properties.Genus_CAPS.replace(/\W/g, '').toLowerCase() + '" target="_blank" style="color:'+ d.properties.color +'">' + d.properties.Genus_CAPS + '</a>';
div.html("<b>Language: " + languageLink + "<br>" +
"<b>Genus: " + genusLink + "<br>" +
"<b>Family: " + familyLink + "<br>" +
"<b>Approx. # of Speakers: <p class='speakers' style='color:"+ d.properties.color +"'>" + d.properties.speakers + "</p> </b>")
.style("left", (d3.event.pageX + 28) + "px")
.style("top", (d3.event.pageY - 28) + "px");
}
/************************** TOOLTIP FUNCTIONS **********************/
var tooltip = svg.append("g")
.attr("class", "tooltip")
.style("display", "none")
tooltip.append("rect")
//.attr("width", 100)
//.attr("height", 20)
//.style("opacity", 0.5);
tooltip.append("text")
.attr("x", 1)
.attr("dy", "1.0em")
.style("text-align", "center")
.attr('fill', 'whitesmoke')
.attr("font-size", "15px")
//.attr("font-weight", "bold");
/************************** DRAG FUNCTIONS **********************/
function dragStart(d) {
//literally causes an error BUT works so no idea
d3.event.preventDefault();
tooltip.style("display", "none");
var x = d3.select(this).attr("cx");
var y = d3.select(this).attr("cy");
d3.select(this).classed("active", true).raise()
}
function dragged(d) {
tooltip.style("display", "none");
d3.select(this)
.attr("cx", d3.event.x)
.attr("cy", d3.event.y);
}
function dragEnd(d) {
if (d3.select(this).classed('leftside')) {
/*d3.select('#mapBubbles-' + d.properties.wals_code_move)
.attr('stroke', "yellow") */
var bubbleMovedX = this.attributes.cx.value;
var bubbleMovedY = this.attributes.cy.value;
var mapBubbleX = d3.select('#mapBubbles-' + d.properties.wals_code_move).attr('cx');
//console.log(d.properties.wals_code_move)
var mapBubbleY = d3.select('#mapBubbles-' + d.properties.wals_code_move).attr('cy');
if (Math.sqrt(Math.pow(Math.abs(bubbleMovedX - mapBubbleX), 2) + Math.pow(Math.abs(bubbleMovedY - mapBubbleY), 2)) < 50) {
d3.select(this)
.classed("active", false)
.classed("leftside", false)
.transition()
.duration(500)
.attr('cx', function (d) {
return mapBubbleX
})
.attr('cy', function (d) {
return mapBubbleY
})
} else {
d3.select(this)
.classed("active", false)
.transition()
.duration(1500)
.attr('cx', function (d) {
return d.properties.bubblex
})
.attr('cy', function (d) {
return d.properties.bubbley
})
}
} else if (!d3.select(this).classed('leftside')) {
/* d3.select('#bubbleChart-' + d.properties.wals_code_move)
.attr('stroke', "yellow") */
var bubbleMovedX = this.attributes.cx.value;
var bubbleMovedY = this.attributes.cy.value;
var bubbleChartX = d3.select('#bubbleChart-' + d.properties.wals_code_move).attr('cx');
var bubbleChartY = d3.select('#bubbleChart-' + d.properties.wals_code_move).attr('cy');
if (Math.sqrt(Math.pow(Math.abs(bubbleMovedX - bubbleChartX), 2) + Math.pow(Math.abs(bubbleMovedY - bubbleChartY), 2)) < 50) {
d3.select(this)
.classed("active", false)
.classed("leftside", true)
.transition()
.duration(500)
.attr('cx', function (d) {
return bubbleChartX
})
.attr('cy', function (d) {
return bubbleChartY
})
} else {
d3.select(this)
.classed("active", false)
.transition()
.duration(1500)
.attr('cx', function (d) {
return projection(d.geometry.coordinates)[0]
})
.attr('cy', function (d) {
return projection(d.geometry.coordinates)[1]
})
}
}
}
d3.selectAll('.name').on('click', function (d) {
var name = this.id;
var selection = d3.selectAll('.' + name);
//console.log(selection.classed('leftside'));
if (selection.classed('leftside')) {
//d3.select(this).html('◁ ' + name.toUpperCase() + ' ▶')
selection
.classed('leftside', false).raise()
.transition()
.duration(1500)
.attr('cx', function (d) {
return projection(d.properties.coords)[0];
})
.attr('cy', function (d) {
return projection(d.properties.coords)[1];
})
} else {
//d3.select(this).html('◀ ' + name.toUpperCase() + ' ▷')
selection
.classed('leftside', true).raise()
.transition()
.duration(1500)
.attr('cx', function (d) {
return d.properties.bubblex
})
.attr('cy', function (d) {
return d.properties.bubbley
})
}
})
}
function toTheLeftToTheLeft() {
//d3.select('.left').html('◀');
//d3.select('.right').html('▷');
d3.selectAll('.movingBubbles')
.classed('leftside', true).raise()
.transition()
.duration(1500)
.attr('cx', function (d) {
return d.properties.bubblex
})
.attr('cy', function (d) {
return d.properties.bubbley
})
}
function toTheRight() {
//d3.select('.left').html('◁');
//d3.select('.right').html('▶');
d3.selectAll('.movingBubbles')
.classed('leftside', false).raise()
.transition()
.duration(1500)
.attr('cx', function (d) {
return projection(d.properties.coords)[0];
})
.attr('cy', function (d) {
return projection(d.properties.coords)[1];
})
}