Skip to content

Commit c6a3134

Browse files
committed
wip: puma map new york cbsa
1 parent 72670d3 commit c6a3134

File tree

1 file changed

+57
-22
lines changed

1 file changed

+57
-22
lines changed

docs/new-york-area.md

+57-22
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,60 @@ const db = DuckDBClient.of({data: FileAttachment("data/income-histogram-historic
1010
```js
1111
const uniqueYears = await db.query("SELECT DISTINCT year FROM data WHERE year BETWEEN 2010 AND 2022 ORDER BY year").then(data => data.map(d => d.year));
1212
const yearRange = [uniqueYears[0], uniqueYears[uniqueYears.length - 1]];
13-
const yearInput = Inputs.range(yearRange, {step: 1, value: uniqueYears[0], width: 150});
13+
const yearInput = Inputs.range(yearRange, {
14+
step: 1,
15+
value: uniqueYears[0],
16+
width: 150,
17+
validate: (input) => input.value !== "2020"
18+
});
1419
const selectedYear = Generators.input(yearInput);
1520
yearInput.querySelector("input[type=number]").remove();
1621
```
1722

1823

1924
```js
20-
// Fetch the mapping of PUMA codes to their names
21-
const pumaNameMapping = await db.query(`
22-
SELECT DISTINCT puma, puma_name, state_code FROM data
23-
`).then(data => new Map(data.map(d => [d.puma, d.puma_name, d.state_code])));
25+
// Hardcoded state code to state name mapping
26+
const stateCodeToName = {
27+
'09': 'Connecticut',
28+
'34': 'New Jersey',
29+
'36': 'New York',
30+
'42': 'Pennsylvania',
31+
'44': 'Rhode Island'
32+
};
33+
34+
// Fetch PUMA details, including state names based on the hardcoded map
35+
const pumaDetails = await db.query(`
36+
SELECT DISTINCT puma, puma_name, state_code FROM data WHERE year = ${selectedYear}
37+
`).then(data => data.map(d => ({
38+
puma: d.puma,
39+
stateCode: d.state_code,
40+
label: `${stateCodeToName[d.state_code]} - ${d.puma_name.replace("PUMA", "").trim()}`
41+
})));
42+
43+
console.log("PUMA Details:", pumaDetails);
44+
45+
// Sort pumaDetails alphabetically by state name and then by PUMA code
46+
pumaDetails.sort((a, b) => {
47+
const stateCompare = stateCodeToName[a.stateCode].localeCompare(stateCodeToName[b.stateCode]);
48+
if (stateCompare !== 0) return stateCompare;
49+
return a.puma.localeCompare(b.puma);
50+
});
51+
52+
console.log("Sorted PUMA Details:", pumaDetails);
2453
```
2554

2655
```js
27-
const selectedPUMAName = pumaNameMapping.get(selectedPUMA);
56+
const PUMAInput = Inputs.select(pumaDetails, {
57+
label: "Select PUMA",
58+
value: d => d.puma,
59+
format: d => d.label
60+
});
61+
const selectedPUMADetails = Generators.input(PUMAInput);
2862
```
2963

3064
```js
31-
const uniquePUMAs = await db.query(`SELECT DISTINCT puma FROM data WHERE year = ${selectedYear}`).then(data => data.map(d => d.puma));
32-
const PUMAInput = Inputs.select(uniquePUMAs, {label: "Select PUMA", value: uniquePUMAs[0]});
33-
const selectedPUMA = Generators.input(PUMAInput);
65+
const selectedPUMA = selectedPUMADetails.puma;
66+
const selectedStateCode = selectedPUMADetails.stateCode;
3467
```
3568

3669
```js
@@ -45,9 +78,9 @@ const orderSectors = await db.query(`
4578
```
4679

4780
```js
48-
const income = db.query(`
81+
const income = await db.query(`
4982
SELECT income, count, sector FROM data
50-
WHERE year = ${selectedYear} AND puma = ${selectedPUMA}
83+
WHERE year = ${selectedYear} AND puma = '${selectedPUMA}' AND state_code = '${selectedStateCode}'
5184
`);
5285
```
5386

@@ -83,14 +116,16 @@ function incomeChart(income, width) {
83116
```
84117

85118
<div class="card">
86-
<h2>The sectors in which people earn the most money shift across time and space</h2>
87-
<h3>How much income per year x million people reported earning in the 2010–2022 American Community Surveys run by the United States' Census Bureau, categorized by their sector of employment, specifically for areas overlapping with the New York-Newark-Jersey City core-based statistical area in 2020.</h3>
88-
<h3><code style="font-size: 90%;"><a href="https://github.com/jaanli/exploring_american_community_survey_data/blob/main/american_community_survey/models/public_use_microdata_sample/figures/income-histogram-with-sector-historical-inflation-adjusted-industry-mapped.sql">Code for data transform</a></code></h3>
89-
<div style="display: flex; align-items: center;">
90-
<h1 style="margin-top: 0.5rem;">${selectedYear}</h1>
91-
${yearInput}
92-
<h1 style="margin-top: 0.5rem;">${pumaNameMapping.get(selectedPUMA)}</h1>
93-
${PUMAInput}
94-
</div>
95-
${resize((width) => incomeChart(income, width))}
96-
</div>
119+
<h2>The sectors in which people earn the most money shift across time and space</h2>
120+
<h3>How much income per year x million people reported earning in the 2010–2022 American Community Surveys run by the United States' Census Bureau, categorized by their sector of employment, specifically for areas overlapping with the New York-Newark-Jersey City core-based statistical area in 2020.</h3>
121+
<h3><code style="font-size: 90%;"><a href="https://github.com/jaanli/exploring_american_community_survey_data/blob/main/american_community_survey/models/public_use_microdata_sample/figures/income-histogram-with-sector-historical-inflation-adjusted-industry-mapped.sql">Code for data transform</a></code></h3>
122+
<div style="display: flex; align-items: center;">
123+
<h1 style="margin-top: 0.5rem;">${selectedPUMADetails.label}</h1>
124+
${PUMAInput}
125+
</div>
126+
<div style="display: flex; align-items: center;">
127+
<h1 style="margin-top: 0.5rem;">${selectedYear}</h1>
128+
${yearInput}
129+
</div>
130+
${resize((width) => incomeChart(income, width))}
131+
</div>

0 commit comments

Comments
 (0)