-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
168 lines (143 loc) · 6.26 KB
/
index.html
File metadata and controls
168 lines (143 loc) · 6.26 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
<!DOCTYPE html>
<!--
Data source for country coordinates: https://github.com/datasets/geo-countries/tree/main
-->
<html lang="en">
<head>
<meta charset="utf-8" />
<script src="https://d3js.org/d3.v7.min.js"></script>
<style>
/*
Source - https://stackoverflow.com/questions/27781634/rotating-globe-in-css
Posted by The Pragmatick, modified by community. See post 'Timeline' for change history
Retrieved 2025-12-21, License - CC BY-SA 4.0
*/
#earth {
width: 300px;
height: 300px;
background-size: 600px;
box-shadow: inset 2px 3px 70px 3px rgb(204, 199, 242),
inset -6px 0 12px 4px rgb(204, 199, 242);
border-radius: 50%;
animation-name: rotate;
animation-duration: 1s;
animation-iteration-count: infinite;
animation-timing-function: linear;
-webkit-animation-name: rotate;
-webkit-animation-duration: 8s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: linear;
/*
background: url(https://web.archive.org/web/20150807125159if_/http://www.noirextreme.com/digital/Earth-Color4096.jpg);
*/
}
@keyframes rotate {
from {
background-position: 0px 0px;
}
to {
background-position: 600px 0px;
}
}
@-webkit-keyframes rotate {
from {
background-position: 0px 0px;
}
to {
background-position: 600px 0px;
}
}
</style>
</head>
<body>
<div id="earth"></div>
<script>
showMap();
const margin = {top: 50, right: 25, bottom: 45, left: 50},
width = 700 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
function showMap() {
const svgWidth = 300;
const svgHeight = 200;
const svg = d3.create("svg")
.attr("width", svgWidth)
.attr("height", svgHeight)
.attr("xmlns", "http://www.w3.org/2000/svg");
const g = svg.append("g");
var projection = d3.geoNaturalEarth1()
.scale(65)
.translate([svgWidth / 2, svgHeight / 2]);
var path = d3.geoPath()
.projection(projection);
//color
var range = ["#fddbe7", "#5d6fe8","#1f56ee"];
var customInterpolator = d3.interpolateRgbBasis(range);
var colorScale = d3.scaleSequential()
.interpolator(customInterpolator);
//data loading
Promise.all([
d3.csv("data/count.csv"),
d3.json("data/countries.geojson")
])
.then(([data, json]) => {
data.forEach(d => {
d.code = d["Code"];
d.avg = +(d["Count"]);
});
colorScale.domain([
d3.min(data, d => d.avg),
d3.max(data, d => d.avg)
]);
console.log(colorScale.domain);
data.forEach(d => {
const code = d.code;
const val = d.avg;
const country = d["Country"];
const feat = json.features.find(f =>
(f.id || f.properties["ISO3166-1-Alpha-3"]) === code);
if (feat) {
feat.properties.value = val;
feat.properties.name = country;
}
});
//console.log(json.features);
json.features.forEach((feature) => {
if (feature.properties.value === undefined) {
feature.properties.value = 0;
}
})
console.log(json.features);
// draw the map
g.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.style("fill", d => {
const value = d.properties.value;
if (value !== undefined) {
return colorScale(d.properties.value);
} else {
return "#ccc";
}
})
//.style("stroke", d => d.properties.value > 0 ? "yellow": null)
//.style("stroke-width", 0.1)
//.style("stroke-opacity", 1)
;
// convert SVG to data URL for background
const svgString = svg.node().outerHTML;
const encodedSvg = encodeURIComponent(svgString);
const dataUri = `data:image/svg+xml;utf8,${encodedSvg}`;
// set as background with repeating pattern
const earthDiv = document.getElementById('earth');
earthDiv.style.backgroundImage = `url("${dataUri}")`;
earthDiv.style.backgroundSize = '200% 100%'; // Make it twice as wide
earthDiv.style.backgroundRepeat = 'repeat-x';
earthDiv.style.backgroundPosition = 'center';
console.log("Map set as background successfully");
}).catch(err => console.error(err));
}
</script>
</body>
</html>