Skip to content

Commit 66438c9

Browse files
committed
winning party proportion
1 parent aa38cdb commit 66438c9

File tree

2 files changed

+2865
-9
lines changed

2 files changed

+2865
-9
lines changed

Taiwan_geo.json

+2,727-1
Large diffs are not rendered by default.

index.html

+138-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
</select>
2323
<!-- Create a div where the graph will take place -->
2424
<div id="map"></div>
25+
<div id="myDiv"></div>
26+
27+
2528
</body>
2629

2730
<script>
@@ -73,16 +76,143 @@
7376

7477
<!-- Choropleth Map -->
7578
<script>
76-
var data = [{
77-
type: "choroplethmapbox", locations: ['臺北市'], z: [-50],
78-
geojson: "Taiwan_geo.json"
79-
}];
79+
function getGeometry(json, cityName) {
80+
//rtype: polygon of zone
81+
for (var i = 0; i < json["features"].length; i++) {
82+
if (json["features"][i]["id"] == cityName) {
83+
return json["features"][i];
84+
}
85+
}
86+
}
87+
88+
function findWinner(cityData) {
89+
//rtype: winner party name
90+
var items = cityData.map(function (i) {//map to ["親民黨", "4.34"]
91+
return [i["candidate_party"], i['ticket_percentage']];
92+
});
93+
items.sort(function (a, b) {
94+
return +b[1] - +a[1];
95+
});
96+
97+
return items[0][0];
98+
}
99+
100+
d3.json("Taiwan_geo.json").then((geoJson)=>{
101+
//read taiwan geometry data
102+
var json = geoJson
103+
d3.csv("/dataset/voteData/cleaned/president/2020/by_city.csv").then(
104+
(csvData) => {
105+
cities = Array.from(new Set(d3.map(csvData, (d) => d.city))); //use set to get unique city name and convert to array
106+
// var data = [{
107+
// type: "choroplethmapbox", locations: [cities[0]], z: [-50],
108+
// geojson: "Taiwan_geo.json"
109+
// }];
110+
var partyInfo = {
111+
"親民黨": { "color": "rgba(227, 104, 47, 0.8)", "winZone": {"type": "FeatureCollection","features":[]} },
112+
"中國國民黨": { "color": "rgba(9, 0, 136, 0.8)", "winZone": {"type": "FeatureCollection","features":[]} },
113+
"民主進步黨": { "color": "rgba(64,145,39, 0.8)", "winZone": {"type": "FeatureCollection","features":[]} },
114+
};
115+
116+
for (var i = 0; i < csvData.length; i += 3) {
117+
var cityData = csvData.slice(i, i + 3);
118+
var cityName = cityData[0].city
119+
var winner = findWinner(cityData);
120+
var cityGeometry = getGeometry(geoJson, cityName)
121+
partyInfo[winner]["winZone"]['features'].push(cityGeometry)
122+
}
123+
var layers = []
124+
for(var party in partyInfo){
125+
var layer = {
126+
sourcetype: "geojson",
127+
source: partyInfo[party]["winZone"],
128+
type: "fill",
129+
color: partyInfo[party]['color'],
130+
}
131+
layers.push(layer)
132+
}
133+
console.log(layers[1])
80134

81-
var layout = {mapbox: {center: {lon: 121, lat: 23.5}, zoom: 7},
82-
width: 1000, height:1000};
83135

84-
var config = {mapboxAccessToken: "pk.eyJ1IjoiYTA1MTI4IiwiYSI6ImNsY2JybmJyYTIyZzEzb2w3dGl3cXpkdXYifQ.aIV8apormocbhm8GpqbySg"};
136+
var layout = {
137+
mapbox: {
138+
center: { lon: 121, lat: 23.5 },
139+
zoom: 7,
140+
style: "light",
141+
layers:layers
142+
},
143+
width: 1000,
144+
height: 1000,
145+
title: '2020'
146+
};
85147

86-
Plotly.newPlot('map', data, layout, config);
148+
var config = {
149+
mapboxAccessToken:
150+
"pk.eyJ1IjoiYTA1MTI4IiwiYSI6ImNsY2JybmJyYTIyZzEzb2w3dGl3cXpkdXYifQ.aIV8apormocbhm8GpqbySg",
151+
};
152+
153+
Plotly.newPlot("map", [
154+
{
155+
type: "scattermapbox",
156+
lat: [46],
157+
lon: [-74],
158+
},
159+
], layout, config);
160+
}
161+
);
162+
});
87163
</script>
88164

165+
<script>
166+
d3.json(
167+
"https://raw.githubusercontent.com/plotly/datasets/master/florida-red-data.json",
168+
function (redjson) {
169+
d3.json(
170+
"https://raw.githubusercontent.com/plotly/datasets/master/florida-blue-data.json",
171+
function (bluejson) {
172+
173+
console.log(bluejson)
174+
Plotly.newPlot(
175+
"myDiv",
176+
[
177+
{
178+
type: "scattermapbox",
179+
lat: [46],
180+
lon: [-74],
181+
},
182+
],
183+
{
184+
title: "Florida Counties",
185+
height: 600,
186+
width: 600,
187+
mapbox: {
188+
center: {
189+
lat: 28,
190+
lon: -84,
191+
},
192+
style: "light",
193+
zoom: 4.8,
194+
layers: [
195+
{
196+
sourcetype: "geojson",
197+
source: redjson,
198+
type: "fill",
199+
color: "rgba(163,22,19,0.8)",
200+
},
201+
{
202+
sourcetype: "geojson",
203+
source: bluejson,
204+
type: "fill",
205+
color: "rgba(40,0,113,0.8)",
206+
},
207+
],
208+
},
209+
},
210+
{
211+
mapboxAccessToken: "your access token",
212+
}
213+
);
214+
}
215+
);
216+
}
217+
);
218+
</script>

0 commit comments

Comments
 (0)