var table; var tickDistanceX = 250/5, tickDistanceY = 200/5; var tickValC, tickValD; var minmax = {}; var plot1 = []; var plot2 = []; var plot3 = []; var plot4 = []; function preload() { table = loadTable('airlines.csv', 'csv', 'header'); } function setup(){ createCanvas(1000,1000); minmax["Cancelled"] = [9999999, 0]; // min, max minmax["Delayed"] = [999999, 0]; // finding min and max for (var j = 0; j < table.getRowCount(); j++){ if (table.getString(j, 'Code') === "SFO"){ if (table.getNum(j, 'Delayed') > minmax['Delayed'][1]){ minmax['Delayed'][1] = table.getNum(j, 'Delayed'); } if (table.getNum(j, 'Delayed') < minmax['Delayed'][0]){ minmax['Delayed'][0] = table.getNum(j, 'Delayed'); } if (table.getNum(j, 'Cancelled') > minmax['Cancelled'][1]){ minmax['Cancelled'][1] = table.getNum(j, 'Cancelled'); } if (table.getNum(j, 'Cancelled') < minmax['Cancelled'][0]){ minmax['Cancelled'][0] = table.getNum(j, 'Cancelled'); } } } // adjust scales to get decimal free while ((minmax['Delayed'][1] % 5) != 0){ minmax['Delayed'][1] = minmax['Delayed'][1] + 1; } while ((minmax['Cancelled'][1]%5) != 0){ minmax['Cancelled'][1] = minmax['Cancelled'][1] + 1; } while ((minmax['Delayed'][0]% 5) != 0){ minmax['Delayed'][0] = minmax['Delayed'][0] - 1; } while ((minmax['Delayed'][0]%5) != 0){ minmax['Delayed'][0] = minmax['Delayed'][0] - 1; } var cancelledRange = minmax['Cancelled'][1] - minmax['Cancelled'][0]; var delayedRange = minmax['Delayed'][1] - minmax['Delayed'][0]; tickValC = cancelledRange/5; tickValD = delayedRange/5; // plot 1 - delayed (y) vs cancelled (x) 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'); plot1.push(new Point(getStartPointX(1, cancelled), getStartPointY(1, delayed), cancelled, delayed)); } } // plot 2 - delayed (y) vs delayed (x) for (var i = 0; i < table.getRowCount(); i++){ if (table.getString(i, 'Code') === "SFO"){ delayed = table.getNum(i, 'Delayed'); plot2.push(new Point(getStartPointX(2, delayed), getStartPointY(2, delayed), delayed, delayed)); } } // plot 3 - cancelled (y) vs cancelled (x) for (var i = 0; i < table.getRowCount(); i++){ if (table.getString(i, 'Code') === "SFO"){ cancelled = table.getNum(i, 'Cancelled'); plot3.push(new Point(getStartPointX(3, cancelled), getStartPointY(3, cancelled), cancelled, cancelled)); } } // plot 4 - cancelled (y) vs delayed (x) for (var i = 0; i < table.getRowCount(); i++){ if (table.getString(i, 'Code') === "SFO"){ delayed = table.getNum(i, 'Delayed'); cancelled = table.getNum(i, 'Cancelled'); plot4.push(new Point(getStartPointX(4, delayed), getStartPointY(4, cancelled), delayed, cancelled)); } } } function getStartPointY(plotNum, value){ if (plotNum == 1 || plotNum == 2){ maxY = minmax['Delayed'][1]; return 50+(((maxY-value)/tickValD)*tickDistanceY); } else{ maxY = minmax['Cancelled'][1]; return 270+(((maxY-value)/tickValC)*tickDistanceY); } } function getStartPointX(plotNum, value){ if (plotNum == 1 || plotNum == 3){ maxX = minmax['Cancelled'][1]; return 300-(((maxX-value)/tickValC)*tickDistanceX); } else{ maxX = minmax['Delayed'][1]; return 570-(((maxX-value)/tickValD)*tickDistanceX); } } function draw(){ clear(); // boxes noFill(); for (var i = 0; i < 2; i++){ rect(50+(270*i),50, 250,200); } for (var i = 0; i < 2; i++){ rect(50+(270*i),270, 250,200); } fill(0); textSize(10); // y axes for (var counter = 0; counter < 2; counter++){ for (var j = 0; j <= 5; j++){ line(48, 50+(tickDistanceY*j)+(220*counter), 53, 50+(tickDistanceY*j)+(220*counter)); // does not depend on range // values for cancelled if (counter == 1){ text(minmax['Cancelled'][1]-(tickValC*j), 20, 53+(tickDistanceY*j)+(220*counter)); } // values for delayed else{ text(minmax['Delayed'][1]-(tickValD*j), 15, 53+(tickDistanceY*j)+(220*counter)); } } } // x axes for (var counter = 0; counter < 2; counter++){ for (var j = 0; j <=5; j++){ line((300+(270*counter))-(tickDistanceX*j), 464, (300+(270*counter))-(tickDistanceX*j), 473); if (counter==0){ text(minmax['Cancelled'][1]-(tickValC*j), (290+(270*counter))-(tickDistanceX*j), 485); } else{ text(minmax['Delayed'][1]-(tickValD*j), (290+(270*counter))-(tickDistanceX*j), 485) } } } var xcoord, ycoord, xval, yval, isHover = false; for (var f = 0; f < plot1.length; f++){ plot1[f].show(); if (plot1[f].checkHover() == true){ isHover = true; xcoord = plot1[f].getxCoord(); ycoord = plot1[f].getyCoord(); xval = plot1[f].getxVal(); yval = plot1[f].getyVal(); } } for (var f = 0; f < plot2.length; f++){ plot2[f].show(); if (plot2[f].checkHover() == true){ isHover = true; xcoord = plot2[f].getxCoord(); ycoord = plot2[f].getyCoord(); xval = plot2[f].getxVal(); yval = plot2[f].getyVal(); } } for (var f = 0; f < plot3.length; f++){ plot3[f].show(); if (plot3[f].checkHover() == true){ isHover = true; xcoord = plot3[f].getxCoord(); ycoord = plot3[f].getyCoord(); xval = plot3[f].getxVal(); yval = plot3[f].getyVal(); } } for (var f = 0; f < plot4.length; f++){ plot4[f].show(); if (plot4[f].checkHover() == true){ isHover = true; xcoord = plot4[f].getxCoord(); ycoord = plot4[f].getyCoord(); xval = plot4[f].getxVal(); yval = plot4[f].getyVal(); } } if (isHover){ fill('red'); ellipse(xcoord, ycoord, 6); fill(240); rect(xcoord-40, ycoord-40, 60, 30); fill(0); text("(" + xval + ", " + yval + ")", xcoord-35, ycoord-20); } textSize(12); text("Scatterplot Matrix of Cancelled vs Delayed Flights at SFO in a Month", 100, 20); text("# of Cancelled Flights", 100, 500); text("# of Delayed Flights", 400, 500); rotate(PI/2); text("# of Delayed Flights", 100, -5); text("# of Cancelled Flights", 300, -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; } }