-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.html
58 lines (50 loc) · 2.08 KB
/
static.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>D3.js – Finland Visualization Example</title>
<link rel="stylesheet" href="style.css">
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://d3js.org/topojson.v3.min.js"></script>
</head>
<body>
<script>
const tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.attr("id", "tooltip")
.style("opacity", 0);
const width = 1000;
const height = 600;
const projection = d3.geoMercator()
.scale(height * 2) // Fit into container
.translate([width / 2, height / 2]) // Centering
.center([26.2356, 65.1599]); // Pan to Pohjois-Pohjanmaa (Northern Ostrobothnia)
const path = d3.geoPath().projection(projection);
const svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.style("background-color", "#eee");
const url = "https://raw.githubusercontent.com/Reittu/d3js-finland-templates/master/finland.json";
d3.json(url).then(topology => {
const geojson = topojson.feature(topology, topology.objects.finland); // topojson -> geojson
svg.selectAll("path")
.data(geojson.features)
.enter().append("path")
.attr("d", path)
.on("mouseover", d => {
tooltip.html(() =>
d.properties.name + "<br>Latitude: " + d.properties.latitude + "<br> Longitude: " + d.properties.longitude
)
.style("opacity", .9);
})
.on("mousemove", d => {
tooltip
.style("left", (d3.event.pageX + 10) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", () => tooltip.style("opacity", 0));
});
</script>
</body>
</html>