Public
Edited
Apr 23, 2024
1 fork
1 star
Insert cell
Insert cell
jsonURL = "https://api.bluegrasscancernav.me/counties"
Insert cell
data = fetch(jsonURL)
.then(response => {
if (!response.ok) throw new Error(response.status);
return response.json();
})
Insert cell
// Function to transform data into dictionaries
function transformCountyDataToDictionary(countyDataArray) {
const countyDictionary = {};
countyDataArray.forEach(data => {
countyDictionary[countyToFips.get(`${data.name} County`)] = {
name: data.name,
ageAdjustedRate: data.ageAdjustedRate,
cancer_rates: data.cancer_rates,
crudeCancerRate: data.crudeCancerRate,
location: data.location,
numberOfCases: data.numberOfCases,
relatedOncologists: data.relatedOncologists,
relatedTypes: data.relatedTypes,
totalPopulation: data.totalPopulation,
trendData: data.trendData
};
});
return countyDictionary;
}

Insert cell

// Transform the county data array into a dictionary
countyDictionary = transformCountyDataToDictionary(data);

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
cancer_rates = selectedFips == 21? () => {} : countyDictionary[selectedFips].cancer_rates;
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
rates = Object.values(countyDictionary).map(county => parseFloat(county.crudeCancerRate.replace(/,/g, '')))

Insert cell
cases = Object.values(countyDictionary).map(county => parseFloat(county.numberOfCases.replace(/,/g, '')))
Insert cell
maxRate = Math.max(...rates)
Insert cell
maxCases = Math.max(...cases)
Insert cell
Array.from({length: 9}, (_, i) => 0 + i * maxRate/9)
Insert cell
Insert cell
Insert cell
countyDictionary
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mutable selectedTrendData = selectedFips == 21? () => {
// Initialize sum and count variables to calculate the mean
let sum = [0, 0, 0, 0, 0];
let count = 0;

// Iterate through the countyDictionary
for (const fips in countyDictionary) {
const countyData = countyDictionary[fips];
const trendData = countyData.trendData;

// If trendData is defined and is an array, calculate the sum
if (Array.isArray(trendData) && trendData.length > 0) {
for (let i = 0; i < 5; i++) {
sum[i] += trendData[i];
}
count += 1; // Increment count by the length of trendData
}
}
for (let i = 0; i < 5; i++) {
sum[i] /= count;
}
return sum;
} : countyDictionary[selectedFips].trendData;
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more