-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
397 lines (328 loc) · 12.6 KB
/
app.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Load the CSV data
d3.csv("data/us.csv").then((data) => {
// Set up the dimensions of the chart
const margin = { top: 20, right: 100, bottom: 30, left: 100 };
const width = 1000 - margin.left - margin.right;
const height = 400 - margin.top - margin.bottom;
// Convert string dates to actual date objects
const dateParser = d3.timeParse("%Y-%m-%d");
data.forEach((d) => {
d.date = dateParser(d.date);
d.cases = +d.cases; // Convert to numbers
d.deaths = +d.deaths;
});
// Create an SVG element within the #chart div
const svgCases = d3.select("#line-chart-cases")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Create an SVG element within the #chart div
const svgDeaths = d3.select("#line-chart-deaths")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Create scales for x and y axes
const xScale = d3.scaleTime()
.domain(d3.extent(data, (d) => d.date))
.range([0, width]);
const yScaleCases = d3.scaleLinear()
.domain([0, d3.max(data, (d) => d.cases)])
.range([height, 0]);
const yScaleDeaths = d3.scaleLinear()
.domain([0, d3.max(data, (d) => d.deaths)])
.range([height, 0]);
// Create x and y axes
const xAxis = d3.axisBottom(xScale);
const yAxisCases = d3.axisLeft(yScaleCases);
const yAxisDeaths = d3.axisRight(yScaleDeaths);
// Append x and y axes to the SVG
svgCases.append("g")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svgCases.append("g")
.call(yAxisCases);
svgDeaths.append("g")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svgDeaths.append("g")
.attr("transform", "translate(0, " + width + ", 0)")
.call(yAxisDeaths);
// Create the line generators
const lineCases = d3.line()
.x((d) => xScale(d.date))
.y((d) => yScaleCases(d.cases));
const lineDeaths = d3.line()
.x((d) => xScale(d.date))
.y((d) => yScaleDeaths(d.deaths));
// Append the lines to the SVG
svgCases.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "blue")
.attr("stroke-width", 1)
.attr("d", lineCases);
svgDeaths.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "red")
.attr("stroke-width", 1)
.attr("d", lineDeaths);
// Add tooltips
const tooltipCases = d3.select("#line-chart-cases")
.append("div")
.attr("class", "tooltip-line-chart")
.style("opacity", 0);
const tooltipDeaths = d3.select("#line-chart-deaths")
.append("div")
.attr("class", "tooltip-line-chart")
.style("opacity", 0);
svgCases.selectAll("dot")
.data(data)
.enter()
.append("circle")
.attr("r", 3)
.attr("cx", (d) => xScale(d.date))
.attr("cy", (d) => yScaleCases(d.cases))
.style("fill", "blue")
.on("mouseover", (event, d) => {
tooltipCases.transition()
.duration(200)
.style("opacity", 1);
tooltipCases.html("Date: " + d.date.toISOString().slice(0, 10) + "<br> Cases: " + d.cases)
.style("left", (event.pageX) + "px")
.style("top", (event.pageY) - 30 + "px");
})
.on("mouseout", () => {
tooltipCases.transition()
.duration(500)
.style("opacity", 0);
});
svgDeaths.selectAll("dot")
.data(data)
.enter()
.append("circle")
.attr("r", 3)
.attr("cx", (d) => xScale(d.date))
.attr("cy", (d) => yScaleDeaths(d.deaths))
.style("fill", "red")
.on("mouseover", (event, d) => {
tooltipDeaths.transition()
.duration(200)
.style("opacity", 1);
tooltipDeaths.html("Date: " + d.date.toISOString().slice(0, 10) + "<br> Deaths: " + d.deaths)
.style("left", (event.pageX) + "px")
.style("top", (event.pageY) -30 + "px");
})
.on("mouseout", () => {
tooltipDeaths.transition()
.duration(500)
.style("opacity", 0);
});
});
/////////////////////////////////////BAR CHARTS/////////////////////////////////////////////////
// Load the CSV data
d3.csv("data/us-states.csv").then((data) => {
// Set up the dimensions of the chart
const margin = { top: 20, right: 100, bottom: 80, left: 100 };
const width = 1000 - margin.left - margin.right;
const height = 400 - margin.top - margin.bottom;
// Convert string dates to actual date objects
const dateParser = d3.timeParse("%Y-%m-%d");
data.forEach((d) => {
d.date = dateParser(d.date);
d.cases = +d.cases; // Convert to numbers
d.deaths = +d.deaths;
});
// Get unique dates for date selection
const uniqueDates = [...new Set(data.map((d) => d.date))];
// Create date selection bar
const dateSelect = d3.select("#date-select");
dateSelect.selectAll("option")
.data(uniqueDates)
.enter()
.append("option")
.text((d) => d.toISOString().slice(0, 10))
.attr("value", (d) => d.toISOString().slice(0, 10)); // Set the value attribute for each option
const maxCasesValue = d3.max(data, (d) => d.cases);
const maxDeathsValue = d3.max(data, (d) => d.deaths);
// Function to update the cases bar chart based on the selected date
function updateCasesChart(selectedDate) {
const filteredData = data.filter((d) => d.date.getTime() === selectedDate.getTime());
// Create scales for x and y axes
const xScale = d3.scaleBand()
.domain(filteredData.map((d) => d.state))
.range([0, width])
.padding(0.1);
const yScaleCases = d3.scaleLinear()
.domain([0, maxCasesValue])
.range([height, 0]);
// Create x and y axes
const xAxisCases = d3.axisBottom(xScale);
const yAxisCases = d3.axisLeft(yScaleCases);
// Create SVG element for cases bar chart
const svgCases = d3.select("#bar-chart-cases")
.selectAll("svg")
.data([null])
.join("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Remove any existing bars
svgCases.selectAll(".bar").remove();
// Create bars for cases
const barsCases = svgCases.selectAll(".bar")
.data(filteredData)
.enter()
.append("rect")
.attr("class", "bar")
.attr("x", (d) => xScale(d.state))
.attr("y", (d) => yScaleCases(d.cases))
.attr("width", xScale.bandwidth())
.attr("height", (d) => height - yScaleCases(d.cases))
.attr("fill", "blue"); // Fill cases bars with blue color
// Add tooltips for cases bars
barsCases.on("mouseover", (event, d) => {
const tooltip = d3.select("#tooltip");
tooltip.transition().duration(200).style("opacity", 1);
tooltip.html(`<strong>${d.state}</strong><br>Cases: ${d.cases}`)
.style("left", event.pageX + "px")
.style("top", event.pageY - 28 + "px");
});
barsCases.on("mouseout", () => {
const tooltip = d3.select("#tooltip");
tooltip.transition().duration(500).style("opacity", 0);
});
// Append x and y axes to the SVG for cases
svgCases.append("g")
.attr("transform", "translate(0," + height + ")")
.call(xAxisCases)
.selectAll("text")
.attr("transform", "rotate(-45)")
.style("text-anchor", "end");
svgCases.append("g")
.call(yAxisCases);
}
// Function to update the deaths bar chart based on the selected date
function updateDeathsChart(selectedDate) {
const filteredData = data.filter((d) => d.date.getTime() === selectedDate.getTime());
// Create scales for x and y axes
const xScale = d3.scaleBand()
.domain(filteredData.map((d) => d.state))
.range([0, width])
.padding(0.1);
const yScaleDeaths = d3.scaleLinear()
.domain([0, maxDeathsValue])
.range([height, 0]);
// Create x and y axes
const xAxisDeaths = d3.axisBottom(xScale);
const yAxisDeaths = d3.axisLeft(yScaleDeaths);
// Create SVG element for deaths bar chart
const svgDeaths = d3.select("#bar-chart-deaths")
.selectAll("svg")
.data([null])
.join("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Remove any existing bars
svgDeaths.selectAll(".bar").remove();
// Create bars for deaths
const barsDeaths = svgDeaths.selectAll(".bar")
.data(filteredData)
.enter()
.append("rect")
.attr("class", "bar")
.attr("x", (d) => xScale(d.state))
.attr("y", (d) => yScaleDeaths(d.deaths))
.attr("width", xScale.bandwidth())
.attr("height", (d) => height - yScaleDeaths(d.deaths))
.attr("fill", "red"); // Fill deaths bars with red color
// Add tooltips for deaths bars
barsDeaths.on("mouseover", (event, d) => {
const tooltip = d3.select("#tooltip");
tooltip.transition().duration(200).style("opacity", 0.9);
tooltip.html(`<strong>${d.state}</strong><br>Deaths: ${d.deaths}`)
.style("left", event.pageX + "px")
.style("top", event.pageY - 28 + "px");
});
barsDeaths.on("mouseout", () => {
const tooltip = d3.select("#tooltip");
tooltip.transition().duration(500).style("opacity", 0);
});
// Append x and y axes to the SVG for deaths
svgDeaths.append("g")
.attr("transform", "translate(0," + height + ")")
.call(xAxisDeaths)
.selectAll("text")
.attr("transform", "rotate(-45)")
.style("text-anchor", "end");
svgDeaths.append("g")
.call(yAxisDeaths);
}
// Set the default date
const defaultDate = dateParser("2022-01-01");
dateSelect.property("value", defaultDate.toISOString().slice(0, 10));
// Initial update of the charts with the default date
updateCasesChart(defaultDate);
updateDeathsChart(defaultDate);
// Add event listener to update the charts on date selection change
dateSelect.on("change", function () {
const selectedDate = dateParser(this.value);
updateCasesChart(selectedDate);
updateDeathsChart(selectedDate);
});
let updateInterval;
// // Function to automate the date selection and chart updates
// function automateCharts() {
// const startDate = new Date(document.getElementById("date-select").value);
// const endDate = d3.max(data, (d) => d.date); // Get the latest date from the data
// let currentDate = startDate;
// const interval = 10; // Time interval between date changes in milliseconds
// // Update the date selection and charts at the specified interval
// updateInterval = setInterval(() => {
// if (currentDate <= endDate) {
// // Update the date selection
// document.getElementById("date-select").valueAsDate = currentDate;
// // Update the charts
// updateCasesChart(currentDate);
// updateDeathsChart(currentDate);
// // Increment the current date
// currentDate.setDate(currentDate.getDate() + 1);
// } else {
// // Stop the automated updates when the end date is reached
// clearInterval(updateInterval);
// // Enable the "Automate" button
// document.getElementById("automate-button").disabled = false;
// // Disable the "Stop" button
// document.getElementById("stop-button").disabled = true;
// }
// }, interval);
// // Disable the "Automate" button
// document.getElementById("automate-button").disabled = true;
// // Enable the "Stop" button
// document.getElementById("stop-button").disabled = false;
// }
// // Function to stop the automated updates
// function stopAutomateCharts() {
// // Clear the interval to stop automated updates
// clearInterval(updateInterval);
// // Enable the "Automate" button
// document.getElementById("automate-button").disabled = false;
// // Disable the "Stop" button
// document.getElementById("stop-button").disabled = true;
// // Update the charts once the automation is stopped
// const selectedDate = new Date(document.getElementById("date-select").value);
// updateCasesChart(selectedDate);
// updateDeathsChart(selectedDate);
// }
// // Event listener for the "Automate" button
// document.getElementById("automate-button").addEventListener("click", automateCharts);
// // Event listener for the "Stop" button
// document.getElementById("stop-button").addEventListener("click", stopAutomateCharts);
});