Published
Edited
Jun 10, 2021
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
domainsZScore = domains.map(domain => ({
...domain,
indicators: domain.indicators.map(indicator => {
const direction = indicator.negative ? -1 : 1;
return {
...indicator,
values: indicator.values.map(value => ({
...value,
zScore: ((value.value - indicator.mean) / indicator.stddev) * direction,
}))
}
})
}))
Insert cell
Insert cell
Insert cell
Insert cell
domainScores = domainsZScore.map(domain => ({
...domain,
domainScore: domain.indicators.reduce((memo, indicator) => indicator.values.map(value => {
const { geoid, zScore } = value;
const match = memo.find(m => m.geoid === geoid);
return {
geoid,
domainScore: match ? match.domainScore + zScore : zScore,
}
}), []).map(ds => ({
...ds,
domainScore: ds.domainScore / domain.indicators.length,
}))
}))
Insert cell
Insert cell
Insert cell
Insert cell
finalScores = domainScores.reduce((memo, domain) => domain.domainScore.map(value => {
const { geoid, domainScore } = value;
const weighted = domainScore * (domain.weight / 100);
const match = memo.find(m => m.geoid === geoid);
return {
geoid,
value: match ? match.value + weighted : weighted,
}
}), [])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function stats(collection) {
return {
mean: d3.mean(collection, d => d.value),
stddev: d3.deviation(collection, d => d.value),
}
}
Insert cell
Insert cell
response = (await fetch('https://hpi2.onrender.com/api/v1/indicators')).json();
Insert cell
Insert cell
Insert cell
indicatorData = await Promise.all(indicatorsFlat.map(indicator =>
fetch('https://hpi2.onrender.com/api/v1/indicator', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
mode: 'cors',
referrerPolicy: 'no-referrer',
body: JSON.stringify({
geography: 12,
indicator: indicator.id,
attributes: ['value'],
})
})
.then(res => res.json())
.then(json => ({
id: indicator.id,
name: indicator.title,
negative: indicator.negative,
values: json.response,
...stats(json.response)
}))
));
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