xxxxxxxxxx
63
let cases;
// COVID DATA FROM CANADA FROM THE FOLLOWING API
// https://opencovid.ca/api/
// For data that resembles what you worked on, look at the summary data
// https://api.opencovid.ca/timeseries?stat=cases&loc=prov
function preload() {
// we provide the url we want to get the data from
// this one is a bit simpler as it contains a summary for a date
const url = 'https://api.opencovid.ca/summary?date=01-09-2020';
// fetch is a function native to javaScript that lets us get information from
// other computers (using APIs)
fetch(url)
// the fetch method returns a promise and as soon as it is resolved
// makes the content available to us. In this case we called the
// data recived 'response', but it comes as a string, and we want it
// as JSON (javaScript object notation)
// we return the recived data after using the json() method to convert it
// to something we can parse
.then(function(response){
return response.json()
})
// we get the json information and store the summary part of it
// in the cases array
.then(function(json) {
// from the json object we get just the summary
cases = json.summary;
return cases;
})
// this last part we use in case that there is an error in the process
// for now we just console.log it
.catch(function(err) {
console.log(err)
});
}
function setup() {
createCanvas(400, 400);
}
function draw() {
// this if statement is checking if the process getting the cases has
// completed, and if not it returns
if (!cases) {
return;
}
// when we have cases this code runs
// first I'm logging the cases. It is a group of objects with information
// right click on the current window browser, click inspect
// and select from the top menu 'Console', there will be a
// something that says 'Cases ARRAY' and a below an array with
// objects. you can expand it to see the different types of
// data presented
console.log('Cases ARRAY ', cases);
background(200);
for(let i = 0; i < cases.length;i++){
console.log(cases[i].province);
}
noLoop(); // we tell it not too loop.
}