-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
703 lines (586 loc) · 22.2 KB
/
index.html
File metadata and controls
703 lines (586 loc) · 22.2 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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.chart-container {
max-width: 1050px;
/*border: 3px solid red;*/
margin: 0 auto;
/* auto centers the div */
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
}
svg {
/*border: 2px solid purple;*/
overflow: visible;
}
.chart-headline {
/*border: 2px solid green;*/
padding-bottom : 0;
margin-bottom : 0;
}
.chart-subtitle {
margin-top : 0;
padding-top : 0;
}
.tight-headline {
margin-bottom: 0;
padding-bottom: 0;
}
.on-text {
color: rgba(0,0,0,1);
}
.off-text {
color: rgba(0,0,0,0.1);
}
.axis-label {
font-size: 18px;
}
.annotation {
font-size: 24px;
}
.legend-text {
font-size: 12px
}
</style>
</head>
<body>
<div class="chart-container">
<h1 class="chart-headline">
More Americans are relying on public health insurance programs and fewer are uninsured than in 2009
</h1>
<h3 class="chart-subtitle">
The distribution of insurance status by household income
</h3>
<h1 class="tight-headline">
<span class="on-text">2009</span>
<span class="off-text"> / 2020</span>
</h1>
<svg id="dancing-chart" width="1050px" height="500px">
</svg>
<p class="chart-notes">
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. Reproduction of work by Quoctrung Bui at the New York Times.
</p>
</div>
<hr>
<div class="chart-container">
<h1 class="chart-headline">
More Than 3 Million Americans Lost Their Jobs
</h1>
<h3 class="chart-subtitle">
Weekly jobless claims in the US
</h3>
<svg id="bar-chart" width="1050px" height="500px">
</svg>
<p class="chart-notes">
Note: Analysis by Quoctrung Bui and Justin Wolfers, New York Times. Official figures are seasonally adjusted. Source: Department of Labor.
</p>
</div>
<hr>
<div class="chart-container">
<h1 class="chart-headline">
Few students from low-income families have access to the US's elite universities
</h1>
<h3 class="chart-subtitle">
Where today’s 28-year-olds went to college, grouped by their parent's income.
</h3>
<svg id="stacked-chart" width="1050px" height="500px">
</svg>
<p class="chart-notes">
Notes: Analysis by New York Times.
</p>
</div>
</body>
<!-- add your script blocks at the end -->
<script src="https://d3js.org/d3.v6.min.js"></script>
<!-- we're using d3 version 6 (the latest version) for all out work -->
<script>
// console.log(d3)
let width = 1050;
let height = 500;
let barchart = d3.select("body").select("#bar-chart")
let stackedchart = d3.select("body").select("#stacked-chart")
let dancingchart = d3.select("body").select("#dancing-chart")
d3.csv("healthcare-disposable-inc.csv").then(function (data) {
let margin = { top: 0, right: 50, bottom: 40, left: 10 };
// console.log({data})
data.forEach(d => {
d.Percentile = +d.Percentile
d.Medicaid = +d.Medicaid
d.Medicare = +d.Medicare
d['Medicare+Medicaid'] = +d['Medicare+Medicaid']
d.CHIP = +d.CHIP
d.Military = +d.Military
d['Direct Purchase'] = +d['Direct Purchase']
d.Employer = +d.Employer
d.Other = +d.Other
d['Subsidized Exchange'] = +d['Subsidized Exchange']
d.Uninsured = +d.Uninsured
})
// console.log({data})
let sel_cols = [
"Medicaid",
"Medicare",
"Medicare+Medicaid",
"Employer",
"Uninsured",
"Direct Purchase",
"Subsidized Exchange",
"CHIP",
"Military",
"Other"
]
let sel_colors = [
"#f44336", // "Medicaid",
"#FF9800", // "Medicare",
"#FDD835", // "Medicare+Medicaid",
"#FFF8E1", // "Employer",
"#26A69A", // "Uninsured",
"#4DD0E1", // "Direct Purchase",
"#F06292", // "Subsidized Exchange",
"#5C6BC0", // "CHIP",
"#90A4AE", // "Military",
"#E0E0E0" // "Other"
]
let d2020 = [...data].filter(d => d.year == "2020")
let d2009 = [...data].filter(d => d.year == "2009")
let year = "2009"
// console.log({data09})
// console.log({data20})
//https://github.com/d3/d3-shape#stacks
let series2020 = d3.stack().keys(sel_cols)(d2020);
let series2009 = d3.stack().keys(sel_cols)(d2009);
console.log({series2009})
// https://github.com/d3/d3-scale#ordinal-scales
let color = d3.scaleOrdinal()
.domain(sel_cols)
.range(sel_colors)
let x = d3.scaleLinear()
.domain([0, 100])
.range([margin.left, width-margin.right])
let y = d3.scaleLinear()
.domain([0, 1])
.range([height - margin.bottom, margin.top])
//https://github.com/d3/d3-shape#areas
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(l => d3.format(".0f")(l) + '%')
let xAxis = dancingchart.append("g")
.attr("class", "x axis")
.call(xAxisSettings)
.call(g => g.selectAll(".domain").remove())
.attr("transform", `translate(0,${height - margin.bottom})`)
let yAxisSettings = d3.axisLeft(y)
.tickValues([.2, .4, .6, .8])
.tickSize(-10)
.tickPadding(4)
.tickFormat(d3.format(".0%"))
let yAxis = dancingchart.append("g")
.attr("class", "y axis")
.call(yAxisSettings)
.call(g => g.selectAll(".domain").remove())
let paths = dancingchart.append("g")
.selectAll("path")
.data(series2009)
.join("path")
.attr("class", d => {
return d.key
})
.attr("fill", d => color(d.key))
.attr("d", area)
dancingchart.append("text")
.attr("class", "axis-label")
.attr("x", width / 2)
.attr("y", height)
.text("Income Percentile")
.attr("text-anchor", "middle")
// ANNOTATIONS ----------
dancingchart.append("text")
.attr("class", "annotation")
.attr("x", margin.left+150)
.attr("y", margin.top+120)
.text("Uninsured")
.attr("text-anchor", "middle")
.style("fill", "white")
dancingchart.append("text")
.attr("class", "annotation")
.attr("x", width-margin.right-200)
.attr("y", height/2)
.text("Employer")
.attr("text-anchor", "middle")
dancingchart.append("text")
.attr("class", "annotation")
.attr("x", margin.left+120)
.attr("y", height-margin.bottom-50)
.text("Medicare")
.attr("text-anchor", "middle")
.style("fill", "white")
dancingchart.append("text")
.attr("class", "annotation")
.attr("x", margin.left+110)
.attr("y", height-margin.bottom-150)
.text("Medicaid")
.attr("text-anchor", "middle")
.style("fill", "white")
// LEGEND ---------
let legend_cols = [
"Medicare+Medicaid",
"Direct Purchase",
"Subsidized Exchange",
"CHIP",
"Military",
"Other"
]
// let legend_colors = [
// "#FDD835", // "Medicare+Medicaid",
// "#4DD0E1", // "Direct Purchase",
// "#F06292", // "Subsidized Exchange",
// "#5C6BC0", // "CHIP",
// "#90A4AE", // "Military",
// "#E0E0E0" // "Other"
// ]
// Add one dot in the legend for each name.
dancingchart.selectAll("mydots")
.data(legend_cols)
.enter()
.append("circle")
.attr("cx", width-margin.right + 15)
.attr("cy", function(d,i){ return margin.top + 15 + i*18}) // 100 is where the first dot appears. 25 is the distance between dots
.attr("r", 7)
.style("fill", function(d){ return color(d)})
dancingchart.selectAll("mylabels")
.data(legend_cols)
.enter()
.append("text")
.attr("x", width-margin.right + 30)
.attr("y", function(d,i){ return margin.top + 15 + i*18}) // 100 is where the first dot appears. 25 is the distance between dots
// .style("fill", function(d){ return color(d)}) // color code legend text
.text(function(d){ return d})
.attr("text-anchor", "left")
.attr("class", "legend-text")
.style("alignment-baseline", "middle")
// Dynamic Headline
let hed = d3.select(".tight-headline")
function dance(filtered_data) {
// dancingchart
// .selectAll("path")
paths // alternative to above two lines ??
.data(filtered_data, d => d.key)
.transition()
.duration(300)
.ease(d3.easeLinear)
.attr("d", area);
}
function update() {
// console.log("update")
// console.log("old year state " + year);
if (year == "2009") {
year = "2020"
dance(series2020)
hed.html("<span class='off-text'>2009 / </span><span class='on-text'>2020</span>")
dancingchart.selectAll(".annotation")
.remove()
dancingchart.append("text")
.attr("class", "annotation")
.attr("x", margin.left+150)
.attr("y", margin.top+120-40)
.text("Uninsured")
.attr("text-anchor", "middle")
.style("fill", "white")
dancingchart.append("text")
.attr("class", "annotation")
.attr("x", width-margin.right-200)
.attr("y", height/2-20)
.text("Employer")
.attr("text-anchor", "middle")
dancingchart.append("text")
.attr("class", "annotation")
.attr("x", margin.left+120)
.attr("y", height-margin.bottom-50-30)
.text("Medicare")
.attr("text-anchor", "middle")
.style("fill", "white")
dancingchart.append("text")
.attr("class", "annotation")
.attr("x", margin.left+110)
.attr("y", height-margin.bottom-150-40)
.text("Medicaid")
.attr("text-anchor", "middle")
.style("fill", "white")
} else {
year = "2009"
dance(series2009)
hed.html("<span class='on-text'>2009</span><span class='off-text'> / 2020</span>")
dancingchart.selectAll(".annotation")
.remove()
dancingchart.append("text")
.attr("class", "annotation")
.attr("x", margin.left+150)
.attr("y", margin.top+120)
.text("Uninsured")
.attr("text-anchor", "middle")
.style("fill", "white")
dancingchart.append("text")
.attr("class", "annotation")
.attr("x", width-margin.right-200)
.attr("y", height/2)
.text("Employer")
.attr("text-anchor", "middle")
dancingchart.append("text")
.attr("class", "annotation")
.attr("x", margin.left+120)
.attr("y", height-margin.bottom-50)
.text("Medicare")
.attr("text-anchor", "middle")
.style("fill", "white")
dancingchart.append("text")
.attr("class", "annotation")
.attr("x", margin.left+110)
.attr("y", height-margin.bottom-150)
.text("Medicaid")
.attr("text-anchor", "middle")
.style("fill", "white")
}
// console.log("new year: " + year);
}
var timer = d3.interval(update, 4000) //in milliseconds
})
d3.csv("nat.csv").then(function (data) {
let margin = { top: 20, right: 40, bottom: 30, left: 30 };
data.forEach(d => {
d.value = +d.value
})
let weeks = data.map(d => d.end)
// console.log({data})
// console.log({weeks})
let x = d3.scaleBand()
.domain(weeks)
.paddingInner(0)
.range([margin.left, width - margin.right - 50])
let y = d3.scaleLinear()
.domain([0, d3.max(data.map(d => d.value)) + 1e5])
.range([height - margin.bottom, margin.top])
let yAxisSettings = d3.axisRight(y) //set axis to the left
.tickValues([5e5, 1e6, 1.5e6, 2e6, 2.5e6, 3e6])
// .tickFormat(d3.format(",.0f"))
.tickFormat(function(tick) {
if (tick >= 1e6) {
if (tick % 1e6 != 0) {
return d3.format(".1f")(tick/1e6) + " million"
} else {
return d3.format(".0f")(tick/1e6) + " million"
}
} else {
return d3.format(",.0f")(tick)
}
})
.tickSize(width - margin.right - 50)
.tickPadding(10) //distance from tick labels to tick marks
let xAxisSettings = d3.axisBottom(x) // d3.scaleTime())
.tickSize(0)//d => {
// d = new Date(d)
// if (d.getUTCMonth() == 0 & d.getUTCDate() <= 7) {
// // return d3.timeFormat("%Y")(d)
// return 10
// } else {
// return 0
// }
// })
// .ticks(5)
.tickFormat(d => {
d = new Date(d)
if (d.getUTCMonth() == 0 & d.getUTCDate() <= 7) {
// console.log(d)
// return d3.timeFormat("%Y")(d)
return d.getUTCFullYear()
} else {
return ""
}
}) //what format do you want https://github.com/d3/d3-format
.tickPadding(10)
//adding to the axes to our chart
let xAxisTicks = barchart.append("g")
.attr("class", "x axis") //give each axis a class
.call(xAxisSettings)
.attr("transform", `translate(0,${height - margin.bottom})`)
let yAxisTicks = barchart.append("g")
.attr("class", "y axis")
.call(yAxisSettings)
.attr("transform", `translate(${margin.left},0)`)
.style("stroke", "#333")
.style("stroke-width", ".5")
.style("opacity", .7)
let bars = barchart.append("g")
.attr("class", "bars")
.selectAll(".bar")
.data(data)
.join("rect")
.attr("class", "bar")
.attr("x", d => x(d.end))
.attr("width", x.bandwidth())
.attr("y", d => y(d.value))
.attr("height", d => y(0) - y(d.value))
.attr("fill", "#D35400")
let maxLabelA = barchart.append("text")
.text("3,283,000 claims")
.attr('x', 815)
.attr('y', 40)
.style("font-size", "20px")
.style("fill", "#D35400")
barchart.append("text")
.text("the week after lockdown")
.attr('x', 815)
.attr('y', 55)
.style("font-size", "15px")
.style("fill", "#D35400")
})
d3.csv("college-selection-by-income-group-01-13.csv").then(function (data) {
console.log({data})
let margin = { top: 20, right: 40, bottom: 30, left: 75 };
const school_cats = [
'Ivy plus', 'Other elite', "Highly selective", 'Selective',
'Not selective', 'Two year or less', "For-profit",
'Not in college', 'Incomplete'
];
let color = d3.scaleOrdinal()
.domain(school_cats)
.range(["#d73027","#f46d43","#fdae61","#fee08b","#ffffbf","#d9ef8b","#a6d96a","#66bd63","#1a9850"].reverse())
.unknown("#ccc")
data.forEach(d => {
d["For-profit"] = +d["For-profit"]
d["Highly selective"] = +d["Highly selective"]
d['Incomplete'] = +d['Incomplete']
d['Ivy plus'] = +d['Ivy plus']
d['Not in college'] = +d['Not in college']
d['Not selective'] = +d['Not selective']
d['Other elite'] = +d['Other elite']
d['Selective'] = +d['Selective']
d['Two year or less'] = +d['Two year or less']
})
let series = d3.stack().keys(school_cats)(data)
console.log({ series })
let y = d3.scaleBand()
.domain(data.map(d => d.income_group))
.range([height - margin.bottom, margin.top])
.paddingInner(0.1)
let x = d3.scaleLinear()
.domain([0, 1])
.rangeRound([margin.left, width-margin.right]) // range()
let xTickSettings = d3.axisBottom(x)
// .tickValues([])
.ticks(11)
.tickSize(5)
.tickPadding(12)
.tickFormat(d3.format(".0%"))
let xAxis = stackedchart.append("g")
.attr("class", "x axis")
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(xTickSettings)
const ylabels = [
'Top 0.1%',
'Top 1%',
'Top 5%',
'Top 10%',
'Top 20%',
'4th quintile',
'3rd quintile',
'2nd quintile',
'Bottom 20%'
].reverse()
let yTickSettings = d3.axisLeft(y)
// .tickValues([.5, 1])
.tickSize(5)
.tickPadding(4)
.tickFormat((d,i) => {
return ylabels[i]
})
let yAxis = stackedchart.append("g")
.attr("class", "y axis")
.attr("transform", `translate(${margin.left},0)`)
.call(yTickSettings)
let group = stackedchart.append("g")
.selectAll("g")
.data(series)
.join("g")
.attr("fill", d => color(d.key))
let bars = group
.selectAll("rect")
.data(d => d)
.join("rect")
.attr("y", (d, i) => y(d.data.income_group))
.attr("height", y.bandwidth())
.attr("x", d => x(d[0]))
.attr("width", d => x(d[1]) - x(d[0]))
stackedchart.append("text")
.text("Ivy plus")
.attr('x', 105)
.attr('y', 47)
.style("font-size", "15px")
.style("fill", "white")
stackedchart.append("text")
.text("Elite")
.attr('x', 275)
.attr('y', 47)
.style("font-size", "15px")
.style("fill", "white")
stackedchart.append("text")
.text("Highly selective")
.attr('x', 465)
.attr('y', 47)
.style("font-size", "15px")
.style("fill", "#2C3E50")
stackedchart.append("text")
.text("Selective")
.attr('x', 700)
.attr('y', 47)
.style("font-size", "15px")
.style("fill", "#2C3E50")
stackedchart.append("text")
.text("Two-year or less")
.attr('x', 250)
.attr('y', height - 49)
.style("font-size", "15px")
.style("fill", "#2C3E50")
stackedchart.append("text")
.text("For-profit")
.attr('x', 384)
.attr('y', height - 49)
.style("font-size", "15px")
.style("fill", "#2C3E50")
stackedchart.append("text")
.text("Not in college by age 22")
.attr('x', 630)
.attr('y', height - 49)
.style("font-size", "15px")
.style("fill", "white")
stackedchart.append("text")
.text("Incomplete")
.attr('x', 940)
.attr('y', height - 49)
.style("font-size", "15px")
.style("fill", "white")
stackedchart.append("text")
.text("Not selective")
.attr('x', 210)
.attr('y', height - 100)
.style("font-size", "15px")
.style("fill", "#2C3E50")
stackedchart.append("text")
.text("Parent's")
.attr('x', -50)
.attr('y', 80)
.style("font-size", "18px")
stackedchart.append("text")
.text("income")
.attr('x', -50)
.attr('y', 98)
.style("font-size", "18px")
})
</script>