var table; var maxX = 0, maxY = 0; var points0 = [], points1 = [], points2 = []; var tickDistanceX = 200/5, tickDistanceY = 200/5; var tickValueX, tickValueY; function preload() { table = loadTable('airlines.csv', 'csv', 'header'); } function setup(){ createCanvas(1000,1000); var minX = 99999999, minY = 99999999; for (var j = 0; j < table.getRowCount(); j++){ if (table.getString(j, 'Code') === "SFO" || table.getString(j, 'Code') === "LAX" || table.getString(j, 'Code') === "EWR"){ if (table.getNum(j, 'Delayed') > maxX){ maxX = table.getNum(j, 'Delayed'); } if (table.getNum(j, 'Delayed') < minX){ minX = table.getNum(j, 'Delayed'); } if (table.getNum(j, 'Cancelled') > maxY){ maxY = table.getNum(j, 'Cancelled'); } if (table.getNum(j, 'Cancelled') < minY){ minY = table.getNum(j, 'Cancelled'); } } } // adjust scales to get decimal free while ((maxX % 5) != 0){ maxX++; } while ((maxY%5) != 0){ maxY++ } while ((minX % 5) != 0){ minX--; } while ((minY%5) != 0){ minY--; } var rangeX = maxX - minX; var rangeY = maxY - minY; tickValueX = rangeX/5; tickValueY = rangeY/5; var delayed, cancelled; for (var i = 0; i < table.getRowCount(); i++){ if (table.getString(i, 'Code') === "SFO"){ delayed = table.getNum(i, 'Delayed'); cancelled = table.getNum(i, 'Cancelled'); points0.push(new Point(getStartPointX(0, delayed), getStartPointY(cancelled), delayed, cancelled)); } if (table.getString(i, 'Code') === "LAX"){ delayed = table.getNum(i, 'Delayed'); cancelled = table.getNum(i, 'Cancelled'); points1.push(new Point(getStartPointX(1, delayed), getStartPointY(cancelled), delayed, cancelled)); } if (table.getString(i, 'Code') === "EWR"){ delayed = table.getNum(i, 'Delayed'); cancelled = table.getNum(i, 'Cancelled'); points2.push(new Point(getStartPointX(2, delayed), getStartPointY(cancelled), delayed, cancelled)); } } } function getStartPointY(value){ return 50+(((maxY-value)/tickValueY)*tickDistanceY); } function getStartPointX(plotNum, value){ return (250+(300*plotNum))-(((maxX-value)/tickValueX)*tickDistanceX); } function draw(){ clear(); // y axes for (var i = 0; i < 3; i++){ line(50+(300*i),50,50+(300*i),250); } // x axes for (var i = 0; i < 3; i++){ line(50+(300*i), 250, 50+(300*i)+200, 250); } var counter = 0; textSize(8); for (var i = 0; i < 3; i++){ for (var j = 0; j <= 5; j++){ // y axis line(48+(300*counter), 50+(tickDistanceY*j), 53+(300*counter), 50+(tickDistanceY*j)); // does not depend on range text(maxY-(tickValueY*j), 25+(300*counter), 55+(tickDistanceY*j)); } for (var k = 0; k <= 5; k++){ // x axis line((250+(300*counter))-(tickDistanceX*k), 247, (250+(300*counter))-(tickDistanceX*k), 254); // does not depend on range text(maxX-(tickValueX*k), (245+(300*counter))-(tickDistanceX*k), 275); } counter++; } textSize(12); text("Number of Delayed Flights", 400, 300); text("Cancelled vs Delayed Flights in a Month", 400, 10); text("SFO", 125, 40); text("LAX", 425, 40); text("EWR", 725, 40); var xcoord, ycoord, xval, yval, isHover = false; for (var f = 0; f < points0.length; f++){ points0[f].show(); if (points0[f].checkHover() == true){ isHover = true; xcoord = points0[f].getxCoord(); ycoord = points0[f].getyCoord(); xval = points0[f].getxVal(); yval = points0[f].getyVal(); } } for (var f = 0; f < points1.length; f++){ points1[f].show(); if (points1[f].checkHover() == true){ isHover = true; xcoord = points1[f].getxCoord(); ycoord = points1[f].getyCoord(); xval = points1[f].getxVal(); yval = points1[f].getyVal(); } } for (var f = 0; f < points2.length; f++){ points2[f].show(); if (points2[f].checkHover() == true){ isHover = true; xcoord = points2[f].getxCoord(); ycoord = points2[f].getyCoord(); xval = points2[f].getxVal(); yval = points2[f].getyVal(); } } if (isHover){ fill('red'); ellipse(xcoord, ycoord, 6); fill(240); rect(xcoord-40, ycoord-40, 70, 30); fill(0); text("(" + xval + ", " + yval + ")", xcoord-35, ycoord-20); } rotate(PI/2); text("Number of Cancelled Flights", 75, -5); } class Point { constructor(x, y, xval, yval) { this.x = x; this.y = y; this.xval = xval; this.yval = yval; } show(){ fill(0); strokeWeight(0); ellipse(this.x, this.y, 3); strokeWeight(1); } checkHover(){ if (dist(this.x, this.y, mouseX, mouseY) < 2){ return true; } else{ return false; } } getxVal(){ return this.xval; } getyVal(){ return this.yval; } getxCoord(){ return this.x; } getyCoord(){ return this.y; } }