Skip to content

Commit

Permalink
Some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sid-kap committed May 3, 2024
1 parent cee827a commit 6ff5992
Showing 1 changed file with 16 additions and 36 deletions.
52 changes: 16 additions & 36 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,21 @@ function getYearRanges(grouping: string): [Array<[number, number]>, number] {
[2005, 2009],
[2010, 2014],
[2015, 2019],
[2020, 2023],
],
2020,
]
} else if (grouping === "five_years_old") {
return [
[
[1980, 1984],
[1985, 1989],
[1990, 1994],
[1995, 1999],
[2000, 2004],
[2005, 2009],
],
2010,
2024,
]
} else {
throw new Error(`Grouping type ${grouping} unknown`)
}
}

/**
* If grouping == "five_years", returns a mapping from each year to
* the middle year of the bucket the year gets mapped to.
*
* If grouping == "none", returns a mapping from each year to itself.
*/
function makeYearBuckets(
grouping: string
): Array<{ year: Date; binned_year: Date }> {
Expand All @@ -73,7 +68,7 @@ function makeYearBuckets(
})
}
}
for (let year = firstNonRangeYear; year <= 2022; year++) {
for (let year = firstNonRangeYear; year <= 2024; year++) {
yearBuckets.push({
year: year,
binned_year: year,
Expand All @@ -83,15 +78,11 @@ function makeYearBuckets(
return yearBuckets
}

const midYearDatesThrough2010 = [1982, 1987, 1992, 1997, 2002, 2007]

function getYearTickValues(grouping) {
if (grouping === "none") {
return null
} else if (grouping === "five_years") {
return midYearDatesThrough2010.concat([2012, 2017, 2020])
} else if (grouping === "five_years_old") {
return midYearDatesThrough2010.concat([2010, 2012, 2014, 2016, 2018, 2020])
return [1982, 1987, 1992, 1997, 2002, 2007, 2012, 2017, 2021.5]
}
}

Expand All @@ -109,7 +100,6 @@ function makeYearToRangeStringMapping(grouping: string): Map<number, string> {
}

const yearRangeAllMapping = makeYearToRangeStringMapping("five_years")
const yearRangeOldMapping = makeYearToRangeStringMapping("five_years_old")

/*eslint @typescript-eslint/no-unused-vars: ["error", { "argsIgnorePattern": "^_" }]*/
expressionFunction("yearRangeAllFormat", function (datum, _) {
Expand All @@ -131,7 +121,6 @@ function spec(
height: number,
perCapita: boolean,
preferHcdData: boolean,
interpolate: boolean,
grouping: string,
isWide: boolean
): TopLevelSpec {
Expand All @@ -155,12 +144,16 @@ function spec(
}
}
}
console.log(makeYearBuckets(grouping))
transforms.push(
...[
{
calculate: "1000 * datum['total_units_per_capita']",
as: "total_units_per_capita_per_1000",
},
// Look up "binned_year" for each year.
// i.e. if grouping == "five_years", the middle year of the 5-year range,
// if grouping == "none", the year itself.
{
lookup: "year",
from: {
Expand All @@ -176,7 +169,7 @@ function spec(
key: "year",
fields: ["binned_year"],
},
default: 2022,
default: 2023,
},
]
)
Expand All @@ -198,7 +191,6 @@ function spec(
grid: false,
formatType: {
five_years: "yearRangeAllFormat",
five_years_old: "yearRangeOldFormat",
none: "yearFormat",
}[grouping],
values: getYearTickValues(grouping),
Expand Down Expand Up @@ -233,7 +225,6 @@ function spec(
{
mark: {
type: "line",
interpolate: interpolate ? "monotone" : "linear",
clip: true,
point: true,
tooltip: true,
Expand Down Expand Up @@ -358,7 +349,7 @@ function getData(path: string): object {
function combineDatas(datas) {
const data = datas
.flatMap((d) => d.data ?? [])
.filter((d) => d.year != "2023")
.filter((d) => d.year != "2024")

return data
}
Expand Down Expand Up @@ -422,16 +413,6 @@ export default function Home(): JSX.Element {

const { preferHcdData, preferHcdDataInput } = usePreferHcdDataInput()

// const [interpolate, setInterpolate] = useState(false)
// const interpolateInput = (
// <div>
// <input type='radio' checked={!interpolate} id='no_smoothing' onChange={() => setInterpolate(false)} />
// <label htmlFor='no_smoothing' className='ml-1 mr-3'>No smoothing</label>
// <input type='radio' checked={interpolate} id='smoothing' onChange={() => setInterpolate(true)} />
// <label htmlFor='smoothing' className='ml-1 mr-3'>Smoothing</label>
// </div>
// )

type Grouping = "five_years" | "none"

const [grouping, setGrouping] = useState<Grouping>("five_years")
Expand Down Expand Up @@ -484,7 +465,6 @@ export default function Home(): JSX.Element {
height,
perCapita,
preferHcdData,
false,
grouping,
isMediumOrWider
)}
Expand Down

0 comments on commit 6ff5992

Please sign in to comment.