-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
executable file
·163 lines (138 loc) · 4.78 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>ZA Cartogram</title>
<meta charset="utf-8">
<script src="lib/d3.v2.min.js"></script>
<script src="lib/topojson.js"></script>
<script src="lib/cartogram.js"></script>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300' rel='stylesheet' type='text/css'>
<style type="text/css">
body {
font-family:'Open Sans', sans-serif;
font-weight: 300;
font-size: 14px;
line-height: 1.4em;
padding-top: 20px;
margin: 0 auto;
width: 600px;
}
#map-container {
height: 500px;
position: relative;
margin: 10px 0;
}
#map {
overflow: visible;
}
#click_to_run {
color:#888;
width: 600px;
font-size: 2em;
text-align: center;
cursor: pointer;
padding-top: 30px;
}
path.munic {
stroke: #AAA;
stroke-width: .5;
}
path.munic:hover {
stroke: #000;
}
</style>
</head>
<body>
<div id="click_to_run" onclick="do_update()">
...
</div>
<div id="map-container">
<svg id="map"></svg>
</div>
<script>
//the colour scheme for the different winning parties.
var colour_map = {
"ANC": "#FFCC00",
"DA": "#005CA7",
"IFP": "#FF1800"
};
var map = d3.select("#map");
var munics = map.append("g")
.attr("id", "munics")
.selectAll("path");
var proj = d3.geo.albers()
.origin([24.7, -29.2])
.parallels([-22.1, -34.1])
.scale(2000)
.translate([300, 300]);
var topology,
geometries,
carto_features;
var vote_data = d3.map();
var carto = d3.cartogram()
.projection(proj)
.properties(function (d) {
// this add the "properties" properties to the geometries
return d.properties;
});
// the data came from some rolling up of info I got from iec.org.za site.
// It lists the winning party, number or registered voters and votes
// cast per local municipality.
d3.csv("data/voting_data.csv", function (data) {
data.forEach(function (d) {
vote_data.set(d.MUNIC, [d.PARTY, d.VOTERS, d.VOTES]);
})
});
// this loads test the topojson file and creates the map.
d3.json("data/za-munics-topo-simp.json", function (data) {
topology = data;
geometries = topology.objects.layer1.geometries;
//these 2 below create the map and are based on the topojson implementation
var features = carto.features(topology, geometries),
path = d3.geo.path()
.projection(proj);
munics = munics.data(features)
.enter()
.append("path")
.attr("class", "munic")
.attr("id", function (d) {
return d.properties.MUNICNAME;
})
.attr("fill", function (e) {
return colour_map[vote_data.get(e.properties.CAT_B)[0]];
})
.attr("d", path);
munics.append("title")
.text(function (d) {
return d.properties.MUNICNAME;
});
d3.select("#click_to_run").text("click here to run");
});
function do_update() {
d3.select("#click_to_run").text("thinking...");
setTimeout(function () {
// this sets the value to use for scaling, per munic. Here I used the total number
// of voters per munic. the scaling is relative to the max value.
carto.value(function (d) {
return +vote_data.get(d.properties["CAT_B"])[1];
});
if (carto_features == undefined)
//this regenrates the topology features for the new map based on
carto_features = carto(topology, geometries).features;
//update the map data
munics.data(carto_features)
.select("title")
.text(function (d) {
return d.properties.MUNICNAME;
});
munics.transition()
.duration(750)
.each("end", function () {
d3.select("#click_to_run").text("done")
})
.attr("d", carto.path);
}, 10);
}
</script>
</body>
</html>