forked from kaitockt/simdChecker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.js
153 lines (113 loc) · 4.21 KB
/
result.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
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
window.addEventListener("load", function() {
let href = window.location.href
// === search by postcode ===
if(href.includes("postCode")) {
// === get and display data from url ===
let postcode = href.substring(href.indexOf("postCode")+9, href.length)
var stdPostcode = postcode.replaceAll("+", " ")
stdPostcode = stdPostcode.replaceAll("%2C", ",")
let result_postcode = document.getElementById('result_postcode')
result_postcode.innerHTML = stdPostcode
// === get data from database ===
this.fetch(`getsimd.php?pc=${postcode}`)
.then(response => response.json())
.then(data => {
let ranks = data["ranks"]
// counting and output
countAndOutput(ranks)
origin = data['origin']
})
}
// === search by address ===
if(href.includes("addr")) {
// === get data from url ===
let address = href.substring(href.indexOf("addr")+5, href.length)
// === get data from database ===
this.fetch(`getpostcode.php?addr=${address}`)
.then(response => response.json())
.then(data => {
let ranks = data["ranks"]
// display address and postcode data
var stdAddr = address.replaceAll("+", " ")
stdAddr = stdAddr.replaceAll("%2C", ",")
let result_postcode = document.getElementById('result_postcode')
result_postcode.innerHTML = stdAddr+" -- "+data["Post Code"]
// counting and output
countAndOutput(ranks)
origin = data['origin']
})
}
// set timeout for loading
setTimeout(loadingDiv, 300)
})
function loadingDiv() {
document.getElementById('loadingdiv').remove()
}
function countAndOutput(ranks) {
let datazone = 6976 // datazone in 2020
// color of each grade
divColor = {
1: "#a50026",
2: "#d73027",
3: "#f46d43",
4: "#fdae61",
5: "#fee090",
6: "#e0f3f8",
7: "#abd9e9",
8: "#74add1",
9: "#4575b4",
10: "#313695"
}
for(const rank in ranks) {
// count the decile of each category
let decile = parseFloat(`${ranks[rank]}`)/datazone*10
// find their grade by rounding up their decile
let grade = Math.ceil(decile)
// === html display ===
let resultDiv = this.document.getElementById(`${rank}`)
// content
resultDiv.innerHTML = `
<p>${rank}: ${ranks[rank]}</p>
<p>Grade: ${grade}</p>
`
// background color
resultDiv.style.background = `${divColor[grade]}`
// font color
if([1,2,3,8,9,10].includes(grade)) {
resultDiv.style.color = "white"
}
}
}
function displayMoreInfo() {
document.getElementById('moreInfo').style.display = "block"
document.getElementById('moreInfoBtn').style.display = "none"
// content
searchDetails()
}
function searchDetails() {
// set timeout for loading
setTimeout(loadingDiv2, 500)
let target = ["Bus Stop", "Lidl", "Sainsbury", "Aldi", "Tesco", "Iceland", "Bar", "School", "Hotel"]
target.forEach(element => {
fetch(`getnearestpoi.php?origin=${origin}&type=${element}`)
.then(response => response.json())
.then(data => {
// === html display ===
// create table's element
let moreInfoResult = document.getElementById('moreInfoResult')
let newtr = document.createElement("tr")
let eletd = document.createElement("td")
let datatd = document.createElement("td")
// content
eletd.innerHTML = `<p>${element}</P>`
datatd.innerHTML = `<p>${data}</p>`
// append to the table
newtr.appendChild(eletd)
newtr.appendChild(datatd)
moreInfoResult.appendChild(newtr)
})
});
}
function loadingDiv2() {
document.getElementById('loadingdiv2').remove()
}