async function formatOntarioData(caseData, geoData, canadaCaseHistory) { return sortOntarioData(caseData, geoData, canadaCaseHistory) } async function getOntarioData() { //let dataRaw = await fetch ('/polopoly_fs/1.4950797!/httpFile/file.txt') //let dataRaw = await fetch ('https://beta.ctvnews.ca/content/dam/common/exceltojson/ontario-data.txt') //let dataRaw = await fetch (`../data/ontario.json`); //let data = await dataRaw.json(); let data = await d3.csv(`https://beta.ctvnews.ca/content/dam/common/exceltojson/conposcovidloc.txt`) //let data = await d3.csv(`../data/conposcovidloc.txt`); //console.log(data) let healthBoundaryRaw = await fetch ('/polopoly_fs/1.4950818!/httpFile/file.txt'); //let healthBoundaryRaw = await fetch (`../data/ontario-health-boundaries.json`); let healthBoundary = await healthBoundaryRaw.json(); let canadaCaseHistoryRaw = await fetch (`https://beta.ctvnews.ca/content/dam/common/exceltojson/COVID-19-Canada-New.txt`); //let canadaCaseHistoryRaw = await fetch (`../../main-tracker/Data/updated-data.json`); let canadaCaseHistory = await canadaCaseHistoryRaw.json(); let [caseData, geoData, ontarioData] = await formatOntarioData(data, healthBoundary, canadaCaseHistory); return [caseData, geoData, ontarioData] } function sortOntarioData(caseData, geoData, canadaCaseHistory) { let healthUnitArray = []; //console.log(geoData) //console.log(canadaCaseHistory) let ontarioData = []; canadaCaseHistory.forEach((day, i, array) => { let date = toDate(day['Date']) let dayNum = String(date.getDate()).padStart(2, '0'); let monthNum = String(date.getMonth() + 1).padStart(2, '0'); let yearNum = date.getFullYear(); ontarioData.push( { date: `${yearNum}-${monthNum}-${dayNum}`, cases: Number(day['ON_Total']), new: Number(day['ON_Total']) - (i > 0 ? Number(array[i-1]['ON_Total']) : 0), } ) }) ontarioData = ontarioData.filter(day => day.cases !== 0) //console.log('fetch', ontarioData) caseData.forEach(person => { //caseData.result.records.forEach(person => { let healthObj = healthUnitArray.find(object => object.healthUnit === person['Reporting_PHU']); if (healthObj) { healthObj.cases.push(person) if (person.OUTCOME1 === 'Resolved') { healthObj.recovered.push(person) } else if (person.OUTCOME1 === 'Fatal') { healthObj.deaths.push(person) } else if (person.OUTCOME1 === 'Not Resolved') { healthObj.active.push(person) } } else { let newHealth = { healthUnit: person['Reporting_PHU'], cases: [] } newHealth.cases.push(person) healthUnitArray.push(newHealth) } }) function count(data, key, values) { let tally = new Array(values.length).fill(0); data.forEach(person => { values.forEach((value, i) => { if (person[key] === value) { tally[i] += 1; } }) }) return tally; } geoData.features.forEach(region => { let hu = healthUnitArray.find(unit => unit.healthUnit === region.properties.ENGNAME); region.data = hu; /* region.data.recovered = region.data.cases.filter(person => person.Outcome1 === 'Resolved') region.data.active = region.data.cases.filter(person => person.Outcome1 === 'Not Resolved') region.data.deaths = region.data.cases.filter(person => person.Outcome1 === 'Fatal') */ }) geoData.data = {} //geoData.data.cases = caseData.result.records; geoData.data.cases = caseData; geoData.data.recovered = geoData.data.cases.filter(person => person.Outcome1 === 'Resolved') geoData.data.active = geoData.data.cases.filter(person => person.Outcome1 === 'Not Resolved') geoData.data.deaths = geoData.data.cases.filter(person => person.Outcome1 === 'Fatal') geoData.data.healthUnit = 'Ontario'; geoData.current = { cases: geoData.data.cases.length, active: geoData.data.active.length, recovered: geoData.data.recovered.length, deaths: geoData.data.deaths.length } function addFilterArray(region, newKey, filter, filterKey) { region.data.timeline.forEach(day => { day[newKey] = day.cases.filter(person => person[filter] === filterKey) }) } sortGenderAges(geoData) //createSortedTimeline(geoData); //let [firstDate, lastDate] = fillOutTimeline(geoData); /* geoData.data.timeline.forEach(day => { addFilterArray(geoData, 'recovered', 'Outcome1','Resolved') addFilterArray(geoData, 'deaths', 'Outcome1', 'Fatal') }) */ function sortGenderAges(region) { let rangeArray = ['<20', '20s', '30s', '40s', '50s', '60s', '70s', '80s', '90+'] region.data.gender = { male: { cases: region.data.cases.filter(person => person['Client_Gender'] === 'MALE'), ages: [] }, female: { cases: region.data.cases.filter(person => person['Client_Gender'] === 'FEMALE'), ages: [] } }; rangeArray.forEach(range => { region.data.gender.male.ages.push({age: range, cases: 0, deaths: 0}) region.data.gender.female.ages.push({age: range, cases: 0, deaths: 0}) }) for (gender in region.data.gender) { region.data.gender[gender].cases.forEach(person => { let obj = region.data.gender[gender].ages.find(age => age.age === person['Age_Group']) if (obj) { obj.cases += 1; //if (person['Age_Group'] === '<20' && person.Outcome1 === 'Fatal') {console.log(person)} if (person.Outcome1 === 'Fatal') { obj.deaths += 1; } } }) } } let populationArray = [ { PHU: "Hamilton Public Health Services", Population: 576272 }, { PHU: "Hastings and Prince Edward Counties Health Unit", Population: 144779 }, { PHU: "Huron Perth District Health Unit", Population: 136672 }, { PHU: "Chatham-Kent Health Unit", Population: 105303 }, { PHU: "Kingston, Frontenac and Lennox & Addington Public Health", Population: 206768 }, { PHU: "Lambton Public Health", Population: 115985 }, { PHU: "Leeds, Grenville and Lanark District Health Unit", Population: 153598 }, { PHU: "Middlesex-London Health Unit", Population: 492971 }, { PHU: "Niagara Region Public Health Department", Population: 450816 }, { PHU: "North Bay Parry Sound District Health Unit", Population: 128804 }, { PHU: "Northwestern Health Unit", Population: 74771 }, { PHU: "Ottawa Public Health", Population: 1019693 }, { PHU: "Peel Public Health", Population: 1553076 }, { PHU: "Peterborough Public Health", Population: 144237 }, { PHU: "Porcupine Health Unit", Population: 90540 }, { PHU: "Renfrew County and District Health Unit", Population: 106578 }, { PHU: "Eastern Ontario Health Unit", Population: 179454 }, { PHU: "Simcoe Muskoka District Health Unit", Population: 562142 }, { PHU: "Sudbury & District Health Unit", Population: 202681 }, { PHU: "Thunder Bay District Health Unit", Population: 154444 }, { PHU: "Timiskaming Health Unit", Population: 33389 }, { PHU: "Region of Waterloo, Public Health", Population: 571232 }, { PHU: "Wellington-Dufferin-Guelph Public Health", Population: 304193 }, { PHU: "Windsor-Essex County Health Unit", Population: 348836 }, { PHU: "York Region Public Health Services", Population: 1200761 }, { PHU: "Southwestern Public Health", Population: 209238 }, { PHU: "Toronto Public Health", Population: 2987513 }, { PHU: "Algoma Public Health Unit", Population: 111060 }, { PHU: "Brant County Health Unit", Population: 152029 }, { PHU: "Durham Region Health Department", Population: 699641 }, { PHU: "Grey Bruce Health Unit", Population: 154672 }, { PHU: "Haldimand-Norfolk Health Unit", Population: 113098 }, { PHU: "Haliburton, Kawartha, Pine Ridge District Health Unit", Population: 185663 }, { PHU: "Halton Region Health Department", Population: 605475 } ] geoData.features.forEach(region => { //createSortedTimeline(region); //fillOutTimeline(region, firstDate, lastDate) //addFilterArray(region, 'recovered', 'Outcome1', 'Resolved') //addFilterArray(region, 'deaths', 'Outcome1', 'Fatal') sortGenderAges(region) region.population = populationArray.find(phu => phu.PHU === region.data.healthUnit).Population; let tally = count(region.data.cases, 'Outcome1', ['Resolved', 'Not Resolved', 'Fatal']); region.current = { cases: region.data.cases.length, recovered: tally[0], active: tally[1], deaths: tally[2] } region.currentPop = { cases: Math.round((region.data.cases.length/(region.population/100000))*10)/10, recovered: Math.round((tally[0]/(region.population/100000)*10)/10), active: Math.round((tally[1]/(region.population/100000))*10)/10, deaths: Math.round((tally[2]/(region.population/100000))*10)/10 } }) function toDate(excelDate) { return new Date((excelDate - (25567 + 1))*86400*1000); } return [caseData, geoData, ontarioData]; }