-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
246 lines (202 loc) · 7.7 KB
/
main.js
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
//tree map, Complaint type by types & boroughs
//https://data.cityofnewyork.us/resource/erm2-nwe9.json
// Fetch data from API
const RESOURCE_URL = 'https://data.cityofnewyork.us/resource/erm2-nwe9.json';
const url = new URL(RESOURCE_URL);
url.searchParams.set(
"$where",
`created_date BETWEEN '2023-01-01T00:00:00' AND '2023-12-31T00:00:00' AND UPPER(complaint_type) LIKE UPPER('%Violation%of%Park%Rules%')`
);
//explained, corrected and refined with vscode copilot
// Set background color for the body
d3.select("body")
.style("background-color", "#3c3c3c");
async function fetchData() {
try {
const response = await d3.json(url);
// Group data by borough and complaint types-park rules, the borough is the first level of the hierarchy, and the complaint type is the second level
const data = d3.rollup(
response,
v => v.length,
d => d.borough,
d => d.descriptor
);
console.log("Complaints by Borough and Type:");
console.log(data);
// Convert the data to a hierarchical format
const root = d3.hierarchy([null, data], ([, value]) => value)
.sum(([, value]) => value)
.sort((a, b) => b.value - a.value);
// Set up the dimensions of the treemap
const width = window.innerWidth * 0.8;
const height = window.innerHeight * 0.8;
const margin = {top: 20, right: 0, bottom: 20, left: 0};
// Create the treemap layout
const treemapLayout = d3.treemap()
.size([width - margin.left - margin.right, height - margin.top - margin.bottom])
.padding(1);
// Update the size of the SVG element
const svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", `0 0 ${width} ${height}`)
.attr("preserveAspectRatio", "xMidYMid meet")
.style("display", "block")
.style("margin", "0 auto");
treemapLayout(root);
// Create a group for each node
const nodes = svg.selectAll("g")
.data(root.leaves())
.enter()
.append("g")
.attr("transform", d => `translate(${d.x0},${d.y0})`);
// Add fade-in and scale animation for the treemap nodes
nodes.style("opacity", 0)
.attr("transform", d => `translate(${d.x0},${d.y0}) scale(0.1)`)
.transition()
.duration(1000)
.style("opacity", 1)
.attr("transform", d => `translate(${d.x0},${d.y0}) scale(1)`);
// // Add fade-in and scale animation for the treemap nodes from bottom right
// nodes.style("opacity", 0)
// .attr("transform-origin", "bottom right")
// .attr("transform", d => `translate(${d.x0},${d.y0}) scale(0.1)`)
// .transition()
// .duration(1000)
// .style("opacity", 1)
// .attr("transform", d => `translate(${d.x0},${d.y0}) scale(1)`);
// Define colourScale
const colourScale = d3.scaleOrdinal()
.domain(["BRONX", "BROOKLYN", "MANHATTAN", "QUEENS", "STATEN ISLAND", "Unspecified"])
.range(["#fec76f", "#f5945c", "#b3be62", "#6dbfb8", "#be95be", "#72757c"]);
// Append a rectangle for each node
nodes.append("rect")
.attr("width", d => d.x1 - d.x0)
.attr("height", d => d.y1 - d.y0)
.attr("fill", d => {
const parentColor = colourScale(d.parent.data[0]);
const shade = d3.scaleLinear()
.domain([0, d.parent.children.length - 1])
.range([0.1, 3]);
return d3.color(parentColor).darker(shade(d.parent.children.indexOf(d)));
});
// Append text labels
nodes.append("text")
.attr("x", 10)
.attr("y", 25)
.style("font-family", "'Open Sans', sans-serif")
.style("font-weight", "medium")
.style("fill", "white")
.style("font-size", d => `${Math.min((d.x1 - d.x0) / 5, (d.y1 - d.y0) / 5, 16)}px`)
.text(d => d.data[0])
.each(function(d) {
const bbox = this.getBBox();
if (bbox.width > (d.x1 - d.x0) || bbox.height > (d.y1 - d.y0)) {
d3.select(this).remove();
}
});
// Add Legend for boroughs
const legend = body.append("div")
.attr("class", "legend")
.style("display", "flex")
.style("justify-content", "center")
.style("margin-top", "5px");
// Sort boroughs by total complaints in descending order
const boroughs = Array.from(data.entries())
.sort((a, b) => d3.sum(b[1].values()) - d3.sum(a[1].values()))
.map(d => d[0]);
const legendItems = legend.selectAll(".legend-item")
.data(boroughs)
.enter()
.append("div")
.attr("class", "legend-item")
.style("display", "flex")
.style("align-items", "center")
.style("margin-right", (d, i) => i === boroughs.length - 1 ? "0px" : "40px");
// Add fade-in animation for the legend itsems
legendItems.style("opacity", 0)
.transition()
.duration(1000)
.style("opacity", 1);
legendItems.append("div")
.style("width", "20px")
.style("height", "20px")
.style("background-color", colourScale)
.style("margin-right", "10px");
legendItems.append("span")
.style("font-size", "14px")
.style("font-family", "'Open Sans', sans-serif")
.style("font-weight", "regular")
.style("color", "white")
.text(d => d);
// Add hover effect to display number of complaints
nodes.on("mouseover", function(event, d) {
d3.select(this).select("rect")
.attr("stroke", "#ac513b")
.attr("stroke-width", 3);
const [x, y] = d3.pointer(event);
body.append("div") //popup window for complaints info
.attr("class", "tooltip")
.style("position", "absolute")
.style("background", "white")
.style("border", "2px solid #72757c")
.style("padding", "10px")
.style("pointer-events", "none")
.style("opacity", "0.9")
.style("left", `${event.pageX + 20}px`)
.style("top", `${event.pageY + 20}px`)
.html(`<strong>${d.data[0]}</strong><br/>Complaints: ${d.value}`);
})
.on("mouseout", function() {
d3.select(this).select("rect")
.attr("stroke", "none");
body.select(".tooltip").remove();
});
// add footer + name
const footer = d3.select("body")
.append("footer")
.style("font-size", "12px")
.style("font-family", "'Open Sans', sans-serif")
.style("font-weight", "bold")
.style("color", "white")
.style("text-align", "center")
.style("padding-top", "50px")
.text("Data Visualisation & Info Aesthetics | Exercise 2 | Xuan");
} catch (error) {
console.error("Error fetching or processing data:", error);
}
}
fetchData();
// add title to the treemap
const body = d3.select("body").style("padding", "20px");
const title = body
.append("h1")
.style("font-size", "32px")
.style("font-family", "'Open Sans', sans-serif")
.style("font-weight", "regular")
.style("color", "white")
.style("text-align", "center")
.style("padding-bottom", "20px")
.text("Types of Park Rule Violation Complaints by Borough in 2023");
// Add fade-in animation for the title
title.style("opacity", 0)
.transition()
.duration(1000)
.style("opacity", 1);
// add description
const description = body
.append("div")
.style("display", "flex")
.style("justify-content", "center")
.style("align-items", "center")
.style("padding-bottom", "30px")
.append("p")
.style("max-width", "600px")
.style("font-size", "15px")
.style("font-family", "'Open Sans', sans-serif")
.style("font-weight", "regular")
.style("color", "white")
.style("text-align", "center")
.style("line-height", "1.6") // Increase leading
.text("This treemap visualises the number of complaints related to violations of park rules in New York City in 2023. The boroughs are represented by the top-level rectangles, and the complaint types are represented by the smaller rectangles within each borough. The size of each rectangle corresponds to the number of complaints.");