-
Notifications
You must be signed in to change notification settings - Fork 0
/
kMeansMap.html
227 lines (201 loc) · 6.1 KB
/
kMeansMap.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Social Data - Project</title>
<script type="text/javascript" src="d3/d3.min.js"></script>
<script src="//d3js.org/queue.v1.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<style>
label {
border: 1px solid black;
border-radius: 20px;
padding: 3px;
font-size: 12px;
font-weight: bold;
cursor: pointer;
margin-left: 10px;
}
</style>
</head>
<body>
<script>
// Creating radio buttons
var shapeData = ["2", "3", "4", "5", "6", "7"], j = 0; // Choose K=2 as default
// Create the shape selectors
var form = d3.select("body").append("form");
labels = form.selectAll("label")
.data(shapeData)
.enter()
.append("label")
.text(function(d) {return "K-Means: " + d;})
.insert("input")
.attr({
type: "checkbox",
class: "shape",
name: "kMeans",
onclick: function(d,i ) {return "changeK("+(i+2)+")"; }
})
.property("checked", function(d, i) {return i===j;});
//Width and height
var width = 600;
var height = 600;
// Setting color domains(intervals of values) for our map
var color = ["#0000FF", "#FFFF00", "#FF0000", "#FF00FF", "#00FF00", "#FFA500", "#FFFFFF"]
//Create SVG element
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.style("margin", "10px auto")
.style("background-color", "#66B2FF");
var projection = d3.geo.mercator()
.center([-73.985, 40.742])
.scale(220000)
.translate([width/2, height/2]);
var path = d3.geo.path().projection(projection);
//Reading map file and data
queue()
.defer(d3.json, "Data/Project/nycZipCode.geojson")
.defer(d3.csv, "Data/Project/kMeansData.csv")
.await(ready);
//Start of Choropleth drawing
function ready(error, map, data) {
var kNum = 2;
dataset = data.map(function(d) {
return [+d["lon"], +d["lat"], +d["k_means_" + kNum], +d["centerX_" + kNum], +d["centerY_" + kNum]];
});
var diffRows = [];
for (i = 0; i < kNum; i++) {
for (j = 0; j < dataset.length; j++) {
if (dataset[j][2] == i){
diffRows.push(dataset[j]);
break;
}
}
}
//Drawing Regions
svg.append("g")
.attr("class", "region")
.selectAll("path")
.data(map.features)
.enter().append("path")
.attr("d", path)
.attr("fill", '#008000')
.attr('stroke',"black")
.attr('stroke-width', "2");
//Drawing Zip Code Text
svg.append("g").attr("class", "zipCodes")
.selectAll("text")
.data(map.features)
.enter()
.append("text")
.text(function(d) { return d.properties.postalCode; })
.attr("x", function(d) { return path.centroid(d)[0]; })
.attr("y", function(d) { return path.centroid(d)[1]; })
.attr("dominant-baseline", "central")
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", "3px")
.style("font-weight", "bold")
.style("cursor", "default")
.attr("fill", "#eee9e9");
// Drawing K-Means Num Text
svg.append("g").attr("class", "kNumText")
.append("text")
.text("K-Means: 2")
.attr("id", "kNum")
.attr("x", 100)
.attr("y", 120)
.attr("dominant-baseline", "central")
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", "28px")
.style("font-weight", "bold")
.attr("fill", "#ffe4c4");
// Drawing Cluster Points
svg.append("g")
.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
.attr("cx", function(d) {
return projection([d[0], d[1]])[0];
})
.attr("cy", function(d) {
return projection([d[0], d[1]])[1];
})
.attr("r", 2.5)
.style("fill", function(d) { return color[d[2]]; })
.style("opacity", "0.75");
svg.append("g")
.attr("id", "startCircles")
.selectAll("p")
.data(diffRows)
.enter()
.append("circle")
.attr("cx", function(d) {
console.log(d[4], d[3]);
return projection([d[4], d[3]])[0];
})
.attr("cy", function(d) {
return projection([d[4], d[3]])[1];
})
.attr("r", 8)
.style("stroke", "black")
.style("stroke-width", "3")
.style("fill", function(d) { return color[d[2]]; });
};
function changeK(val) {
console.log(val);
svg.selectAll("#startCircles").remove();
svg.selectAll("#newCircles").remove();
diffRows = [];
d3.csv("Data/Project/kMeansData.csv", function(data) {
dataset = data.map(function(d) {
return [+d["Y"], +d["X"], +d["k_means_" + val], +d["centerX_" + val], +d["centerY_" + val]];
});
for (i = 0; i < val; i++) {
for (j = 0; j < dataset.length; j++) {
if (dataset[j][2] == i){
diffRows.push(dataset[j]);
break;
}
}
}
svg.select("#kNum")
.text("K-Means: " + val);
svg.selectAll("p")
.data(diffRows)
.enter()
.append("circle")
.attr("id", "newCircles")
.attr("cx", function(d) {
return projection([d[4], d[3]])[0];
})
.attr("cy", function(d) {
return projection([d[4], d[3]])[1];
})
.attr("r", 8)
.style("stroke", "black")
.style("stroke-width", "3")
.style("fill", function(d) { return color[d[2]]; });
svg.selectAll("circle")
.data(dataset)
.style("fill", function(d) { return color[d[2]]; });
});
}
</script>
<script>
$("input:checkbox").on('click', function() {
var $box = $(this);
if ($box.is(":checked")) {
var group = "input:checkbox[name='" + $box.attr("name") + "']"
$(group).prop("checked", false);
$box.prop("checked", true);
} else {
$box.prop("checked", true);
}
});
</script>
</body>
</html>