var table; var minmax = {}; var coordinates = []; var tickDistance = 500/11; var tickValCarrier, tickValWeather, tickValSecurity; function preload() { table = loadTable('airlines.csv', 'csv', 'header'); } function setup(){ createCanvas(1000,700); minmax["carrier"] = [9999999, 0]; // min, max minmax["weather"] = [999999, 0]; minmax["security"] = [999999, 0]; // get min and max for (var j = 0; j < table.getRowCount(); j++){ if (table.getString(j, 'Code') === "SFO"){ if (table.getNum(j, '# of Delays.Carrier') > minmax['carrier'][1]){ minmax['carrier'][1] = table.getNum(j, '# of Delays.Carrier'); } if (table.getNum(j, '# of Delays.Carrier') < minmax['carrier'][0]){ minmax['carrier'][0] = table.getNum(j, '# of Delays.Carrier'); } if (table.getNum(j, '# of Delays.Weather') > minmax['weather'][1]){ minmax['weather'][1] = table.getNum(j, '# of Delays.Weather'); } if (table.getNum(j, '# of Delays.Weather') < minmax['weather'][0]){ minmax['weather'][0] = table.getNum(j, '# of Delays.Weather'); } if (table.getNum(j, '# of Delays.Security') > minmax['security'][1]){ minmax['security'][1] = table.getNum(j, '# of Delays.Security'); } if (table.getNum(j, '# of Delays.Security') < minmax['security'][0]){ minmax['security'][0] = table.getNum(j, '# of Delays.Security'); } } } // adjust scales to get decimal free while ((minmax['carrier'][1] % 10) != 0){ minmax['carrier'][1] = minmax['carrier'][1] + 1; } while ((minmax['carrier'][0]% 10) != 0){ minmax['carrier'][0] = minmax['carrier'][0] - 1; } while ((minmax['weather'][1] % 10) != 0){ minmax['weather'][1] = minmax['weather'][1] + 1; } while ((minmax['weather'][0]% 10) != 0){ minmax['weather'][0] = minmax['weather'][0] - 1; } while ((minmax['security'][1] % 10) != 0){ minmax['security'][1] = minmax['security'][1] + 1; } while ((minmax['security'][0]% 10) != 0){ minmax['security'][0] = minmax['security'][0] - 1; } var carrierRange = minmax['carrier'][1] - minmax['carrier'][0]; var weatherRange = minmax['weather'][1] - minmax['weather'][0]; var securityRange = minmax['security'][1] - minmax['security'][0]; tickValCarrier = carrierRange/10; tickValWeather = weatherRange/10; tickValSecurity = securityRange/10; // create coordinate objects var carrier, weather, security, cy, wy, sy, month, year; for (var j = 0; j < table.getRowCount(); j++){ if (table.getString(j, 'Code') === "SFO"){ carrier = table.getNum(j, '# of Delays.Carrier'); weather = table.getNum(j, '# of Delays.Weather'); security = table.getNum(j, '# of Delays.Security'); cy = getStartCarrier(carrier); wy = getStartWeather(weather); sy = getStartSecurity(security); coordinates.push(new Coordinate(cy, wy, sy, carrier, weather, security, month, year)); } } } // y values function getStartCarrier(value){ return 50+(((minmax['carrier'][1]-value)/tickValCarrier)*tickDistance); } function getStartWeather(value){ return 50+(((minmax['weather'][1]-value)/tickValWeather)*tickDistance); } function getStartSecurity(value){ return 50+(((minmax['security'][1]-value)/tickValSecurity)*tickDistance); } function draw(){ clear(); var index, isHover = false; for (var j = 0; j < coordinates.length; j++){ strokeWeight(1); coordinates[j].show(); if (coordinates[j].checkHover()){ isHover = true; index = j; } } fill(0); strokeWeight(0); stroke(0); text("Carrier", 75, 525); text("Weather", 325, 525); text("Security", 575, 525); text("Number of Delays in a Month Due to Carrier, Weather, and Security at SFO", 125, 30); strokeWeight(1); // axes line(100, 50, 100, 505); line(350, 50, 350, 505); line(600, 50, 600, 505); // ticks on carrier for (var i = 0; i < 11; i++){ strokeWeight(1); line(96, 50+(tickDistance*i), 104, 50+(tickDistance*i)); strokeWeight(0); text(minmax['carrier'][1]-(tickValCarrier*i), 65, 55+(tickDistance*i)); } // ticks on weather for (var i = 0; i < 11; i++){ strokeWeight(1); line(346, 50+(tickDistance*i), 354, 50+(tickDistance*i)); strokeWeight(0); text(minmax['weather'][1]-(tickValWeather*i), 320, 55+(tickDistance*i)); strokeWeight(1); } // ticks on security for (var i = 0; i < 11; i++){ strokeWeight(1); line(596, 50+(tickDistance*i), 604, 50+(tickDistance*i)); strokeWeight(0); text(minmax['security'][1]-(tickValSecurity*i), 575, 55+(tickDistance*i)); } if (isHover){ coordinates[index].getDetails(); } } class Coordinate { constructor(y1, y2, y3, val1, val2, val3){ this.y1 = y1; this.y2 = y2; this.y3 = y3; // stored info about the point this.carrier = val1; this.weather = val2; this.security = val3; // x coords this.x1 = 100; this.x2 = 350; this.x3 = 600; } show(){ fill(200); stroke(200); ellipse(this.x1, this.y1, 6); line(this.x1, this.y1, this.x2, this.y2); ellipse(this.x2, this.y2, 6); line(this.x2, this.y2, this.x3, this.y3); ellipse(this.x3, this.y3, 6); } checkHover(){ var dist1 = dist(this.x1, this.y1, mouseX, mouseY); var dist2 = dist(this.x2, this.y2, mouseX, mouseY); var dist3 = dist(this.x3, this.y3, mouseX, mouseY); if (dist1 < 4 || dist2 < 4 || dist3 < 4){ return true; } else{ return false; } } getDetails(){ stroke('red'); strokeWeight(3); ellipse(this.x1, this.y1, 3); line(this.x1, this.y1, this.x2, this.y2); ellipse(this.x2, this.y2, 3); line(this.x2, this.y2, this.x3, this.y3); ellipse(this.x3, this.y3, 3); fill(0); text(this.carrier, this.x1, this.y1); text(this.weather, this.x2, this.y2); text(this.security, this.x3, this.y3); } }