-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
224 lines (197 loc) · 7.04 KB
/
index.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
var path = '';
var mapMode = 'county_mode';
var displayMode = 'total_mode';
const COUNTYNAME = [
"臺北市","嘉義市","新竹市","基隆市","新北市","桃園市","臺中市","高雄市","臺南市","金門縣","澎湖縣","雲林縣","連江縣","新竹縣","苗栗縣","屏東縣","嘉義縣","宜蘭縣","南投縣","花蓮縣","臺東縣"
];
const colorFn = (min, max) => d3.scale.linear().domain([min, max]).range(["#090", "#f00"]);
const county_total_colorFn = colorFn(0,5000);
const district_total_colorFn = colorFn(0,500);
const county_ratio_colorFn = colorFn(0,11094); // d3.scale.linear().domain([0, 11094]).range(["#090", "#f00"]);
const district_ratio_colorFn = colorFn(0,11094); // d3.scale.linear().domain([0, 11094]).range(["#090", "#f00"]);
const originalViewBox = "100 100 600 200";
var county_features = topojson.feature(countyData, countyData.objects.county).features;
var district_features = topojson.feature(districtData, districtData.objects.twmap).features;
$(document).ready(function() {
render();
hideDiv('firecaseTable');
});
// TODO : optimize color function
// const color_func = (obj) => {
// const arr = Object.values(obj);
// const min = Math.min(...arr);
// const max = Math.max(...arr);
// return d3.scale.linear().domain([min, max]).range(["#090", "#f00"]);
// }
function renderStreetDataTable(d_name) {
const streetData = taipei_fire_counts[d_name];
const tableElemnets = streetData.slice(0,3).map((data, index) => {
return (
'<tr>\
<th scope="row">' + (index+1) + '</th>\
<td>' + data.street + '</td>\
<td>' + data.count + '</td>\
</tr>'
)
});
document.getElementById("tableBody").innerHTML = tableElemnets.join('');
}
function renderModelFullDataTable(d_name) {
const streetData = taipei_fire_counts[d_name];
const tableElemnets = streetData.map((data, index) => {
return (
'<tr>\
<th scope="row">' + (index+1) + '</th>\
<td>' + data.street + '</td>\
<td>' + data.count + '</td>\
</tr>'
)
});
document.getElementById("fullDataTable").innerHTML = tableElemnets.join('');
showDiv('popModalBtn');
}
function resetScale(min,max){
document.getElementById("leftnum").innerHTML = min;
document.getElementById("rightnum").innerHTML = max;
}
function clearselected() {
if (document.querySelector('.selected')) {
document.querySelector('.selected').classList.remove('selected');
}
}
function addSelectClass(c_id, px) {
clearselected();
document.getElementById(c_id).classList.add('selected');
document.getElementById(c_id).style["stroke-width"] = px;
}
function showDiv(elementId) {
document.getElementById(elementId).style["display"] = "";
}
function hideDiv(elementId) {
document.getElementById(elementId).style["display"] = "none";
}
function update(properties, total) {
var name = properties.C_Name;
var strokepx = 2;
var id = properties.County_ID;
if( mapMode !== 'county_mode'){
name = properties.TOWNNAME;
id = properties.TOWNCODE;
renderStreetDataTable(name);
renderModelFullDataTable(name);
strokepx = 0.4;
}
$("#name").text(name);
addSelectClass(id, strokepx);
$("#total").text(total);
}
function zoomMap(viewboxsize, scalesize){
var mapsvg = document.getElementsByTagName("svg")[0];
mapsvg.setAttribute("viewBox", viewboxsize);
mapsvg.setAttribute("transform", 'scale(' + scalesize + ')');
}
function goToDistrictMap() {
const c_name = $("#name").text();
if (!COUNTYNAME.includes(c_name))
return;
mapMode = 'district_mode';
zoomMap(viewBoxforDistrict[c_name], scaleforDistrict[c_name]);
clearselected();
render();
hideDiv('findDetailBtn');
showDiv('backToFullMapBtn');
showDiv('firecaseTable');
resetScale(0,500);
}
function backToFullMap() {
mapMode = 'county_mode';
zoomMap(originalViewBox);
clearselected();
render();
hideDiv('backToFullMapBtn');
hideDiv('firecaseTable');
showDiv('findDetailBtn');
resetScale(0,5000);
}
function useRatioMode() {
displayMode = 'ratio_mode';
render();
hideDiv('useRatioBtn');
showDiv('useTotalBtn');
document.getElementById("unit").innerHTML = '( 火災次數 / 戶數 )';
resetScale(0,0.011094);
}
function useTotalMode(){
displayMode = 'total_mode';
render();
hideDiv('useTotalBtn');
showDiv('useRatioBtn');
document.getElementById("unit").innerHTML = '( 火災次數 / 年 )';
}
function computeFeature(mMode, dmode) {
var features = [];
if (mMode === 'county_mode'){
features = county_features;
}
else{
var c_name = $("#name").text();
if (c_name === "?") {
return;
}
var features = district_features.filter(feature => {
if (feature.properties.COUNTYNAME === c_name)
return feature;
});
}
// and total attr in features
for (idx = features.length - 1; idx >= 0; idx--){
if (features[idx].properties.C_Name) {
if (displayMode === 'total_mode')
features[idx].total = county_total[features[idx].properties.C_Name];
else
features[idx].total = county_ratio[features[idx].properties.C_Name];
}
else {
if (displayMode === 'total_mode')
features[idx].total = district_total[features[idx].properties.COUNTYNAME][features[idx].properties.TOWNNAME] || 100;
else
features[idx].total = district_ratio[features[idx].properties.COUNTYNAME][features[idx].properties.TOWNNAME] || 100;
}
}
return features;
}
function render() {
features = computeFeature(mapMode, displayMode);
d3.select("svg").selectAll("*").remove();
var prj = function(v) {
var ret = d3.geo.mercator().center([122, 23.25]).scale(6000)(v);
var position = { x: ret[0], y: ret[1] };
return [position.x, position.y];
};
path = d3.geo.path().projection(prj);
d3.select("svg").selectAll("path").data(features).enter().append("path");
d3.select("svg").selectAll("path").data(features).attr({
id: (d) => d.properties.TOWNCODE || d.properties.County_ID
});
d3.select("svg").selectAll("path").attr({
"d": path,
"fill": function(d) {
if (d.properties.County_ID) {
if (displayMode === 'ratio_mode')
return county_ratio_colorFn(d.total*1000000);
else
return county_total_colorFn(d.total);
}
else {
if (displayMode === 'ratio_mode')
return district_ratio_colorFn(d.total*1000000);
else
return district_total_colorFn(d.total);
}
}
});
d3.select("svg").selectAll("path").on("click", (d) => {
console.log(d.properties, d.total);
update(d.properties,d.total);
});
}