/* class demo on Jan 15, 2015 By Mohit Hingorani if you have any questons regarding the code please email me at mohit@mat.ucsb.edu we will be going over on how to import any table into processing and arrays again in next class in this demo we are importing a simple integer table This demo creates a two-dimensional grid where each cell has 3 values: a verticallocation, a horizontal location, and a cell colorvalue. All the values are stored in a csv file which has 11 columns of integers acquired through a MySQL quary in the SPL database. There are 31rows of data representing a query which has looked for data for 31 days COMMENTS: any line that begins with // or is in-between /* */ /* is commented out and the program does not see it, only humans. ------------ Variables include int = are integers such as, 5, 89, 594803 float = are decimal based numbers such as 5.45, 1489.3482 string = "life", "overthetop" strings are a sequence of characters and alwasy in double quotes int []array = {34,65,2} is a 1 dimensional array, a list of data separated by commas int[][]array = a 2 dimensional array as we use in this demo */ /* initializing a variable outside of a function makes it global. The variable is active and available throughtout the programe Variables initialized inside a function are called local variables and lose their values once the program exits the function */ int radius =100; int xPosition = 20; /*Creating a two-dimensional array we are calling "Data Matrix", and initialize its values to null. A two-dimensional array is really nothing more than an array of arrays (a three-dimensional array is an array of arrays of arrays). */ int [][] dataMatrix = null; // a variable to keep track of the MaxCount int maxCount ; //variables to represent the vertical and horizontal locations of the grid we will draw int numRows, numColumns; // crating a font of size 16 and helvetica PFont font = createFont( "Helvetica", 16, true); // creating a table. Table objects store data with multiple rows and columns, much like in a traditional spreadsheet Table myTable; void setup() { //setting up a screen size 640*360 size( 640,360); // set background to white background(255); //activate smoothing anti-aliasing functions between pixels smooth(); // set framerate to 10 per second. NOrmal video is 30 or 60 frameRate( 10); //Print out the text in the command prompt once the program reaches here println( " setup done"); //in this step we copy the data from the csv file into our own table. //we use header flag to tell processing to ignore the first line of the csv file as it contains the names of the columns myTable = loadTable( "onemonthmindacbkjcbk.csv", "header"); // assign these variables to contain the number of rows and columns from myTable numRows = myTable.getRowCount(); numColumns = myTable.getColumnCount(); // initialize the 2D array with number of rows and columns dataMatrix = new int[numRows][numColumns]; // copy everything from table into a 2D array // use two for loops to iterate over both rows and columns // this step is not necessary, but helps in matrix manipulations which we will be doing later for ( int i = 0 ; i< numRows ; i++) // a for loop where i is set to 0 and increments all the way to numRows by 1 { for ( int j = 0 ; j< numColumns ; j++)// a for loop where j is set to 0 and increments all the way to numColumns by 1 { dataMatrix[i][j] = myTable.getInt(i,j); // copying the table integer value at mytable (i,j) position into the dataMatrix print( dataMatrix[i][j ] + " "); // print out the value of dataMatrix } println(); // switch to next line in the prompt, improves legibility } // selecting the maximum number from dataMatrix maxCount = dataMatrix[0][0]; // we set our maxCount variable to dataMatrix[0][0] , then change it if we encounter a larger number for ( int i = 0 ; i< numRows ; i++) { for ( int j = 0 ; j< numColumns ; j++) { if ( dataMatrix[i][j] > maxCount) // this is an if statement which checks for the condition in brackets // if true, it executes the statements in brackets { maxCount = dataMatrix[i][j]; } } } println( numRows +" "+ numColumns + " " + maxCount ); // printing out the variables } //The Draw() processing function repeats itselfs continuously at 60 frames per second unless the frameRate is changed void draw() { // refresh background every frame background(255); //creating the cells sequentially // this where the actual drawing of data happens // we run two nested loops, one for rows and the other for columns , we go through each cell to retrive its value // and fill the color of the cell accordingly for ( int i = 0 ; i< numRows ; i++) { for ( int j = 0 ; j < numColumns ; j++) // -1 as the last column is empty, our dataMatrix is smaller than the table by one column { fill( 255* dataMatrix[i][j] / maxCount ); // when we divide dataMatrix by maxCount, we should recieve a value between 0 and 1. // on multiplying it by 255, we use the full color range from 0 to 255 rect( i * 16 ,j * 26 ,15,25 ); // draw the rectange of size 15,25 at the appropriate position, the position of the cell depends on its value in the table position (i,j) } } //the % is called modulo. It finds the remainder after division. So 21% 20 returns 1, 21%12 returns 7, 21%21 returns 0 if ( frameCount % 20 ==0) { println( "draw called " + frameCount ); // here we are printing out the frameCount ( how many frames have been drawn so far) } }