-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
699 lines (605 loc) · 21.9 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
<!DOCTYPE>
<html>
<head>
<!--Part of the code to generate grid/axis/legends/data point hover are borrowed from lecture code-->
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>World Happiness Data Analysis</title>
<!--- D3 IMPORT HERE --->
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="https://d3js.org/topojson.v3.min.js"></script>
<script src="legend.js"></script>
<style>
g.mouseover rect {
fill: white;
stroke: #222;
stroke-width: 1px;
}
g.mouseover text {
font-family: Arial, sans-serif;
}
g.mouseover text:first-child {
font-weight: bold;
}
</style>
<style>
.country {
fill: #edf9fe;
}
.outline {
stroke: black;
stroke-width: 1px;
fill: none;
}
.slidecontainer {
width: 25%;
margin-top: 20px;
}
.slider {
-webkit-appearance: none;
width: 100%;
height: 15px;
border-radius: 5px;
background: #d3d3d3;
outline: none;
opacity: 0.7;
-webkit-transition: 0.2s;
transition: opacity 0.2s;
}
.container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin-top: 40px;
}
#scatter {
margin: auto;
}
#demo {
text-align: left;
margin: auto;
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
}
#selector {
text-align: right;
}
.btn-group label {
display: inline-block;
background-color: #ddd;
padding: 10px 20px;
font-family: sans-serif, Arial;
font-size: 14px;
font-weight: bold;
border: 2px solid #444;
border-radius: 4px;
}
.btn-group input[type='radio'] {
display: none;
}
.btn-group input[type='radio']:checked+label {
background-color: #bfb;
border-color: #4c4;
}
.btn-group label:hover {
background-color: #dfd;
}
</style>
</head>
<body>
<h1 style="text-align: center; font-size: 24pt;">World Happiness Data Analysis</h1>
<div id="demo">
<div class="container">
<div>
<p style="font-weight: bold; text-align: center;">Select a factor to explore</p>
<div id="selector" class="btn-group" role="group" aria-label="Basic radio toggle button group"
style=" margin-bottom:18pt;">
<input type="radio" class="btn-check" name="btnradio" id="btnradio1" autocomplete="off" checked
value="free" />
<label class="btn btn-outline-info" for="btnradio1">Freedom</label>
<input type="radio" class="btn-check" name="btnradio" id="btnradio2" autocomplete="off"
value="generos" />
<label class="btn btn-outline-info" for="btnradio2">Generosity</label>
<input type="radio" class="btn-check" name="btnradio" id="btnradio3" autocomplete="off"
value="life" />
<label class="btn btn-outline-info" for="btnradio3">Healthy Life Expectancy at Birth</label>
<input type="radio" class="btn-check" name="btnradio" id="btnradio4" autocomplete="off" checked
value="ladder" />
<label class="btn btn-outline-info" for="btnradio4">Life Ladder</label>
<input type="radio" class="btn-check" name="btnradio" id="btnradio5" autocomplete="off"
value="gdp" />
<label class="btn btn-outline-info" for="btnradio5">Log GDP Per Capita</label>
<input type="radio" class="btn-check" name="btnradio" id="btnradio6" autocomplete="off"
value="corr" />
<label class="btn btn-outline-info" for="btnradio6">Perceptions of Corruption</label>
<input type="radio" class="btn-check" name="btnradio" id="btnradio7" autocomplete="off"
value="support" />
<label class="btn btn-outline-info" for="btnradio7">Social Support</label>
</div>
</div>
<div style="display: flex; margin-top: 15px; border: 1px solid black">
<div id="region-legend" style="margin-right: 10px">
<span id="legend-title" style="color: black; font-weight: bold">Hover on a region</span>
</div>
<!-- <svg id="scatter" height="435" width="970" style="border: 1px solid black"></svg> -->
<svg id="scatter" height="435" width="970"></svg>
</div>
<div class="slidecontainer" style="margin-bottom: 18pt; text-align: center;">
<label for="year-select-label2" id="year" style="color: black; font-weight: bold;">Select a year
</label><br />
<input type="range" min="2015" max="2019" value="2015" class="slider" id="sliderRange" />
Year: <span id="sliderYear"></span>
</div>
<div id="mapCanvas" style="border: 1px solid black">
<svg id="map" width="800" height="500"></svg>
</div>
<div>
<svg id="colorLegend" height="80" width="500" style="background: #fff; margin-top: 30px"></svg>
</div>
</div>
</div>
<script>
let svg = d3.select('svg#scatter');
let width = svg.attr('width');
let height = svg.attr('height');
let margin = { top: 10, right: 300, bottom: 50, left: 50 };
let chartWidth = width - margin.left - margin.right;
let chartHeight = height - margin.top - margin.bottom;
let annotations = svg.append('g').attr('id', 'annotations');
let chartArea = svg
.append('g')
.attr('id', 'points')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
let requestData = async function () {
// Set global variables
let columnSelected = 'Freedom';
let yearSelected = 2015;
let regionSelected = undefined;
let circles = {};
// Read in dataset
// const world = await d3.json('./datasets/world.json');
// const countryCodes = await d3.json('./datasets/country-codes.json');
let dataOriginal = await d3.csv('aggregated.csv', d3.autoType);
console.log(dataOriginal);
// add initial global filter
dataOriginal = dataOriginal.filter((d) => {
return d['Region'] != null && d['Happiness Score'] != null && d['Happiness Score'] != '#N/A';
});
console.log(dataOriginal);
data = dataOriginal.filter((d) => {
return d[columnSelected] != null;
});
requestMapData(data);
// X axis Extent/Scale/Axis/Grid (dynamic by data selection)
let xExtent = d3.extent(data, (d) => d[columnSelected]);
let xScale = d3.scaleLinear().domain(xExtent).range([0, chartWidth]);
let bottomAxis = d3.axisBottom(xScale).tickFormat(d3.format('~f'));
let bottomGridlines = d3
.axisBottom(xScale)
.tickSize(-chartHeight - 10)
.tickFormat('');
let bottomAxisG = annotations
.append('g')
.attr('class', 'x axis')
.attr('transform', 'translate(' + margin.left + ',' + (chartHeight + margin.top + 10) + ')')
.call(bottomAxis);
let bottomGridlinesG = annotations
.append('g')
.attr('class', 'x gridlines')
.attr('transform', 'translate(' + margin.left + ',' + (chartHeight + margin.top + 10) + ')')
.attr('color', 'lightgrey')
.call(bottomGridlines);
let bottomAxisText = annotations
.append('g')
.append('text')
.attr('transform', 'translate(' + chartWidth / 2 + ',' + (chartHeight + margin.bottom) + ')')
.attr('text-anchor', 'middle')
.attr('font-family', 'sans-serif')
.attr('font-size', 13)
.style('font-weight', 'bold')
.text(columnSelected);
// Y axis Extent/Scale/Axis/Grid (fixed)
const yExtent = d3.extent(data, (d) => d['Happiness Score']); // hapiness score
const yScale = d3.scaleLinear().domain(yExtent).range([chartHeight, 0]);
let leftAxis = d3.axisLeft(yScale).tickFormat(d3.format('~f'));
let leftGridlines = d3
.axisLeft(yScale)
.tickSize(-chartWidth - 10)
.tickFormat('');
annotations
.append('g')
.attr('class', 'y axis')
.attr('transform', 'translate(' + (margin.left - 10) + ',' + margin.top + ')')
.call(leftAxis);
annotations
.append('g')
.attr('class', 'y gridlines')
.attr('transform', 'translate(' + (margin.left - 10) + ',' + margin.top + ')')
.attr('color', 'lightgrey')
.call(leftGridlines);
annotations
.append('text')
.attr('class', 'y label')
.attr('text-anchor', 'end')
.attr('y', 3)
.attr('x', -150)
.attr('dy', '.75em')
.attr('transform', 'rotate(-90)')
.attr('font-family', 'sans-serif')
.attr('font-size', 13)
.style('font-weight', 'bold')
.text('Happiness Score');
// 'region' Extent/Scale/Legend
// let regionDomain = data.map((d) => { return d['Region']; })
let regionDomain = [
'South Asia',
'East Asia',
'Southeast Asia',
'Central and Eastern Europe',
'Western Europe',
'Commonwealth of Independent States',
'Middle East and North Africa',
'Sub-Saharan Africa',
'Latin America and Caribbean',
'North America and ANZ',
];
regionDomain = regionDomain.filter(function (item, pos) {
return regionDomain.indexOf(item) == pos;
});
console.log('regionDomain', regionDomain);
let regionScale = d3
.scaleOrdinal()
.range(['#f3722cff', '#f3722cff', '#f3722cff', '#f9c74fff', '#f9c74fff', '#f9c74fff', '#4d908eff', '#4d908eff', '#277da1ff', '#277da1ff']);
// draw Region legend
regionDomain.forEach((d, index) => {
d3.select('div#region-legend')
.append('p')
.text(d)
// .style('display', 'list-item')
.style('color', regionScale(d))
.style('font-weight', 'bold')
.on('mouseover', function () {
updateDataPoints(d, yearSelected);
})
.on('mouseout', function () {
recoverByYear(yearSelected);
});
});
// draw data points by defalut year
recoverByYear(yearSelected);
d3.select('#sliderYear').text(yearSelected);
// hover over data points styles and actions
const mouseover = svg
.append('g')
.attr('class', 'mouseover')
.attr('transform', `translate(${margin.left + chartWidth + 15},${margin.top + 15})`);
function stringLen(str) {
const dummytext = mouseover.append('text').attr('class', 'legendtext').attr('visibility', 'hidden');
dummytext.text(str);
let len = dummytext.node().getComputedTextLength();
dummytext.remove();
return len;
}
const frame = mouseover.append('rect').attr('class', 'frame').attr('x', 0).attr('y', 0).attr('rx', 5).attr('ry', 5).attr('height', 190);
const textbox = mouseover.append('g').attr('transform', 'translate(10,10)');
const format1 = d3.format(',d');
let xMarker = chartArea
.append('line')
.attr('id', 'xMarker')
.attr('fill', 'none')
.attr('stroke', 'black')
.attr('stroke-width', 1)
.attr('y1', 0)
.attr('y2', chartHeight)
.attr('visibility', 'hidden');
let yMarker = chartArea
.append('line')
.attr('id', 'yMarker')
.attr('fill', 'none')
.attr('stroke', 'black')
.attr('stroke-width', 1)
.attr('x1', 0)
.attr('x2', chartWidth)
.attr('visibility', 'hidden');
updateCircleHover();
// update hover event listeners for updated 'circles'
function updateCircleHover() {
circles.on('mouseover', function () {
let selection = d3.select(this);
selection.raise();
mouseOverDataPoint(selection);
});
circles.on('mouseout', function () {
let selection = d3.select(this);
mouseOutDataPoint(selection);
});
}
function mouseOutDataPoint(circleSelected) {
circleSelected.transition().duration(200).attr('stroke-width', 1).attr('stroke', 'none').attr('r', 4).attr('opacity', 0.75);
textbox.attr('visibility', 'hidden');
frame.attr('visibility', 'hidden');
xMarker.attr('visibility', 'hidden');
yMarker.attr('visibility', 'hidden');
}
function mouseOverDataPoint(circleSelected) {
textbox.attr('visibility', '');
frame.attr('visibility', '');
xMarker.attr('visibility', '');
yMarker.attr('visibility', '');
let d = circleSelected.datum();
circleSelected.transition().duration(200).attr('stroke-width', 4).attr('stroke', 'black').attr('r', 6).attr('opacity', 1);
console.log(d);
textbox.html('');
let countryName = `Country: ${d['Country']}`;
let freedom = `Freedom: ${d['Freedom']} / 1`;
let generosity = `Generosity: ${d['Generosity']}`;
let happScore = `Happiness Score: ${d['Happiness Score']}`;
let life = `Life expectancy: ${d['Healthy life expectancy at birth']} years`;
let ladder = `Life Ladder: ${d['Life Ladder']}`;
let gdp = `Log GDP per capita: ${d['Log GDP per capita']}`;
let corruption = `Corruption Perception: ${d['Perceptions of corruption']} / 1`;
let support = `Social support: ${d['Social support']} / 1`;
let maxWidth = Math.max(
stringLen(countryName),
stringLen(freedom),
stringLen(generosity),
stringLen(happScore),
stringLen(life),
stringLen(ladder),
stringLen(gdp),
stringLen(corruption),
stringLen(support)
);
frame.attr('width', maxWidth + 30);
textbox.append('text').text(countryName).attr('x', 0).attr('y', 10);
textbox.append('text').text(freedom).attr('x', 0).attr('y', 30);
textbox.append('text').text(generosity).attr('x', 0).attr('y', 50);
textbox.append('text').text(happScore).attr('x', 0).attr('y', 70);
textbox.append('text').text(life).attr('x', 0).attr('y', 90);
textbox.append('text').text(ladder).attr('x', 0).attr('y', 110);
textbox.append('text').text(gdp).attr('x', 0).attr('y', 130);
textbox.append('text').text(corruption).attr('x', 0).attr('y', 150);
textbox.append('text').text(support).attr('x', 0).attr('y', 170);
yMarker.attr('y1', yScale(d['Happiness Score'])).attr('y2', yScale(d['Happiness Score']));
xMarker.attr('x1', xScale(d[columnSelected])).attr('x2', xScale(d[columnSelected]));
}
// Select data using drop down menu & hover over legend)
// Only show data points with certain YEAR & REGION
function updateDataPoints(region, year) {
yearSelected = year;
// data filtering
if (yearSelected == 0) dataByYear = data;
else
dataByYear = data.filter((d) => {
return d['year'] == yearSelected;
});
if (region == null) dataByYearRegion = dataByYear;
else
dataByYearRegion = dataByYear.filter((d) => {
return d['Region'] == region;
});
// reduce opacity for circles (dataByYear)
chartArea
.selectAll('circle')
.data(dataByYear)
.join('circle')
.attr('class', (d) => d['Region'])
.attr('cx', (d) => xScale(d[columnSelected]))
.attr('cy', (d) => yScale(d['Happiness Score']))
.attr('r', 4)
.style('fill', (d) => regionScale(d['Region']))
.attr('opacity', 0.1);
// add opacity for circle with certain year & region
circles = chartArea
.selectAll('region')
.data(dataByYearRegion)
.join('circle')
.attr('class', (d) => d['Region'])
.attr('cx', (d) => xScale(d[columnSelected]))
.attr('cy', (d) => yScale(d['Happiness Score']))
.attr('r', 4)
.style('fill', (d) => regionScale(d['Region']))
.attr('opacity', 1);
updateCircleHover();
}
// Show data points with certain year
function recoverByYear(year) {
console.log('recoverByYear()', columnSelected, yearSelected);
yearSelected = year;
let updatedData = data;
if (yearSelected != 0)
updatedData = data.filter((d) => {
return d['year'] == yearSelected;
});
circles = chartArea
.selectAll('circle')
.data(updatedData)
.join('circle')
.attr('class', (d) => d['Region'])
.attr('cx', (d) => xScale(d[columnSelected]))
.attr('cy', (d) => yScale(d['Happiness Score']))
.attr('r', 4)
.style('fill', (d) => regionScale(d['Region']))
.attr('opacity', 0.75);
updateCircleHover();
return updatedData;
}
d3.select('#sliderRange').on('change', function () {
yearSelected = d3.select(this).property('value');
console.log(yearSelected);
d3.select('#sliderYear').text(yearSelected);
let newData = recoverByYear(yearSelected);
requestMapData(newData);
});
d3.select('#selector').on('change', (e) => {
updateByColumn(e.target.value);
});
// only this function filter/modify 'data'
function updateByColumn(valueSelected) {
// set global variable 'columnSelected'
if (valueSelected == 'generos') {
columnSelected = 'Generosity';
} else if (valueSelected == 'life') {
columnSelected = 'Healthy life expectancy at birth';
} else if (valueSelected == 'ladder') {
columnSelected = 'Life Ladder';
} else if (valueSelected == 'gdp') {
columnSelected = 'Log GDP per capita';
} else if (valueSelected == 'corr') {
columnSelected = 'Perceptions of corruption';
} else if (valueSelected == 'support') {
columnSelected = 'Social support';
} else {
columnSelected = 'Freedom';
}
console.log('updateByColumn()', columnSelected, yearSelected);
// filter data by 'columnSelected'
data = dataOriginal.filter((d) => {
return d[columnSelected] != null;
});
// update xextent/scale/gridlines/axis
xExtent = d3.extent(data, (d) => d[columnSelected]);
xScale.domain(xExtent);
bottomAxis.scale(xScale);
bottomAxisG.transition().call(bottomAxis);
bottomGridlines.scale(xScale);
bottomGridlinesG.transition().call(bottomGridlines);
bottomAxisText.text(columnSelected);
// After data update, redraw datapoints
let newData = recoverByYear(yearSelected);
}
};
// call
requestData();
</script>
<script>
const requestMapData = async function (data) {
console.log('refresh map data');
document.getElementById('map').innerHTML = '';
document.getElementById('colorLegend').innerHTML = '';
const svgmap = d3.select('svg#map');
const widthm = parseInt(svgmap.attr('width'));
const heightm = parseInt(svgmap.attr('height'));
const marginm = { top: 20, right: 20, bottom: 20, left: 20 };
const mapWidth = widthm - marginm.left - marginm.right;
const mapHeight = heightm - marginm.top - marginm.bottom;
const map = svgmap.append('g').attr('transform', 'translate(' + marginm.left + ',' + marginm.top + ')');
const world = await d3.json('./datasets/world.json');
const countryCodes = await d3.json('./datasets/country-codes.json');
let countryHappiness = {};
let CountryIds = [];
data.forEach((country) => {
countryHappiness[country['Country']] = Number(country['Happiness Score']).toFixed(2);
CountryIds.push(getCountryId(country['Country'], countryCodes));
});
var allHappiness = Object.values(countryHappiness);
var countries = topojson.feature(world, world.objects.countries);
countries.features = countries.features.filter((country) => {
return country.id !== '010';
});
world.objects.countries.geometries = world.objects.countries.geometries.filter((country) => {
return CountryIds.includes(country.id);
});
var countriesMesh = topojson.mesh(world, world.objects.countries);
var projection = d3.geoMercator().fitSize([mapWidth, mapHeight], countries);
var path = d3.geoPath().projection(projection);
map.append('rect').attr('x', -margin.left).attr('y', -margin.top).attr('width', width).attr('height', height).attr('fill-opacity', 0);
map
.selectAll('path.country')
.data(countries.features)
.join('path')
.attr('class', 'country')
.attr('d', path)
.on('mouseover', mouseEntersPlot)
.on('mouseout', mouseLeavesPlot);
map.append('path').datum(countriesMesh).attr('class', 'outline').attr('d', path);
let tooltipWidth = 200;
let tooltipHeight = 80;
let momesh = map.append('path').attr('class', 'mouseover outline').attr('d', '');
let tooltip = map.append('g').attr('class', 'tooltip').attr('visibility', 'hidden');
tooltip
.append('rect')
.attr('fill', 'black')
.attr('opacity', 0.9)
.attr('x', -tooltipWidth / 2.0)
.attr('y', 0)
.attr('width', tooltipWidth)
.attr('height', tooltipHeight);
let txt = tooltip
.append('text')
.attr('class', 'countryName')
.attr('fill', 'white')
.attr('text-anchor', 'middle')
.attr('alignment-baseline', 'hanging')
.attr('x', 0)
.attr('y', 20);
let txt2 = tooltip
.append('text')
.attr('class', 'countryInfo')
.attr('fill', 'white')
.attr('text-anchor', 'middle')
.attr('alignment-baseline', 'hanging')
.attr('x', 0)
.attr('y', 45);
// color scale
const colorScale = d3.scaleSequential(d3.interpolateGnBu).domain(d3.extent(Object.values(allHappiness)));
console.log(allHappiness);
map.selectAll('.country').style('fill', (d) => {
let countryName = getCountryName(d.id, countryCodes);
if (countryHappiness[countryName]) {
return colorScale(countryHappiness[countryName]);
}
});
const sortedAllHappiness = allHappiness.sort(function (a, b) {
return a - b;
});
drawLegend(sortedAllHappiness, d3.select('#colorLegend'), colorScale);
function mouseEntersPlot() {
let country = d3.select(this);
if (CountryIds.includes(country.datum().id)) {
tooltip.style('visibility', 'visible');
country.attr('stroke', 'black').attr('stroke-width', 3).style('opacity', 0.5);
}
let countryID = country.datum().id;
let countryName = getCountryName(countryID, countryCodes);
txt.text(countryName);
txt2.text('Happiness Score: ' + countryHappiness[countryName]);
let bounds = path.bounds(country.datum());
let xPos = (bounds[0][0] + bounds[1][0]) / 2.0;
xPos = xPos > 580 ? 580 : xPos;
let yPos = bounds[1][1] + 10;
yPos = yPos > 400 ? 400 : yPos;
// console.log(xPos);
tooltip.attr('transform', `translate(${xPos},${yPos})`);
var mo = topojson.mesh(world, world.objects.countries, function (a, b) {
return a.id === countryID || b.id === countryID;
});
momesh.datum(mo).attr('d', path);
}
function mouseLeavesPlot() {
tooltip.style('visibility', 'hidden');
let country = d3.select(this);
if (CountryIds.includes(country.datum().id)) {
country.attr('stroke', 'black').attr('stroke-width', 1).style('opacity', 1);
}
momesh.attr('d', '');
}
function getCountryId(name, countryCodes) {
for (let country of countryCodes) {
if (name === country['name']) {
return country['country-code'];
}
}
}
function getCountryName(id, countryCodes) {
for (let country of countryCodes) {
if (id === country['country-code']) {
return country.name;
}
}
}
};
</script>
</body>
</html>