-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patharea.html
210 lines (162 loc) · 7.74 KB
/
area.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<svg id="First" width="1280px" height="600px"></svg>
<svg id="Second" width="1280px" height="600px"></svg>
<svg id="Third" width="1280px" height="600px"></svg>
<svg id="Forth" width="1280px" height="600px"></svg>
<script type="text/javascript" src='./libs/d3.js'></script>
<script type="text/javascript" src="http://d3js.org/topojson.v1.min.js"></script>
<script type="text/javascript">
d3.json("./geo/area/twn_town.json", function(topodata) {
console.log('topddata Looks like below:', topodata);
// topojson feature function 整理topodata成為一塊一塊的地理區塊
var features = topojson.feature(topodata, topodata.objects["twn_town"]).features;
console.log('features: ', features);
// 這裡要注意的是 topodata.objects["county"] 中的 "county" 為原本 shp 的檔名
var path = d3.geo.path().projection( // 路徑產生器
// d3.geo.mercator().center([121,24]).scale(8000) // 座標變換函式
d3.geo.mercator().center([120.979,23.832]).scale(6000)
);
d3.select("svg#First").selectAll("path").data(features)
.enter().append("path").attr("d",path);
// Declare an empty object for the json objects to be assigned
var sevenCrimes = null;
getJsonFile('./police/104台灣各縣市七宗罪統計地圖.json').then(function(result) {
// Seven crimes get. However the type of results is as string.
// Use the JSON parse to string into JSON object.
sevenCrimes = JSON.parse(result);
// Set up the linear color range.
var colorRange = d3.scale.linear()
.domain([
d3.min(sevenCrimes, dataFilter),
d3.max(sevenCrimes, dataFilter)
]).range(['#FFB88C', '#DE6262']);
function dataFilter(d) {
return d['毒品']
}
/*Paint the colors on the Taiwan map.*/
// Traverse through the features and append the data to each country.
for (var i = features.length-1; i >= 0; i --) {
features[i].properties.sevenCrimes = sevenCrimes.find(function(d) {
return d["縣市"] === features[i].properties.C_Name
});
}
// Select the whole countries
d3.select('svg').selectAll('path').data(features).attr({
d: path,
fill: function(d) {
return d.properties.sevenCrimes ?
colorRange(d.properties.sevenCrimes['毒品']) : '#000'
}
});
});
});
d3.json("./geo/area/pgh_town.json", function(topodata) {
console.log('topddata Looks like below:', topodata);
// topojson feature function 整理topodata成為一塊一塊的地理區塊
var features = topojson.feature(topodata, topodata.objects["pgh_town"]).features;
console.log('features: ', features);
// 這裡要注意的是 topodata.objects["county"] 中的 "county" 為原本 shp 的檔名
var path = d3.geo.path().projection( // 路徑產生器
// d3.geo.mercator().center([121,24]).scale(8000) // 座標變換函式
d3.geo.mercator().center([121.9739,23.97565]).scale(6000)
);
d3.select("svg#Second").selectAll("path").data(features)
.enter().append("path").attr("d",path);
// Declare an empty object for the json objects to be assigned
// var sevenCrimes = null;
// getJsonFile('./police/104台灣各縣市七宗罪統計地圖.json').then(function(result) {
// // Seven crimes get. However the type of results is as string.
// // Use the JSON parse to string into JSON object.
// sevenCrimes = JSON.parse(result);
// // Set up the linear color range.
// var colorRange = d3.scale.linear()
// .domain([
// d3.min(sevenCrimes, dataFilter),
// d3.max(sevenCrimes, dataFilter)
// ]).range(['#FFB88C', '#DE6262']);
// function dataFilter(d) {
// return d['毒品']
// }
// Paint the colors on the Taiwan map.
// // Traverse through the features and append the data to each country.
// for (var i = features.length-1; i >= 0; i --) {
// features[i].properties.sevenCrimes = sevenCrimes.find(function(d) {
// return d["縣市"] === features[i].properties.C_Name
// });
// }
// // Select the whole countries
// d3.select('svg').selectAll('path').data(features).attr({
// d: path,
// fill: function(d) {
// return d.properties.sevenCrimes ?
// colorRange(d.properties.sevenCrimes['毒品']) : '#000'
// }
// });
// });
});
d3.json("./geo/area/Lic_town.json", function(topodata) {
console.log('topddata Looks like below:', topodata);
// topojson feature function 整理topodata成為一塊一塊的地理區塊
var features = topojson.feature(topodata, topodata.objects["Lic_town"]).features;
console.log('features: ', features);
// 這裡要注意的是 topodata.objects["county"] 中的 "county" 為原本 shp 的檔名
var path = d3.geo.path().projection( // 路徑產生器
// d3.geo.mercator().center([121,24]).scale(8000) // 座標變換函式
d3.geo.mercator().center([121.9739,23.97565]).scale(10000)
);
d3.select("svg#Third").selectAll("path").data(features)
.enter().append("path").attr("d",path);
});
d3.json("./geo/area/Kim_town.json", function(topodata) {
console.log('topddata Looks like below:', topodata);
// topojson feature function 整理topodata成為一塊一塊的地理區塊
var features = topojson.feature(topodata, topodata.objects["Kim_town"]).features;
console.log('features: ', features);
// 這裡要注意的是 topodata.objects["county"] 中的 "county" 為原本 shp 的檔名
var path = d3.geo.path().projection( // 路徑產生器
// d3.geo.mercator().center([121,24]).scale(8000) // 座標變換函式
// d3.geo.mercator().center([121.9739,23.97565]).scale(6500)
// d3.geo.mercator().center([121,23.97565]).scale(6500)
d3.geo.mercator().center([118.391,24.448]).scale(6500)
);
d3.select("svg#Forth").selectAll("path").data(features)
.enter().append("path").attr("d",path);
});
/* Get the json file. */
function getJsonFile(path) {
var p = new Promise(function(resolve, reject) {
var r = new XMLHttpRequest();
// The event envokes when the load task is finished
r.addEventListener("load", loadListener.bind(r, resolve));
// The progress event
r.addEventListener('progress', progressListener);
r.open('GET', path);
r.send();
});
return p
}
function progressListener(oEvent) {
if (oEvent.lengthComputable) {
var percentComplete = oEvent.loaded / oEvent.total;
console.log(percentComplete);
// ...
} else {
// Unable to compute progress information since the total size is unknown
}
}
function loadListener(resolve) {
if (this.status === 200) {
resolve(this.responseText);
} else {
console.log('Get action is failed');
}
}
</script>
</body>
</html>