-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest_Choropleth.html
More file actions
277 lines (234 loc) · 11.9 KB
/
Test_Choropleth.html
File metadata and controls
277 lines (234 loc) · 11.9 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<!DOCTYPE html>
<meta charset="utf-8">
<title>Visualising South African Municipalities Data</title>
<style>
div.tooltip {
position: absolute;
text-align: center;
width: 320px;
height: 150px;
padding: 2px;
font: 16px sans-serif;
background: lightsteelblue;
border: 0px;
border-radius: 8px;
pointer-events: none;
}
p { text-align: center }
h1 { text-align: center }
h2 { text-align: center }
svg {
display: block;
margin-left: auto;
margin-right: auto
}
</style>
<body background="Picture.jpg">
<h1>Visualising South African Municipalities Data</h1>
<h2>
<button id="Area">Area</button>
<button id="Population">Population</button>
<button id="PopulationDensity">Population Density</button>
<button id="ElectricityAccess">Electricity Access</button>
<button id="ElectricityPrices">Commercial Electricity Prices</button>
<button id="PV_Yield">PV Yield Stellenbosch</button>
</h2>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v0.min.js"></script>
<script>
// Button Variables
var AreaButton = document.getElementById("Area");
var PopulationButton = document.getElementById("Population");
var PopulationDensityButton = document.getElementById("PopulationDensity");
var ElectrAccessButton = document.getElementById("ElectricityAccess");
var ElectrPricesButton = document.getElementById("ElectricityPrices");
var PV_YieldButton = document.getElementById("PV_Yield");
var width = 1000,
height = 750,
scale = 2500;
var projection = d3.geo.mercator()
.scale(scale)
.translate([-scale/4.3, -scale/2.6]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var color = d3.scale.quantize()
.range(["rgb(253, 208, 162)", "rgb(253, 174, 107)","rgb(253, 141, 60)", "rgb(241, 105, 19)","rgb(217, 72, 1)","rgb(140, 45, 4)"]);
//Load csv and define color domain
//https://dl.dropboxusercontent.com/u/75011568/SouthAfricaMunicipalities_Data.csv
d3.csv("SouthAfricaMunicipalities_Data.csv", function(error, SAdata) {
//https://dl.dropboxusercontent.com/u/75011568/SA_Local_Municipalities.json
d3.json("SA_Local_Municipalities.json", function(error, json) {
//Merge the data and GeoJSON
//Loop through once for each data value
for (var i = 0; i < SAdata.length; i++) {
//Grab municipality name
var MunicipalityName = SAdata[i].Name;
//Grab data value, and convert from string to float
var dataArea = parseFloat(SAdata[i].Area);
var dataPopulation = parseFloat(SAdata[i].Population);
var dataPopulationDensity = parseFloat(SAdata[i].PopulationDensity);
var dataElectricityAccess_Percentage = parseFloat(SAdata[i].ElectricityAccess_Percentage);
var dataEnergyCharge_Commercial = parseFloat(SAdata[i].EnergyCharge_Commercial);
var dataPV_Yield = parseFloat(SAdata[i].AveragePV_YieldStellenbosch);
//Find the corresponding municipality inside the GeoJSON
for (var j = 0; j < json.features.length; j++) {
var jsonMunicipality = json.features[j].properties.MUNICNAME;
//document.write([MunicipalityName, jsonMunicipality]);
//document.write(' ');
if (MunicipalityName == jsonMunicipality) {
//Copy the data value into the JSON
json.features[j].properties.value = [
dataArea,
dataPopulation,
dataPopulationDensity,
dataElectricityAccess_Percentage,
dataEnergyCharge_Commercial,
dataPV_Yield
];
//Stop looking through the JSON
break;
}
}
}
//document.write(json.features[100].properties.value[5]); document.write(' ');
//Define color domains
var ColorDomain_Area = [d3.min(json.features, function(d) { return +d.properties.value[0]; }),d3.max(json.features, function(d) { return +d.properties.value[0]; })];
var ColorDomain_Population = [d3.min(json.features, function(d) { return +d.properties.value[1]; }),d3.max(json.features, function(d) { return +d.properties.value[1]; })];
var ColorDomain_PopulationDensity = [d3.min(json.features, function(d) { return +d.properties.value[2]; }),d3.max(json.features, function(d) { return +d.properties.value[2]; })];
var ColorDomain_ElectricityAccess_Percentage = [d3.min(json.features, function(d) { return +d.properties.value[3]; }),d3.max(json.features, function(d) { return +d.properties.value[3]; })];
var ColorDomain_EnergyCharge_Commercial = [d3.min(json.features, function(d) { return +d.properties.value[4]; }),d3.max(json.features, function(d) { return +d.properties.value[4]; })];
var ColorDomain_PV_Yield = [d3.min(json.features, function(d) { return +d.properties.value[5]; }),d3.max(json.features, function(d) { return +d.properties.value[5]; })];
color.domain(ColorDomain_Area);
// Draw paths and give initial colour
var paths = svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.attr("stroke","white")
/*.style("fill","steelblue");*/
.style("fill", function(d) {
//Get data value
var value = d.properties.value[0];
if (value) {
//If value exists…
return color(value);
} else {
//If value is undefined…
return "#ccc";
}
});
// Mouseover
paths.on("mouseover", function(d) {
div.transition()
.duration(100)
.style("opacity", .9);
div.html("<big><big><b>" +d.properties.MUNICNAME+"</b></big></big>" + "<br/>" + "Area = "+ d.properties.value[0] + " km\u00B2 <br/> Population = " + d.properties.value[1] + " people <br/> Population Density = " + d.properties.value[2] + " people per km\u00B2 <br/> Electricity Access = " + d.properties.value[3] + " % <br/> Commercial Tariff = " + d.properties.value[4] + " cR/kWh <br/> PV Yield = " + d.properties.value[5] + " h")
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 16) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(100)
.style("opacity", 0);
});
// Define function of buttons
d3.select(AreaButton).on('click', function(d) {
color.domain(ColorDomain_Area);
svg.selectAll("path")
.style("fill", function(d) {
//Get data value
var value = d.properties.value[0];
if (value) {
//If value exists…
return color(value);
} else {
//If value is undefined…
return "#ccc";
}
});
});
d3.select(PopulationButton).on('click', function(d) {
color.domain(ColorDomain_Population);
svg.selectAll("path")
.style("fill", function(d) {
//Get data value
var value = d.properties.value[1];
if (value) {
//If value exists…
return color(value);
} else {
//If value is undefined…
return "#ccc";
}
});
});
d3.select(PopulationDensityButton).on('click', function(d) {
color.domain(ColorDomain_PopulationDensity);
svg.selectAll("path")
.style("fill", function(d) {
//Get data value
var value = d.properties.value[2];
if (value) {
//If value exists…
return color(value);
} else {
//If value is undefined…
return "#ccc";
}
});
});
d3.select(ElectrAccessButton).on('click', function(d) {
color.domain(ColorDomain_ElectricityAccess_Percentage);
svg.selectAll("path")
.style("fill", function(d) {
//Get data value
var value = d.properties.value[3];
if (value) {
//If value exists…
return color(value);
} else {
//If value is undefined…
return "#ccc";
}
});
});
d3.select(ElectrPricesButton).on('click', function(d) {
color.domain(ColorDomain_EnergyCharge_Commercial);
svg.selectAll("path")
.style("fill", function(d) {
//Get data value
var value = d.properties.value[4];
if (value) {
//If value exists…
return color(value);
} else {
//If value is undefined…
return "#ccc";
}
});
});
d3.select(PV_YieldButton).on('click', function(d) {
color.domain(ColorDomain_PV_Yield);
svg.selectAll("path")
.style("fill", function(d) {
//Get data value
var value = d.properties.value[5];
if (value) {
//If value exists…
return color(value);
} else {
//If value is undefined…
return "#ccc";
}
});
});
});
});
</script>