-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
130 lines (106 loc) · 3.06 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
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<style>
#wrapper {
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.country-boundary {
stroke: #999;
stroke-width: .3px;
opacity: 0.8;
}
.hidden {
display: none;
}
div.tooltip {
background-color: #fff;
padding: 7px;
text-shadow: #f5f5f5 0 1px 0;
font: 13px Helvetica Neue;
border: 2.5px solid;
border-color: black;
border-radius: 3px;
opacity: 0.95;
position: absolute;
box-shadow: rgba(0, 0, 0, 0.3) 0 2px 10px;
}
</style>
</head>
<body>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="https://unpkg.com/topojson@3"></script>
<div id="wrapper">
<div id="chart"></div>
</div>
<script>
var width = 960,
height = 600;
function zoomed({ transform }) {
d3.select('g').attr("transform", transform);
}
const zoom = d3.zoom()
.scaleExtent([1 / 4, 9])
.on("zoom", zoomed)
var svg = d3.select('#chart')
.append('svg')
.attr('width', width)
.attr('height', height)
.call(zoom);
var g = svg.append('g');
var tooltip = d3.select('#chart').append('div')
.attr('class', 'hidden tooltip');
const projection = d3.geoMercator()
.scale(140)
.translate([width / 2, height / 1.4]);
var path = d3.geoPath()
.projection(projection);
var mouseover = function (d) {
tooltip.classed('hidden', false);
d3.select(this)
.style("stroke", "black")
.style("opacity", 1)
}
var mousemove = (event, d) => {
tooltip
.attr('style', 'left:' + (event.clientX + 20) + 'px; top:' + (event.clientY - 10) + 'px')
.html(d.properties.name + '<br>' + d.properties.test);
}
var mouseout = function (d) {
tooltip.classed('hidden', true);
d3.select(this)
.style("stroke", "#999")
.style("opacity", 0.8)
}
d3.json("https://cdn.jsdelivr.net/npm/world-atlas@2/countries-110m.json").then((data) => {
var features = topojson.feature(data, data.objects.countries).features
// Set whatever relative data to your countries here, modify the function if necessary
var myList = []
features.forEach((element,index) => {
myList.push(element.properties.name)
element.properties = {
...element.properties,
test: index
}
});
console.log(myList)
// Set the colors for the data you wish to appear here
var myColor = d3.scaleLinear().domain([0,features.length])
.range(["white", "green"])
g.selectAll("path")
.data(features)
.enter()
.append("path")
.attr("d", path)
.attr("class", "country-boundary")
.attr("fill", (d)=>{return myColor(d.properties.test)}) // set your color scale variable
.on('mouseover', mouseover)
.on('mousemove', mousemove)
.on('mouseout', mouseout);
});
</script>
</body>