-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
390 lines (328 loc) · 11.4 KB
/
index.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.chart-container {
max-width: 900px;
margin: 0px;
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
}
svg {
overflow: visible;
}
h1 {
border: 2px solid green;
}
.filler {
text-align: center;
height: 10rem;
}
.filler h2 {
font-family: 'Franklin Gothic Medium';
text-align: center;
font-size: 50px;
margin: 30px;
margin: 15px auto;
}
.filler h3 {
font-family: 'Franklin Gothic Medium';
text-align: center;
font-size: 42px;
margin: 15px auto;
color: gray;
}
.filler p {
font-family: 'Franklin Gothic Medium';
font-size: 24px;
text-align: left;
line-height: 1.2;
max-width: 600px
}
.Note {
width: 1000px;
color:grey;
font-size: 10 px;
margin: 10px auto;
}
#scrolly-overlay .scrolly {
font-family: 'Franklin Gothic Medium';
font-size: 18px;
max-width: 60rem;
margin: 3rem auto;
background-color:white;
padding: 0rem;
position: relative;
}
#scrolly-overlay .scrolly article {
padding: 5;
max-width: 30rem;
margin: 0 auto;
position: relative;
}
#scrolly-overlay .scrolly article .step {
min-height: 67vh;
margin-bottom: 1rem;
}
#scrolly-overlay .scrolly article .step:last-of-type {
margin-bottom: 0;
}
#scrolly-overlay .scrolly article .step.is-active p {
background-color: #008080;
}
#scrolly-overlay .scrolly article .step p {
margin: 0;
padding: 1rem;
text-align: center;
font-weight: 400;
background-color: white;
transition: background-color 250ms ease-in-out;
color: #f4f4f4;
}
#scrolly-overlay .scrolly figure.sticky {
position: sticky;
width: 900px;
height: 600px;
background: white;
margin: 3;
top: 30vh;
left: 0;
}
</style>
</head>
<body>
<section class='filler'>
<h2>Medicare Plan Type Percentage by Income Percentile</h2>
<h3>Start Scrolling to Find Out 2009 vs. 2020 Changes</h3>
<h3>↓</h3>
</section>
<section id='scrolly-overlay'>
<div class='scrolly'>
<!-- sticky graphic -->
<figure class='sticky'>
<svg width="900px" height="600px">
</svg>
</figure>
<!-- step text -->
<article>
<div class='step' data-index='0'>
<p>2009</p>
</div>
<div class='step' data-index='1'>
<p>2020</p>
</div>
</article>
</div>
</section>
<section>
<p class = "Note">
Note: The income measure used here is disposable income, which is defined as market income plus cash transfers from the government and noncash benefits like food stamps and public housing. It excludes taxes, out-of-pocket medical spending, insurance premiums and other expenses. There have been small changes in the census questionnaire between the two survey dates. Source: Current Population Survey, analysis by Matt Bruenig
</p>
</section>
</body>
<script src="https://d3js.org/d3.v6.min.js"></script>
<script src="https://unpkg.com/enter-view@1.0.0/enter-view.min.js"></script>
<!-- https://github.com/russellgoldenberg/enter-view -->
<script>
console.log({ d3 })
var margin = { top: 20, right: 20, bottom: 30, left:0 },
width = 900 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var svg = d3.select("svg").append("g")
.attr("transform", "translate(" + margin.left + " " + margin.top + ")");
var sel_cols = ["Medicaid", "Medicare", "Medicare+Medicaid", "Employer", "Uninsured",
"Direct Purchase", "Subsidized Exchange", "CHIP", "Military", "Other"],
sel_colors = ["#f44336", "#FF9800", "#FDD835", "#FFF8E1", "#26A69A",
"#4DD0E1", "#F06292", "#5C6BC0", "#90A4AE", "#E0E0E0"];
d3.csv("healthcare.csv").then(data => {
//parsing data
let year_state = 2020;
let d2020 = data.filter(d => d.year == 2020);
var series2020 = d3.stack().keys(sel_cols)(d2020);
let d2009 = data.filter(d => d.year == 2009);
var series2009 = d3.stack().keys(sel_cols)(d2009);
let color = d3.scaleOrdinal()
.domain(sel_cols)
.range(sel_colors)
//setting up x & y axis
let x = d3.scaleLinear()
.domain([0, 100])
.range([margin.left, width])
let y = d3.scaleLinear()
.domain([0, 1])
.range([height - margin.bottom, margin.top])
let area = d3.area()
.x(d => x(d.data.Percentile))
.y0(d => y(d[0]))
.y1(d => y(d[1]))
let xAxisSettings = d3.axisBottom(x)
.tickSize(6)
.tickPadding(6)
.ticks(10)
.tickValues([5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 95])
.tickFormat(d3.format(".0f"))
let xAxis = svg.append("g")
.attr("class", "x axis")
.call(xAxisSettings)
.call(g => g.selectAll(".domain").remove())
.attr("transform", `translate(0,${height - margin.bottom})`)
//x axis label
svg.append("text")
.attr("class", "x label")
.attr("text-anchor", "end")
.attr("x", width/2 + 20)
.attr("y", height + 20)
.text("Income Percentile");
let yAxisSettings = d3.axisLeft(y)
.tickValues([.2, .4, .6, .8])
.tickSize(6)
.tickPadding(1)
.tickFormat(d3.format(".0%"))
let yAxis = svg.append("g")
.attr("class", "y axis")
.call(yAxisSettings)
.call(g => g.selectAll(".domain").remove())
//plotting
let plot = svg.append("g");
let hed = d3.select(".headline");
plot
.selectAll("path")
.data(series2009)
.join("path")
.attr("class", d => {
console.log(d)
return d.key
})
.attr("fill", d => color(d.key))
.attr("d", area)
//main tags
svg.append("text")
.attr("class", "label")
.attr("x", 890)
.attr("y", 200)
.text("Employer")
svg.append("text")
.attr("class", "label")
.attr("x", 60)
.attr("y", 380)
.text("Medicaid")
.style("fill", "white")
svg.append("text")
.attr("class", "label")
.attr("x", 60)
.attr("y", 90)
.text("Uninsured")
.style("fill", "white")
svg.append("text")
.attr("class", "label")
.attr("x", 60)
.attr("y", 270)
.text("Medicare")
.style("fill", "black")
svg.append("text")
.attr("class", "label")
.attr("x", 890)
.attr("y", 50)
.text("Direct Purchase")
.style("fill", "black")
// add remaining legends
svg.append("text")
.attr("class", "legends")
.text("Medicare and Medicaid")
.attr("x", 50)
.attr("y", 5)
svg.append('rect')
.attr('x', 35)
.attr('y', -5)
.attr('width', 10)
.attr('height', 10)
.attr('fill', 'rgb(253, 216, 53)');
svg.append("text")
.attr("class", "legends")
.text("Subsidized Exchange")
.attr("x", 250)
.attr("y", 5)
svg.append('rect')
.attr('x', 235)
.attr('y', -5)
.attr('width', 10)
.attr('height', 10)
.attr('fill', 'rgb(240, 98, 146)');
svg.append("text")
.attr("class", "legends")
.text("CHIP")
.attr("x", 450)
.attr("y", 5)
svg.append('rect')
.attr('x', 435)
.attr('y', -5)
.attr('width', 10)
.attr('height', 10)
.attr('fill', 'rgb(92, 107, 192)');
svg.append("text")
.attr("class", "legends")
.text("Military")
.attr("x", 550)
.attr("y", 5)
svg.append('rect')
.attr('x', 535)
.attr('y', -5)
.attr('width', 10)
.attr('height', 10)
.attr('fill', 'rgb(144, 164, 174)');
svg.append("text")
.attr("class", "legends")
.text("Other")
.attr("x", 660)
.attr("y", 5)
svg.append('rect')
.attr('x', 645)
.attr('y', -5)
.attr('width', 10)
.attr('height', 10)
.attr('fill', 'rgb(224, 224, 224)');
//smooth transition between charts
function dance(filtered_data) {
plot
.selectAll("path")
.data(filtered_data)
.join("path")
.transition()
.duration(1000)
.ease(d3.easeLinear)
.attr("class", d => {
console.log(d)
return d.key
})
.attr("fill", d => color(d.key))
.attr("d", area)
}
const container = d3.select('#scrolly-overlay');
const stepSel = container.selectAll('.step'); //final all the step nodes
function updateChart(index) {
const sel = container.select(`[data-index='${index}']`);
stepSel.classed('is-active', (d, i) => i === index);
if (index == 1) {
dance(series2020)
} else {
dance(series2009)
}
}
function init() {
enterView({
selector: stepSel.nodes(),
offset: 0.3,
enter: el => {
const index = +d3.select(el).attr('data-index');
updateChart(index);
},
exit: el => {
let index = +d3.select(el).attr('data-index');
index = Math.max(0, index - 1);
updateChart(index);
}
});
}
init();
})
</script>