-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
701 lines (613 loc) · 24.6 KB
/
index.html
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
<meta name="author" content="Oliver Davis">
<meta name="description" content="spACE maps how the nature and nurture of complex
human traits varies geographically within populations. It is developed by the
Dynamic Genetics lab (http://dynamicgenetics.org), part of the MRC Integrative
Epidemiology Unit at the University of Bristol, UK.
Copyright (C) Oliver Davis 2020. This program is free software: you can
redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version. See https://www.gnu.org/licenses/">
<title>Dynamic Genetics spACE</title>
<link href="css/singlePageTemplate.css" rel="stylesheet" type="text/css">
<!--The following script tag downloads a font from the Adobe Edge Web Fonts server for use within the web page. We recommend that you do not modify it.-->
<script>var __adobewebfontsappname__="dreamweaver"</script>
<script src="http://use.edgefonts.net/source-sans-pro:n2:default.js" type="text/javascript"></script>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<!-- Mapbox headers -->
<script src="https://api.mapbox.com/mapbox-gl-js/v1.1.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.1.1/mapbox-gl.css" rel="stylesheet" />
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="./js/m4p.js"></script>
</head>
<body>
<!-- Main Container -->
<div class="container">
<div id="info"><div id="info-text">
<h2>spACE</h2>
<p>spACE maps how the nature and nurture of complex human traits varies
geographically within populations. It is developed by the Dynamic Genetics lab,
part of the MRC Integrative Epidemiology Unit at the University of Bristol, UK.
The approach uses data from cohorts of twins to break down the variation in
complex traits into genetic (A) and environmental influences (C and E;
technically anything other than DNA sequence).</p>
<p>The maps here show genetic and environmental influences on autistic traits
vary in the UK and Sweden. Use the buttons in the side bar to select maps showing genetic (A) or
environmental (E) influences in either country (C is approximately zero across
all maps, so is not shown here). On each map, red points are high compared to
the mean, whereas blue points are low. The distribution of values is shown in
the side bar histogram, coloured in the same way as the map. Selecting a point
on the map shows its value and where this value lies in the histogram (with 95%
confidence intervals in grey).</p>
<p>The map models raw variance, so A and E are not constrained to add up to one
in any location. However, the values can be roughly interpreted as the proportion
of the variation in the population explained by genetic or environmental
differences among people of the same ancestral background at each location,
highlighting how the local environment can draw out or mask genetic influences
on human characteristics.</p>
</div></div>
<!-- Map Container -->
<div id="map">
<a href="https://dynamicgenetics.org"><img id="logo" src="./images/Dynamic Genetics logo.png" alt="Dynamic Genetics"></a>
<!-- Chart Container -->
<div class="chart"><svg id="histogram"></svg>
<div class="buttons">
<div id="location"><div id="uk" class="selectedLocation"><span>UK</span></div><div id="sweden"><span>Sweden</span></div></div>
<div id="ace"><div id="a" class="selectedComponent"><span>A</span></div><div id="c"><span>C</span></div><div id="e"><span>E</span></div></div>
</div>
</div>
</div>
<!-- Map Script -->
<script src="./js/mapbox-access-token.js"></script>
<script>
const width=300;
const height=300;
const margin = ({top: 20, right: 20, bottom: 35, left: 47})
const svg = d3.select("#histogram");
const intervals = svg.append("g");
const sidebar = d3.select("div.chart");
const open_close = sidebar.append("svg").attr("id","open_close");
open_close.append("circle").attr("id", "opener").attr("cx", 20).attr("cy", 20).attr("r", 20).attr("fill", "#fff").attr("opacity", 0.5);
let open = true;
const cross_lines = open_close.append("g").attr("class", "cross_lines");
cross_lines.append("path").attr("d", "M 20 10 V 30").attr("stroke", "#4c4c4c").attr("stroke-width", 4).attr("stroke-linecap", "round");
cross_lines.append("path").attr("d", "M 10 20 H 30").attr("stroke", "#4c4c4c").attr("stroke-width", 4).attr("stroke-linecap", "round");
// cross_lines.attr("transform", "rotate(-45, 20, 20)");
open_close.style("transform", "rotate(-45deg)");
d3.select("#opener").on("click", e => {
if(open){
// console.log("closing");
sidebar.transition().duration(750).style("right", "-301px");
// cross_lines.transition().duration(750).attr("transform", "rotate(0, 20, 20)");
open_close.transition().duration(750).style("transform", "rotate(90deg)");
open = false;
} else {
// console.log("opening");
sidebar.transition().duration(750).style("right", "0px");
// cross_lines.transition().duration(750).attr("transform", "rotate(-45, 20, 20)");
open_close.transition().duration(750).style("transform", "rotate(-45deg)");
open = true;
}
});
const info_button = sidebar.append("svg").attr("id","info_button");
info_button.append("circle").attr("id", "informer").attr("cx", 20).attr("cy", 20).attr("r", 20).attr("fill", "#fff").attr("opacity", 0.5);
const ie = info_button.append("g").attr("id", "ie");
ie.append("path").attr("d", "M 20 21 V 29").attr("stroke", "#4c4c4c").attr("stroke-width", 4).attr("stroke-linecap", "round");
ie.append("circle").attr("cx", 20).attr("cy", 13).attr("r", 2).attr("stroke", "#4c4c4c");
d3.select("#informer").on("click", e => {
// console.log("clicked");
d3.select("#info").style("visibility", "visible");
});
const info_text = d3.select("#info-text");
const close_info = info_text.append("svg").attr("id","close_info");
close_info.append("circle").attr("id", "closer").attr("cx", 20).attr("cy", 20).attr("r", 20).attr("fill", "#fff").attr("opacity", 0.5);
const ex = close_info.append("g").attr("class", "cross_lines");
ex.append("path").attr("d", "M 20 10 V 30").attr("stroke", "#4c4c4c").attr("stroke-width", 4).attr("stroke-linecap", "round");
ex.append("path").attr("d", "M 10 20 H 30").attr("stroke", "#4c4c4c").attr("stroke-width", 4).attr("stroke-linecap", "round");
d3.select("#closer").on("click", e => {
// console.log("clicked");
d3.select("#info").style("visibility", "hidden");
});
const promiseSweden = d3.json("./data/asdSweden.json");
const promiseUK = d3.json("./data/asdUK.json");
Promise.all([promiseSweden, promiseUK]).then(data => {
let sweden, uk;
[sweden, uk] = data;
// console.log(sweden);
// console.log(uk);
function getColourScale(values){
let x = d3.scaleLinear()
.domain(d3.extent(values)).nice();
let colourScale = d3.scaleLinear()
.domain([x.domain()[0],
m4p.percentile(values,4),
d3.mean(values),
m4p.percentile(values,96),
x.domain()[1]])
.range(["#20D2FF","#20D2FF","#3F1466","#FF2040","#FF2040"])
.interpolate(d3.interpolateHcl);
// console.log([x.domain()[0],
// m4p.percentile(values,4),
// d3.mean(x.domain()),
// m4p.percentile(values,96),
// x.domain()[1]]);
return colourScale;
}
// let values = uk.features.map(d => d.properties.A);
//
// let x = d3.scaleLinear()
// .domain(d3.extent(values)).nice()
// .range([margin.left, width - margin.right]);
//
// let histogram = d3.histogram().domain(x.domain()).thresholds(x.ticks(40));
//
// // console.log(histogram)
//
// let bins = histogram(values)
//
// // console.log(bins);
// let means = d3.zip(bins.map(d => d.x0),bins.map(d => d.x1)).map(d => d3.mean(d));
// // console.log(means);
//
// let y = d3.scaleLinear()
// .domain([0, d3.max(bins, d => d.length)]).nice()
// .range([height - margin.bottom, margin.top])
// console.log(x.domain());
// m4p.percentile(values,4);
// m4p.percentile(values,96);
// let colourScale = d3.scaleLinear().domain([x.domain()[0],m4p.percentile(values,4),d3.mean(x.domain()),m4p.percentile(values,96),x.domain()[1]]).range(["#20D2FF","#20D2FF","#3F1466","#FF2040","#FF2040"]).interpolate(d3.interpolateHcl);
let colourScale_uk_a = getColourScale(uk.features.map(d => d.properties.A));
uk.features.forEach(function(d){d.properties.colorA = colourScale_uk_a(d.properties.A)});
let colourScale_uk_e = getColourScale(uk.features.map(d => d.properties.E));
uk.features.forEach(function(d){d.properties.colorE = colourScale_uk_e(d.properties.E)});
let colourScale_sweden_a = getColourScale(sweden.features.map(d => d.properties.A));
sweden.features.forEach(function(d){d.properties.colorA = colourScale_sweden_a(d.properties.A)});
let colourScale_sweden_e = getColourScale(sweden.features.map(d => d.properties.E));
sweden.features.forEach(function(d){d.properties.colorE = colourScale_sweden_e(d.properties.E)});
// console.log(data);
// console.log([x.domain()[0],d3.mean(x.domain()),x.domain()[1]]);
// console.log(means.map(d => colourScale(d)));
function drawHistogram(chart_area, name, values, height, width, margin){
const chart = chart_area.append("g").attr("id", name);
let x = d3.scaleLinear()
.domain(d3.extent(values)).nice()
.range([margin.left, width - margin.right]);
let colourScale = getColourScale(values);
let histogram = d3.histogram().domain(x.domain()).thresholds(x.ticks(40));
let bins = histogram(values);
let means = d3.zip(bins.map(d => d.x0),bins.map(d => d.x1)).map(d => d3.mean(d));
let y = d3.scaleLinear()
.domain([0, d3.max(bins, d => d.length)]).nice()
.range([height - margin.bottom, margin.top]);
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.attr("class","axis_white")
.call(d3.axisBottom(x).tickSizeOuter(0))
// .call(d3.axisBottom(x).tickSizeOuter(0).tickFormat(d3.format("~f")))
.call(g => g.append("text")
.attr("x", width - margin.right)
.attr("y", -4)
.attr("fill", "#fff")
.attr("font-weight", "bold")
.attr("text-anchor", "end"))
// .text(uk.x))
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.attr("class","axis_white")
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 4)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.attr("fill","#fff"))
// .text(uk.y))
let bar = chart.append("g")
.selectAll("rect")
.data(bins)
.join("rect")
.attr("x", d => x(d.x0) + 1)
.attr("width", d => Math.max(0, x(d.x1) - x(d.x0) - 1))
.attr("y", d => y(d.length))
.attr("height", d => y(0) - y(d.length))
.attr("fill", d => colourScale(d3.mean([d.x0,d.x1])));
chart.append("g")
.call(xAxis)
.selectAll("text")
.attr("transform", "rotate(25)")
.style("text-anchor", "start");
// chart.append("g")
// .call(xAxis)
// .selectAll("text")
// .attr("y", 0)
// .attr("x", 9)
// .attr("dy", ".35em")
// .attr("transform", "rotate(20)")
// .style("text-anchor", "start");
// text label for the x axis
chart.append("text")
.attr("transform",
"translate(" + (width/2) + " ," +
(height) + ")")
.attr("fill", "#fff")
.style("text-anchor", "middle")
.text("variance");
chart.append("g")
.call(yAxis);
// text label for the y axis
chart.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 2)
.attr("x",0 - (height / 2))
.attr("dy", "1em")
.attr("fill", "#fff")
.style("text-anchor", "middle")
.text("locations");
chart.attr("display", "none");
}
drawHistogram(svg, "uk_a", uk.features.map(d => d.properties.A), height, width, margin);
drawHistogram(svg, "uk_e", uk.features.map(d => d.properties.E), height, width, margin);
drawHistogram(svg, "sweden_a", sweden.features.map(d => d.properties.A), height, width, margin);
drawHistogram(svg, "sweden_e", sweden.features.map(d => d.properties.E), height, width, margin);
d3.select("#uk_a").attr("display", "display");
//
// xAxis = g => g
// .attr("transform", `translate(0,${height - margin.bottom})`)
// .attr("class","axis_white")
// .call(d3.axisBottom(x).tickSizeOuter(0))
// .call(g => g.append("text")
// .attr("x", width - margin.right)
// .attr("y", -4)
// .attr("fill", "#fff")
// .attr("font-weight", "bold")
// .attr("text-anchor", "end"))
// // .text(uk.x))
//
// yAxis = g => g
// .attr("transform", `translate(${margin.left},0)`)
// .attr("class","axis_white")
// .call(d3.axisLeft(y))
// .call(g => g.select(".domain").remove())
// .call(g => g.select(".tick:last-of-type text").clone()
// .attr("x", 4)
// .attr("text-anchor", "start")
// .attr("font-weight", "bold")
// .attr("fill","#fff"))
// // .text(uk.y))
//
//
// // console.log(uk.x);
// // console.log(sweden.x);
//
// let bar = chart.append("g")
// .selectAll("rect")
// .data(bins)
// .join("rect")
// .attr("x", d => x(d.x0) + 1)
// .attr("width", d => Math.max(0, x(d.x1) - x(d.x0) - 1))
// .attr("y", d => y(d.length))
// .attr("height", d => y(0) - y(d.length))
// .attr("fill", d => colourScale_uk_a(d3.mean([d.x0,d.x1])));
//
// chart.append("g")
// .call(xAxis);
//
// // text label for the x axis
// chart.append("text")
// .attr("transform",
// "translate(" + (width/2) + " ," +
// (height) + ")")
// .attr("fill", "#fff")
// .style("text-anchor", "middle")
// .text("variance");
//
// chart.append("g")
// .call(yAxis);
//
// // text label for the y axis
// chart.append("text")
// .attr("transform", "rotate(-90)")
// .attr("y", 2)
// .attr("x",0 - (height / 2))
// .attr("dy", "1em")
// .attr("fill", "#fff")
// .style("text-anchor", "middle")
// .text("locations");
let map = new mapboxgl.Map({
container: 'map',
//style: "mapbox://styles/mapbox/dark-v9",
//style: "mapbox://styles/mapbox/light-v9",
//style: "mapbox://styles/oliverdavis/cj6qkooen3n022rp7cefenea8",
style: "mapbox://styles/oliverdavis/cj6qau4kn3dsv2rme72b1rezu",
//style: "mapbox://styles/oliverdavis/cj8mvdyhx78ss2rno8gr6lpoq",
center: [-2.89479, 54.093409],
zoom: 5,
maxZoom: 11
});
//let dataColours = ["#20D2FF","#23BCEE","#26A7DD","#2A92CC","#2D7DBB","#3168AA","#345399",
//"#383E88","#3B2977","#3F1466","#541561","#69165D","#7E1859","#941955","#A91A50","#BE1C4C",
//"#D41D48","#E91E44","#FF2040"];
// let numberScale = d3.scaleLinear().domain([0,100]).range([100,200]);
// let selectedCountry = "uk";
// let selectedComponent = "a";
let selectedLocation = d3.select(".selectedLocation").attr("id");
let selectedComponent = d3.select(".selectedComponent").attr("id");
let currentMap = selectedLocation + "." + selectedComponent;
let currentChart = selectedLocation + "_" + selectedComponent;
d3.selectAll("#ace div").on("click", function(){
let previousMap = currentMap;
d3.selectAll("#ace div").classed("selectedComponent", false);
d3.select("#"+this.id).classed("selectedComponent", true);
selectedLocation = d3.select(".selectedLocation").attr("id");
selectedComponent = d3.select(".selectedComponent").attr("id");
currentMap = selectedLocation + "." + selectedComponent;
//console.log(previousMap);
//console.log(currentMap);
// updateMap()
map.setLayoutProperty(previousMap, 'visibility', 'none');
map.setLayoutProperty(currentMap, 'visibility', 'visible');
// updateChart()
let previousChart = previousMap.split(".").join("_");
// console.log(previousChart);
d3.select("#" + previousChart).attr("display", "none");
currentChart = currentMap.split(".").join("_");
// console.log(currentMap);
d3.select("#" + currentChart).attr("display", "display");
});
d3.selectAll("#location div").on("click", function(){
let previousMap = currentMap;
d3.selectAll("#location div").classed("selectedLocation", false);
d3.select("#"+this.id).classed("selectedLocation", true);
selectedLocation = d3.select(".selectedLocation").attr("id");
selectedComponent = d3.select(".selectedComponent").attr("id");
currentMap = selectedLocation + "." + selectedComponent;
// updateMap()
map.setLayoutProperty(previousMap, 'visibility', 'none');
map.setLayoutProperty(currentMap, 'visibility', 'visible');
if(previousMap.split(".")[0] !== selectedLocation){
if(selectedLocation == "uk"){
map.flyTo({
center: [-2.89479, 54.093409],
zoom: 5,
essential: true // this animation is considered essential with respect to prefers-reduced-motion
})
} else {
map.flyTo({
center: [16.325556, 62.3875],
zoom: 4,
essential: true // this animation is considered essential with respect to prefers-reduced-motion
})
}
};
// updateChart()
let previousChart = previousMap.split(".").join("_");
// console.log(previousChart);
d3.select("#" + previousChart).attr("display", "none");
currentChart = currentMap.split(".").join("_");
// console.log(currentMap);
d3.select("#" + currentChart).attr("display", "display");
});
// let currentMap = 'uk.a';
map.on('load', function () {
map.addSource("aceUK", {type: "geojson", data: uk});
map.addLayer({
"id": "uk.a",
"type": "circle",
"source": "aceUK",
"layout": {
"visibility": "visible"
},
"paint": {
'circle-radius': ['interpolate', ["exponential", 1.75], ['zoom'], 1, 2, 11, 30],
"circle-color": ["get","colorA"],
'circle-blur': ['interpolate', ["exponential", 3], ['zoom'], 1, 0, 11, 0.5],
'circle-opacity': ['interpolate', ["exponential", 3], ['zoom'], 1, 1, 11, 0.7]
}
});
map.addLayer({
"id": "uk.e",
"type": "circle",
"source": "aceUK",
"layout": {
"visibility": "none"
},
"paint": {
'circle-radius': ['interpolate', ["exponential", 1.75], ['zoom'], 1, 2, 11, 30],
"circle-color": ["get","colorE"],
'circle-blur': ['interpolate', ["exponential", 3], ['zoom'], 1, 0, 11, 0.5],
'circle-opacity': ['interpolate', ["exponential", 3], ['zoom'], 1, 1, 11, 0.7]
}
});
map.addSource("aceSweden", {type: "geojson", data: sweden});
map.addLayer({
"id": "sweden.a",
"type": "circle",
"source": "aceSweden",
"layout": {
"visibility": "none"
},
"paint": {
'circle-radius': ['interpolate', ["exponential", 1.75], ['zoom'], 1, 2, 11, 30],
"circle-color": ["get","colorA"],
'circle-blur': ['interpolate', ["exponential", 3], ['zoom'], 1, 0, 11, 0.5],
'circle-opacity': ['interpolate', ["exponential", 3], ['zoom'], 1, 1, 11, 0.7]
}
});
map.addLayer({
"id": "sweden.e",
"type": "circle",
"source": "aceSweden",
"layout": {
"visibility": "none"
},
"paint": {
'circle-radius': ['interpolate', ["exponential", 1.75], ['zoom'], 1, 2, 11, 30],
"circle-color": ["get","colorE"],
'circle-blur': ['interpolate', ["exponential", 3], ['zoom'], 1, 0, 11, 0.5],
'circle-opacity': ['interpolate', ["exponential", 3], ['zoom'], 1, 1, 11, 0.7]
}
});
// } );
});
// Create a popup, but don't add it to the map yet.
let popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
});
//let popupFlag = false;
//let markerBase = y(0);
//let marker = svg.append("g").attr("transform", `translate(0,${margin.top})`).append("path").attr("d",`M 0 0 L 0 ${markerBase}`).attr("stroke","#F5EF38").classed("marker",true).attr("opacity",0);
const maps = ["uk.a", "uk.e", "sweden.a", "sweden.e"];
maps.forEach((item, i) => {
map.on('mouseover', item, function(e) {
// Change the cursor style as a UI indicator.
map.getCanvas().style.cursor = 'pointer';
});
});
// map.on('mouseover', currentMap, function(e) {
// // Change the cursor style as a UI indicator.
// map.getCanvas().style.cursor = 'pointer';
// });
maps.forEach((item, i) => {
map.on('click', item, function(e) {
// Change the cursor style as a UI indicator.
//map.getCanvas().style.cursor = 'pointer';
let coordinates = e.features[0].geometry.coordinates.slice();
let description = d3.format(".2f")(e.features[0].properties[item.split(".")[1].toUpperCase()]);
// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
// Populate the popup and set its coordinates
// based on the feature found.
popup.setLngLat(coordinates)
.setHTML(description)
.addTo(map);
d3.selectAll(".marker").remove();
let values;
let dataname = item.split(".");
if(dataname[0] === "uk"){
if(dataname[1] === "a"){
values = uk.features.map(d => d.properties.A);
} else {
values = uk.features.map(d => d.properties.E);
}
} else {
if(dataname[1] === "a"){
values = sweden.features.map(d => d.properties.A);
} else {
values = sweden.features.map(d => d.properties.E);
}
}
// console.log(e.features[0]);
// console.log(e.features[0].properties[item.split(".")[1].toUpperCase()]);
let x = d3.scaleLinear()
.domain(d3.extent(values)).nice()
.range([margin.left, width - margin.right]);
let markerPlace = x(e.features[0].properties[item.split(".")[1].toUpperCase()]);
// let markerBase = y(0);
let markerBase = height - margin.bottom - margin.top;
let marker = d3.select("#" + item.split(".").join("_"))
.append("g")
.attr("transform", `translate(${markerPlace},${margin.top})`)
.append("path").attr("d",`M 0 0 L 0 ${markerBase}`)
.attr("stroke","#F5EF38")
.classed("marker",true)
.attr("shape-rendering", "crispEdges");
let lowerint = x(e.features[0].properties[item.split(".")[1].toUpperCase() + "_lo"])
let upperint = x(e.features[0].properties[item.split(".")[1].toUpperCase() + "_hi"])
let intwidth = upperint - lowerint;
let intheight = height - margin.bottom - margin.top;
let confint = intervals
.append("g")
.append("rect")
.attr("x", lowerint)
.attr("y", margin.top)
.attr("width", intwidth)
.attr("height", intheight)
.attr("fill", "#353535")
.classed("marker",true);
//popupFlag = true;
//marker.attr("transform", `translate(${markerPlace},${margin.top})`).attr("opacity",1);
});
});
// map.on('click', currentMap, function(e) {
// // Change the cursor style as a UI indicator.
// //map.getCanvas().style.cursor = 'pointer';
//
// let coordinates = e.features[0].geometry.coordinates.slice();
// let description = d3.format(".2f")(e.features[0].properties.A);
//
// // Ensure that if the map is zoomed out such that multiple
// // copies of the feature are visible, the popup appears
// // over the copy being pointed to.
// while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
// coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
// }
//
// // Populate the popup and set its coordinates
// // based on the feature found.
// popup.setLngLat(coordinates)
// .setHTML(description)
// .addTo(map);
//
// d3.selectAll(".marker").remove();
//
// let markerPlace = x(e.features[0].properties.A);
// // let markerBase = y(0);
// let markerBase = height - margin.bottom - margin.top;
//
// let marker = chart
// .append("g")
// .attr("transform", `translate(${markerPlace},${margin.top})`)
// .append("path").attr("d",`M 0 0 L 0 ${markerBase}`)
// .attr("stroke","#F5EF38")
// .classed("marker",true)
// .attr("shape-rendering", "crispEdges");
//
// //popupFlag = true;
//
// //marker.attr("transform", `translate(${markerPlace},${margin.top})`).attr("opacity",1);
// });
maps.forEach((item, i) => {
map.on('mouseleave', item, function() {
map.getCanvas().style.cursor = '';
popup.remove();
//if(popupFlag==true){
// popupFlag = false;
// d3.selectAll(".marker").transition().duration(2000).attr("opacity",50);
//}
d3.selectAll(".marker").remove();
});
});
// map.on('mouseleave', currentMap, function() {
// map.getCanvas().style.cursor = '';
// popup.remove();
// //if(popupFlag==true){
// // popupFlag = false;
// // d3.selectAll(".marker").transition().duration(2000).attr("opacity",50);
// //}
// d3.selectAll(".marker").remove();
// });
});
// log the zoom level to console
/* map.on("zoom", function(){
console.log(map.getZoom());
}) */
</script>
</div>
<!-- Main Container Ends -->
</body>
</html>