Published
Edited
Apr 15, 2021
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
filterBySelection = (data, selection) => {
return ages.reduce((acc, ageRange)=>{
const filtered = data[ageRange].filter((d)=> d.barLabel === selection.barLabel && d.sex === selection.sex && d.age === selection.ageKey)
if(filtered.length > 0) {
acc[ageRange] = filtered
}
return acc
}, {})


}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
monthlyDiffData = {
return datasets.map((dataset)=>{
const diffs = ages.reduce((acc, agerange)=> {
const data = dataset.data.filter((d)=>d.age === agerange)
acc[agerange] = calculateMonthDiffsTest(data)
return acc
}, {})

const name = dataset.name
return {...diffs, name, yLabel: dataset.yLabel}
})
}
Insert cell
test_calc = datasets[0].data.filter((d)=> d.age=== "16-19")
Insert cell
monthArray
Insert cell
calculateMonthDiffsTest(test_calc)
Insert cell
calculateMonthDiffsTest = (dataset) => {

return dataset.reduce((timeSeriesContainer, series)=>{
const data = series.data
const barLabel = series.ethnicity === "hispanic_latino" ? "hispanic_latino" : series.race
const yearlyValues = data.reduce((acc, dataObj, index)=>{
const year = dataObj.date.getFullYear()
const month = monthArray[dataObj.date.getMonth()]
const value = {month, value: dataObj.value, year, date: dataObj.date }
if(acc[month]) {
acc[month] = [...acc[month], value]
} else {
acc[month] = [value]
}
return acc
},{})
const timeseries = Object.values(yearlyValues).map((valArray)=>{
const sortedByYear = valArray.sort((a,b)=> a.year-b.year)
const month = sortedByYear[0].month
const monthDiffArray = sortedByYear.reduce((acc, d, index)=>{
if(index === 0) {
acc[0] = {value: d.value, diff: 0, year: d.year, diffYear: d.year,diffValue: d.value, date: d.date,prevDate:d.date, barLabel, age: series.age, sex: series.sex, uuid: uuid(), month: d.month, label: `${d.year}`}
} else {
const last = acc[index-1]
const diff = d.value - last.value
const newValues = {value: d.value, diff, year: d.year, diffYear: last.year, diffValue: last.value, date: d.date, prevDate:last.date, barLabel, month: d.month, age: series.age, sex: series.sex, uuid: uuid(), label: `${last.year}-${d.year}`}
acc[index] = newValues
}
return acc
},[])
return monthDiffArray
})

let container = {}
for (let i = 0; i < timeseries.length -1; i++) {
const current = timeseries[i]
current.forEach((d)=>{
if(d.year !== 2018) {
if(container[`${d.year}`]) {
container[`${d.year}`].push(d)
} else {
container[`${d.year}`] = [d]
}
}
})
}
const finalValues = Object.keys(container).map((key)=>{
const timeseries = container[key]
return {
barLabel,
year_range: key,
ethnicity: series.ethnicity,
race: series.race,
age: series.age,
sex: series.sex,
timeseries
}
})
timeSeriesContainer = [...timeSeriesContainer, ...finalValues]
return timeSeriesContainer

}, [])


}
Insert cell
Insert cell
Insert cell
calculateDataExtent = (data, sampleKey, use2019) => {
const sample = data[sampleKey]
const setDates = sample.reduce((acc, dateObj)=>{
dateObj.timeseries.forEach((valObj)=>{
acc.add(valObj.date.toDateString())
if(use2019) {
acc.add(valObj.date.toDateString())
}
})
return acc
},new Set([]))
const dateArray = Array.from(setDates)
const result = dateArray.map((d)=> new Date(d)).sort((a,b)=> a-b)
return result
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more