xxxxxxxxxx
193
//Creating an array of strings to hold the entire file
let strings = [];
//Loading the file for data visualization
//so it finishes loading before the program begins
function preload() {
strings = loadStrings("death.csv");
}
function setup() {
createCanvas(500, 400);
background(255);
// Checking if the load was successful
if (strings == null) {
print("failed to load the file, stopping here");
// this is an endless loop; it's a common way
// to prevent a program from continuing when
// something is so wrong that there is no sense
// in continuing
//while (true) {}
// we comment while statement out because it
// slows down the program running, but we need to
// leave it here in case of debugging
}
// make sure that the file is successfully loaded
print(
"strings loaded from file successfully, read " + strings.length + " lines"
);
let singleRow = [];
// loop over each row in the file
// we do it in the setup function, so we don't need
// a draw function
for (let csvRowNumber = 1; csvRowNumber < strings.length; csvRowNumber++) {
// get a single row and split that row
// into individual values by ','
singleRow = split(strings[csvRowNumber], ",");
// Assigning variables to calculate the sum
// of all deaths in a single row
// the name of the variable corresponds to
// the name of the disease
// we use int to convert the value into integer
// the number in [] corresponds to the placement of // each value in a row
let deathsMeningitis = int(singleRow[3]);
let deathsAlzheimers = int(singleRow[4]);
let deathsParkinsons = int(singleRow[5]);
let deathsNutritional = int(singleRow[6]);
let deathMalaria = int(singleRow[7]);
let deathsDrowning = int(singleRow[8]);
let deathsInterpersonalViolence = int(singleRow[9]);
let deathsMaternalDisorders = int(singleRow[10]);
let deathsHIV = int(singleRow[11]);
let deathsDrugUse = int(singleRow[12]);
let deathsTuberculosis = int(singleRow[13]);
let deathsCardiovascular = int(singleRow[14]);
let deathsLowerRespiratory = int(singleRow[15]);
let deathsNeonatal = int(singleRow[16]);
let deathsAlcoholUse = int(singleRow[17]);
let deathsSelfHarm = int(singleRow[18]);
let deathsExposureToNature = int(singleRow[19]);
let deathsDiarrheal = int(singleRow[20]);
let deathsEnvironmental = int(singleRow[21]);
let deathsNeoplasms = int(singleRow[22]);
let deathsConflictTerrorism = int(singleRow[23]);
let deathsDiabetes = int(singleRow[24]);
let deathsChronicKidney = int(singleRow[25]);
let deathsPoisonings = int(singleRow[26]);
let deathsProteinMalnutrition = int(singleRow[27]);
let deathsRoadInjuries = int(singleRow[28]);
let deathsChronicRespiratory = int(singleRow[29]);
let deathsCirrhosis = int(singleRow[30]);
let deathsDigestiveDiseases = int(singleRow[31]);
let deathsFireHeatHot = int(singleRow[32]);
let deathsAcuteHepatitis = int(singleRow[33]);
//Calculating the sum of deaths number
//for every disease in a single row
let totalDeathsThisRow =
deathsMeningitis +
deathsAlzheimers +
deathsParkinsons +
deathsNutritional +
deathMalaria +
deathsDrowning +
deathsInterpersonalViolence +
deathsMaternalDisorders +
deathsHIV +
deathsDrugUse +
deathsTuberculosis +
deathsCardiovascular +
deathsLowerRespiratory +
deathsNeonatal +
deathsAlcoholUse +
deathsSelfHarm +
deathsExposureToNature +
deathsDiarrheal +
deathsEnvironmental +
deathsNeoplasms +
deathsConflictTerrorism +
deathsDiabetes +
deathsChronicKidney +
deathsPoisonings +
deathsProteinMalnutrition +
deathsRoadInjuries +
deathsChronicRespiratory +
deathsCirrhosis +
deathsDigestiveDiseases +
deathsFireHeatHot +
deathsAcuteHepatitis;
// How big should our circles be?
// we use const instead of let, because we need to
// have it fixed for later creation of circles
// Specifically it is going to be used in assigning
// the radius of a circle
const MIN_CIRCLE_SIZE = 1;
const MAX_CIRCLE_SIZE = 10;
// Scaling the radius of circles using map
// 0, 500000 is a range for true values in the file
// MIN_CIRCLE_SIZE, MAX_CIRCLE_SIZE is
// the range for scaled values
// print(totalDeathsThisRow);
// print is here if debugging is needed
// If statement to make sure that we know
// what row is not defined
if (isNaN(totalDeathsThisRow)) {
print(csvRowNumber);
}
// Scaling the radius of a circle
let circleRadius = map(
totalDeathsThisRow,
0,
500000,
MIN_CIRCLE_SIZE,
MAX_CIRCLE_SIZE
);
// Assigning a random value for position of a circle
let xPosition = random(width);
let yPosition = random(height);
//print (x,y);
// Drawing a circle in x&y position which is random
// with a range of 1 to 10 which was defined in
// circleRadius
circle(xPosition, yPosition, circleRadius);
// Use unchar function to convert a
// single-character string to its corresponding
// integer representation
// We also use split because we have 3 letters in a
// country code and we need to extract only
// the first letter
let countryCode = unchar(split(singleRow[1], "")[0]);
print(countryCode);
// We know that the country code as represented by
// a number is going to be between 65 and 90 that
// corresponds to letters from A to Z
// We assign red values to the letters
let redValue = map(countryCode, 65, 90, 0, 255);
// Coloring the circle in a range for red value
// and fixed green,blue and alpha values
noStroke();
fill(redValue, 100, 100, 155);
} // end of for loop
} // end of setup function