-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject_Final.html
More file actions
239 lines (198 loc) · 6.51 KB
/
Copy pathProject_Final.html
File metadata and controls
239 lines (198 loc) · 6.51 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Final Narrative Project</title>
<meta name="description" content="description" />
<meta name="author" content="author" />
<meta name="keywords" content="keywords" />
<link rel="stylesheet" href="./stylesheet.css" type="text/css" />
<script src="https://d3js.org/d3.v7.min.js"></script>
<style>
body {
font-family: 'Trebuchet MS', sans-serif;
margin: 0;
padding: 0;
}
.b {
width: 3%;
background: black;
color: whitesmoke;
border-radius: 100%;
font-weight: bold;
padding: 1em;
border: none;
}
.b:hover {
background: rgb(105, 105, 105);
color: rgb(202, 202, 202);
cursor: pointer;
transition: background 0.5s;
}
.mySlides {
display: none;
}
.container {
display: flex;
justify-content: center;
align-items: center;
height: 65vh;
}
.content {
word-wrap: break-word;
width: 60vw;
margin: 0 auto;
padding-top: 5px;
}
hr.fancy {
width: 70%;
opacity: 0.3;
height: 1px;
background-image: linear-gradient(to right, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.75), rgba(0, 0, 0, 0));
}
h1 {
margin-left: 15px;
margin-top: 25px;
font-weight: bold;
}
p.author {
margin-left: 20%;
}
.mySlides{
margin-left: 2%;
margin-right: 2%;
width:800px;
height:600px;
padding-top: 2%;
padding-bottom: 2%;
border:1px solid #ccc;
}
#my_dataviz {
height: 400px;
text-align: center;
}
</style>
<script>
let slideIndex = 1;
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
let i;
let slides = document.getElementsByClassName("mySlides");
if (n > slides.length) { slideIndex = 1 }
if (n < 1) { slideIndex = slides.length }
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slideIndex - 1].style.display = "block";
}
window.onload = function () {
showSlides(slideIndex);
}
</script>
</head>
<body>
<h1>Narrative Visualization: In Progress</h1>
<hr class="fancy" />
<p class="author">Emily Aguilar, 2025</p>
<hr class="fancy" />
<div class="content">
<p>Hello ipsum dolor sit amet, consectetur adipiscing elit. Etiam elementum et dolor vel mollis. Vestibulum congue
ultricies aliquet. Mauris sit amet viverra nisl, at eleifend diam. Duis hendrerit ac massa consectetur feugiat.
Curabitur sit amet tellus at turpis semper posuere ac ac nunc. Aenean quis odio ornare, vulputate sem eget,
faucibus purus. Nulla sit amet sagittis est. Aenean gravida fermentum vulputate. Pellentesque vitae enim non est
semper ultricies eget id urna. Curabitur et sagittis nulla. Praesent nulla ex, volutpat non blandit volutpat,
ornare quis risus.</p>
</div>
<div id="chart_income"></div>
<!-- Keep only this D3 -->
<script src="https://d3js.org/d3.v7.min.js"></script>
<script>
// ====== CONFIG ======
const csvPath = "HouseholdIncome.csv"; // or "cases/household_income.csv" if you rename the file
const bracketColumn = "Household Income Bucket";
const yearColumn = "Year";
const shareColumn = "share";
const selectedBracket = "< $10,000"; // change to any value that exists in Household In
// ====== DRAW CHART ======
const container = d3.select("#chart_income"); // <-- make sure your HTML has <div id="chart_income" ...>
container.selectAll("*").remove();
const margin = { top: 20, right: 30, bottom: 40, left: 60 };
const width = 760 - margin.left - margin.right;
const height = 420 - margin.top - margin.bottom;
const svg = container.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})`);
d3.csv(csvPath, d3.autoType).then(raw => {
// Filter to the chosen income bracket and prep data
const data = raw
.filter(d => d[bracketColumn] === selectedBracket)
.map(d => ({ year: +d[yearColumn], share: +d[shareColumn] }))
.filter(d => Number.isFinite(d.year) && Number.isFinite(d.share))
.sort((a, b) => a.year - b.year);
if (!data.length) {
svg.append("text")
.attr("x", 0).attr("y", 20).attr("fill", "#b00020")
.text(`No rows for bracket "${selectedBracket}". Check column names/values.`);
console.log("Columns seen:", raw.length ? Object.keys(raw[0]) : []);
return;
}
// Scales
const x = d3.scaleLinear()
.domain(d3.extent(data, d => d.year)).nice()
.range([0, width]);
const y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.share)]).nice()
.range([height, 0]);
// Axes
svg.append("g")
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(x).ticks(data.length).tickFormat(d3.format("d"))); // show years as integers
svg.append("g")
.call(d3.axisLeft(y).tickFormat(d3.format(".0%"))); // share as percent
// Line
const line = d3.line()
.x(d => x(d.year))
.y(d => y(d.share));
svg.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 2)
.attr("d", line);
// Points
svg.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("cx", d => x(d.year))
.attr("cy", d => y(d.share))
.attr("r", 3);
// Title
svg.append("text")
.attr("x", 0).attr("y", -5)
.attr("font-weight", "600")
.text(`Share of households (${selectedBracket}) by year`);
}).catch(err => {
console.error("Error:", err);
container.append("div")
.style("color", "#b00020")
.style("font-family", "monospace")
.text("Failed to load CSV: " + err.message);
});
</script>
<div class="container">
<button class="b" onclick="plusSlides(-1)">❮</button>
<div id="my_dataviz" class="mySlides" src="about:blank" style="background:#FAF8F0;"></div>
<div class="mySlides" src="about:blank" style="background:green"></div>
<div class="mySlides" src="about:blank" style="background:red"></div>
<button class="b" onclick="plusSlides(1)">❯</button>
</div>
</body>
</html>