var table; function preload(){ var origFileName = '698.csv' table = loadTable(origFileName, 'csv'); } //file:///C:/Jareds_Stuff/1_JuniorYear/Spring_2018/1_DataVis/Assignment/AS3_Graphs/index.html var uniqueVertArr = []; var verticesArr = []; var edgesArr = []; //interval: the size of each box to be used var interval = 10; //TODO: CAN'T CHANGE THE CANVAS SETTINGS TO LET MULTIPLE GRAPHS ON ONE PAGE function setup() { //can change colOfData to column num of whatever data you want var canvas = createCanvas(5000, 5000); background(750,250,210); //col = 03 var arr = []; print(table.getRowCount() + ' total rows in table'); //Gets the data stored in the first row, separated by commas (how my csv ends up) var str1 = table.getString(0,0); var strArr = str1.split(","); for(var i = 0; i < strArr.length; i++) { var arrVertStr = strArr[i].split(" "); var edgeData = []; for(var k = 0; k < arrVertStr.length; k++) { //loop through the strArr, convert each value to int vertex var vertex = new Object(); var nodeID = parseInt(arrVertStr[k]); vertex.nodeID = nodeID; vertex.axisPos = 0; vertex.posY = 0; vertex.dispX = 0; vertex.dispY = 0; //If nodeID is a number, then addToVertices and build the edgeData Object; if(!isNaN(nodeID)) { addToVertices(uniqueVertArr, vertex); edgeData.push(nodeID); } } //If the edge is a number, push the edge pair to edgesArr if(!isNaN(edgeData[0]) || !isNaN(edgeData[1])) { edgesArr.push(edgeData); } } //sort verticesArr by nodeID, in ascending order sortVertices(); //assign x position on the x axis, the y position on the y axis assignPositions(); } // Check if vertex is not in uniueVertArr, then add that to array and to verticesArr function addToVertices (vertices, vertex) { //If the vertex has not been added to the uniqueVertArr list, // then add vertex to verticesArr and vertex.nodeID to uniqueVertArr if (vertices.indexOf(vertex.nodeID) == -1) { vertices.push(vertex.nodeID); verticesArr.push(vertex); } } //Sort the vertices in ascending order: verticesArr then uniqueVertArr function sortVertices() { verticesArr.sort(function(a, b) { return parseFloat(a.nodeID) - parseFloat(b.nodeID); }); uniqueVertArr.sort(function(a, b) { return parseFloat(a) - parseFloat(b); }); } //Loop through verticesArr, assigning x and y position as their (index * interval) function assignPositions() { for(var index = 0; index < verticesArr.length; index++) { verticesArr[index].axisPos = 100 + index * interval; } } //draw the edges as boxes in terms of colored squares function drawEdges() { for(var eIndex = 0; eIndex < edgesArr.length; eIndex++) { var firstEdge = edgesArr[eIndex][0]; var secEdge = edgesArr[eIndex][1]; var firstIndex = uniqueVertArr.indexOf(firstEdge); var secIndex = uniqueVertArr.indexOf(secEdge); var xVal = verticesArr[firstIndex].axisPos; var yVal = verticesArr[secIndex].axisPos; fill(color('blue')); //draw edge rect(xVal, yVal, interval, interval); //draw reverse edge (undirected) rect(yVal, xVal, interval, interval); if(mouseX > xVal && mouseY > yVal && mouseX < (xVal + interval) && mouseY < (yVal + interval)) { var c = color(750,250,210); fill(c); rect(xVal-35, yVal - 60, 130, 40); fill('red'); rect(xVal, yVal, interval, interval); textSize(30); var txtBox = "("+firstEdge.toString() + ", " + secEdge.toString() + ")"; text(txtBox, xVal - 30, yVal - 30); } else if(mouseX > yVal && mouseY > xVal && mouseX < (yVal + interval) && mouseY < (xVal + interval)) { var c = color(750,250,210); fill(c); rect(yVal-35, xVal - 60, 130, 40); fill('red'); rect(yVal, xVal, interval, interval); textSize(30); var txtBox = "("+secEdge.toString() + ", " + firstEdge.toString() + ")"; text(txtBox, yVal - 30, xVal - 30); } } } function drawAxis(){ background(750,250,210); //initial graph corners values: tleft: 50,50 tright: (850,50) bleft: var count = verticesArr.length; var squareSize = count * interval; strokeWeight(2); //top left to top right line(100, 100, 100+squareSize, 100); //top right to bottom right line(100+squareSize, 100, 100+squareSize, 100+squareSize); //bottom right to bottom left line(100+squareSize, 100+squareSize, 100, 100+squareSize); //bottom left to top left line(100, 100+squareSize, 100, 100); strokeWeight(.1); for(var index = 0; index < verticesArr.length; index++) { //offset = 100 var val = 100 + (index * interval); var lowBound = 100; var upBound= 100 + squareSize; //vertical lines line(val, lowBound, val, upBound); //horizontal lines line(lowBound, val, upBound, val); } //drawEdges(); } function draw() { //background(700); //background(500,250,210); //Draw Axis also draw's the bars (saves having to pass information along methods) //drawAxis(); //noLoop(); drawAxis(); drawEdges(); }