Repository navigation
Expand file tree
/
Copy pathlogic.js
More file actions
271 lines (243 loc) · 7.55 KB
/
Copy pathlogic.js
File metadata and controls
271 lines (243 loc) · 7.55 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
const mapWidth = 1968 / 2.4;
const mapHeight = 1580 / 2.4; // the 2.4 can be increased or decreased to adjust the size of the map while maintaining the aspect ratio
// These are the extent of our datapoints, coordinates-wise
const longitudeRange = [-122.52876879101329, -122.34501499128038];
const latitudeRange = [37.69947941416328, 37.81633202723721];
// Note: I think this was incorrect in the starter code provided so I had to get some help from ChatGPT to fix it.
const mapFrameGeoJSON = (() => {
const coords = [
[longitudeRange[0], latitudeRange[0]],
[longitudeRange[1], latitudeRange[1]],
];
return {
type: "Feature",
geometry: { type: "LineString", coordinates: coords },
};
})();
// This projects a given [longitude, latitude] pair into [x, y] pixel positions onto our image.
// D3 geoMercator projection fitted to San Francisco map
const projection = d3.geoMercator().fitExtent(
[
[0, 0],
[mapWidth, mapHeight],
],
mapFrameGeoJSON
);
var svg = d3
.select("#map")
.attr("width", mapWidth)
.attr("height", mapHeight)
.style("border", "1px solid black");
// Add map image to our container
svg
.append("image")
.attr("width", mapWidth)
.attr("height", mapHeight)
.attr("href", "sf_map.png")
.style("pointer-events", "none");
// function to check if a point is inside one of the circles
function checkIfInsideCircle(x1, y1, circlex, circley, r) {
if (
(x1 - circlex) * (x1 - circlex) + (y1 - circley) * (y1 - circley) <=
r * r
) {
return true;
}
return false;
}
const yearSliderLower = document.querySelector("#yearSliderLower");
const yearSliderUpper = document.querySelector("#yearSliderUpper");
const checkboxes = document.querySelector("#checkboxes");
const selectAllButton = document.querySelector("#select_all");
const deselectAllButton = document.querySelector("#deselect_all");
const circle1 = svg
.append("circle")
.attr("class", "circle1")
.attr("cx", projection([-122.45, 37.75])[0])
.attr("cy", projection([-122.45, 37.75])[1])
.attr("r", 100)
.attr("fill", "none")
.attr("stroke", "blue")
.attr("stroke-width", 6)
.raise();
const circle2 = svg
.append("circle")
.attr("class", "circle2")
.attr("cx", projection([-122.4, 37.75])[0])
.attr("cy", projection([-122.4, 37.75])[1])
.attr("r", 100)
.attr("fill", "none")
.attr("stroke", "red")
.attr("stroke-width", 6)
.raise();
d3.csv("data/SF_Film_Locations_Filtered.csv").then((data) => {
let films = svg
.selectAll(".film_circles")
.data(data)
.join("circle")
.attr("class", "film_circles")
.attr("cx", (d) => projection([d.Longitude, d.Latitude])[0])
.attr("cy", (d) => projection([d.Longitude, d.Latitude])[1])
.attr("r", 2)
.attr("fill", "black")
.call((circles) => {
circles
.append("title")
.text(
(d) =>
"Title: " +
d["Title"] +
"\nDirector: " +
d["Director"] +
"\nWriter: " +
d["Writer"] +
"\nActors: " +
d["Actor 1"] +
", " +
d["Actor 2"] +
", " +
d["Actor 3"]
);
});
// build the filters for directors with checkboxes dynamically
uniqueDirectorNames = new Set();
data.forEach((d) => {
uniqueDirectorNames.add(d["Director"]);
});
uniqueDirectorNames.forEach((d) => {
const addCheck = document.createElement("input");
addCheck.type = "checkbox";
addCheck.id = d;
addCheck.className = "directorFilter";
addCheck.checked = true;
const addLabel = document.createElement("label");
const addBreak = document.createElement("br");
addLabel.htmlFor = d;
addLabel.textContent = d;
checkboxes.appendChild(addCheck);
checkboxes.appendChild(addLabel);
checkboxes.appendChild(addBreak);
});
function updateRelevantData(data) {
films = svg
.selectAll(".film_circles")
.data(data, (d) => d["Title"])
.join("circle")
.attr("class", "film_circles")
.attr("cx", (d) => projection([d.Longitude, d.Latitude])[0])
.attr("cy", (d) => projection([d.Longitude, d.Latitude])[1])
.attr("r", 2)
.attr("fill", "black")
.call((circles) => {
circles
.append("title")
.text(
(d) =>
"Title: " +
d["Title"] +
"\nDirector: " +
d["Director"] +
"\nWriter: " +
d["Writer"] +
"\nActors:" +
d["Actor 1"] +
", " +
d["Actor 2"] +
", " +
d["Actor 3"]
);
});
getPointColors();
}
function processAllFilters() {
const selectedDirectors = [];
checkboxes.querySelectorAll(".directorFilter").forEach((checkbox) => {
if (checkbox.checked) {
selectedDirectors.push(checkbox.id);
}
});
const relevantData = data.filter(
(d) =>
selectedDirectors.includes(d["Director"]) &&
+d["Release Year"] >= +yearSliderLower.value &&
+d["Release Year"] <= +yearSliderUpper.value
);
return relevantData;
}
// apply director checkbox filter
selectAllButton.addEventListener("click", () => {
checkboxes.querySelectorAll(".directorFilter").forEach((checkbox) => {
checkbox.checked = true;
});
const relevantData = processAllFilters();
updateRelevantData(relevantData);
});
deselectAllButton.addEventListener("click", () => {
checkboxes.querySelectorAll(".directorFilter").forEach((checkbox) => {
checkbox.checked = false;
});
const relevantData = processAllFilters();
updateRelevantData(relevantData);
});
checkboxes.addEventListener("change", () => {
const relevantData = processAllFilters();
updateRelevantData(relevantData);
});
// apply year slider filter
yearSliderLower.addEventListener("input", () => {
const relevantData = processAllFilters();
updateRelevantData(relevantData);
});
yearSliderUpper.addEventListener("input", () => {
const relevantData = processAllFilters();
updateRelevantData(relevantData);
});
// allow for radius to be adjusted
const circle_one_radius = document.querySelector("#circle_one_radius");
const circle_two_radius = document.querySelector("#circle_two_radius");
circle_one_radius.addEventListener("input", () => {
const r = +circle_one_radius.value;
circle1.attr("r", r);
getPointColors();
});
circle_two_radius.addEventListener("input", () => {
const r = +circle_two_radius.value;
circle2.attr("r", r);
getPointColors();
});
// deal with dragging
function dragged(event) {
d3.select(this).raise().attr("cx", event.x).attr("cy", event.y);
getPointColors();
}
circle1.call(d3.drag().on("drag", dragged));
circle2.call(d3.drag().on("drag", dragged));
// color change
function getPointColors() {
films.each(function (d) {
const currentPoint = d3.select(this);
const isInsideOne = checkIfInsideCircle(
+currentPoint.attr("cx"),
+currentPoint.attr("cy"),
+circle1.attr("cx"),
+circle1.attr("cy"),
+circle1.attr("r")
);
const isInsideTwo = checkIfInsideCircle(
+currentPoint.attr("cx"),
+currentPoint.attr("cy"),
+circle2.attr("cx"),
+circle2.attr("cy"),
+circle2.attr("r")
);
if (isInsideOne && isInsideTwo) {
currentPoint.attr("fill", "green");
currentPoint.attr("r", 2.3);
} else {
currentPoint.attr("fill", "gray");
currentPoint.attr("r", 2);
}
});
}
getPointColors();
});