-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
79 lines (63 loc) · 2.11 KB
/
map.js
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
document.addEventListener("DOMContentLoaded", () => {
d3.json("/ch.json")
.then(ch => {
const initiatives = [
{
id: 0,
name: "Erleichterte Einbürgerung der dritten Ausländergeneration",
datum: "12.02.2017",
},
{
id: 1,
name: "Atomausstiegsinitiative",
datum: "27.11.2016",
},
]
let currentInitiative = initiatives[0]
const width = 800
const height = 600
const svg = d3.select("svg")
const projection = d3.geoMercator()
.translate([width/2, height/2])
.rotate([-7.43864, -46.95108, 0])
.center([0.54, -0.1])
.scale(13000)
const path = d3.geoPath().projection(projection)
const cantons = svg.selectAll("path")
.data(ch.features)
.enter()
.append("path")
.attr("d", path)
.attr("stroke", "#FFF")
.attr("stroke-width", 1)
const colorScale = d3.scaleLinear()
.domain([20, 50, 50.01, 80])
.range(["#FF585D", "#FFF6F6", "#F2F9F9", "#008C95"])
cantons.attr("fill", d => {
return colorScale(d.properties.initiatives[currentInitiative.id].ja)
})
const initiativeSelector = d3.select("#initiativeSelector")
.on("change", e => {
currentInitiative = initiatives[e.target.value]
cantons
.transition()
.duration(250)
.attr("fill", d => {
return colorScale(d.properties.initiatives[currentInitiative.id].ja)
})
})
cantons
.on("mouseenter", (e, d) => {
const initiativeData = d.properties.initiatives[currentInitiative.id]
const passed = initiativeData.passed
const ja = initiativeData.ja
const cantonName = d.properties.name
d3.select("#info")
.style("color", passed ? "#008C95" : "#FF585D")
.text(cantonName + " — " + (passed ? ja + "% Ja" : 100 - ja + "% Nein"))
})
.on("mouseleave", () => {
d3.select("#info").text("")
})
})
})