10.24 Share some Code

Transforming Data: Cultural Strategies in DataMining
Instructor: George Legrady
bowea324
Posts: 13
Joined: Tue Sep 13, 2011 10:58 am

Re: 10.24 Share some Code

Post by bowea324 » Mon Nov 21, 2011 7:08 am

Here, I compare 'political' texts to 'revolutionary' texts checked out during the twelve month cycle during 2007. I added a bit of a propagandistic tint to the color, and a transparency to each of the layered bar graphs so that if one of the layers was beneath one another at any time, it would remain visible with slight color variation.


import de.bezier.data.sql.*;

MySQL msql;

void setup()
{
size( 600, 600 );
background(255, 10, 100);

String user = "mat259";
String pass = "V1sual1zat1on";

/*String [] monthLables = new String[12];
monthLables[0] = "Jan";
monthLables[1] = "Feb";
monthLables[2] = "Mar";
monthLables[3] = "Apr";
monthLables[4] = "May";
monthLables[5] = "Jun";
monthLables[6] = "Jul";
monthLables[7] = "Aug";
monthLables[8] = "Sep";
monthLables[9] = "Oct";
monthLables[10] = "Nov";
monthLables[11] = "Dec";*/
// regular way of defining an array

String [] monthLables = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; // inline version of defining an array (same as above)

String database = "spl_years";

msql = new MySQL( this, "tango.mat.ucsb.edu", database, user, pass );

if ( msql.connect() )
{

msql.query( "select month(ckoutDateTime), count(*) from transactions2007 where title like '%political%' "+
"group by month(ckoutDateTime) order by month(ckoutDateTime)" );

int month2Count = 0;

while(msql.next()) {

int amonth = msql.getInt(1);
int count = msql.getInt(2);

fill(255, 455, 223 , 50);
stroke(0 ,50);
rect( month2Count * (width/12) , height , width /12 , count * -0.5);
println(count);

fill(255);
textAlign(CENTER);
text(monthLables[month2Count], 20+ month2Count * (width/12) , 550);

month2Count++;


}


msql.query( "select month(ckoutDateTime), count(*) from transactions2007 where title like '%revolutionary%' "+
"group by month(ckoutDateTime) order by month(ckoutDateTime)" );

int month4Count = 0;

while(msql.next()) {

int amonth = msql.getInt(1);
int count = msql.getInt(2);

fill(255, 400, 300 , 50);
stroke(0 ,50);
rect( month4Count * (width/12) , height , width /12 , count * -0.5);
println(count);

fill(255);
textAlign(CENTER);
text(monthLables[month4Count], 20+ month4Count * (width/12) , 550);

month4Count++;

}
}
else
{
println( "Conection Failed!! ");
}
}

void draw()
{

}

athenallewellyn
Posts: 6
Joined: Tue Sep 13, 2011 11:08 am

Re: 10.24 Share some Code

Post by athenallewellyn » Mon Nov 21, 2011 1:40 pm

This code will find the number of titles containing the word "horses" , jan-dec 2006, and stack them according to number of titles. back ground is orange.

import de.bezier.data.sql.*;


MySQL msql;


void setup() {

size( 700, 700 ); // set the size of screen to 700,700

background(355, 104, 50); // set background color
smooth(); // smooths shapes/fonts
stroke(255, 204, 0); // color of the border of the shapes


String user = "mat259";
String pass = "V1sual1zat1on";

// name of the database to use
String database = "spl_years";

// connect to database of server tango
//
msql = new MySQL( this, "tango.mat.ucsb.edu", database, user, pass ); // connect to the database

if ( msql.connect() ) // if connection is successfull...
{
msql.query( "select month(ckoutDateTime), count(*) from transactions2006 where title like '%horses%' group by month(ckoutDateTime) order by month(ckoutDateTime)");
// execute this query

int row = 0; // a variable keeping the row number as we iterrate through rows

while (msql.next ()) // while result has more row, do:
{

int month1= msql.getInt(1); // get the value of first column (month in this case)

int count = msql.getInt(2); // get the value of the second column (count / number of books etc.)


rect( row*width/12, height/2 -count, width/12, count);
/*

rect draws a rectangle. the syntax is:
rect( x , y , width, height ) draws a rectangle with top-left corner at x,y

-here we want a rectangle for each month. its width is the width of screen devided by 12, since there are 12 results each for one month
-its height to be equal to number of books returned by query (since count is around 100-200 there is no need to scale it)
- its x to be (row* width /12): for first row 0*width/12 = 0, second 1*width/12 , third 2* width/12 etc.
- its y to be the middle of screen (height/2)
but since we want the bottom of rectangles to be aligned we subtract count from that (try removing -count and see what happens)


*/

text(month1, row*width/12 + 20, height/2 + 20);

/*
sytax: text( a_text , x, y) writes a_text at x,y

write the label for each column (month1).
*/

println("----"); // prints numbers on the consule (for debugging - no effect on visualization)
println( month1);
println(count);

row= row+1; // increase row by one each time the while block is executed
}

textAlign(CENTER); // make text center aligned
text("Number of books having 'horses' in the title by month in 2006", width/2, height/2 +100);

}
else
{
println( "Conection Failed!! ");
}
}

luc393
Posts: 6
Joined: Tue Sep 13, 2011 11:01 am

Re: 10.24 Share some Code

Post by luc393 » Sun Nov 27, 2011 7:59 pm

This code is a visualization of the number of times books with titles including "oil" and "water" are checked out per month. The bars and colors overlap to mimic oil and water and the size of the bar is determined by the *count, or number of times the book is checked out every month. The text labels the months of the year.

~Christine

import de.bezier.data.sql.*;

MySQL msql;

void setup()
{
size( 600, 600 );
background(255);

String user = "mat259";
String pass = "V1sual1zat1on";

/*String [] monthLables = new String[12];
monthLables[0] = "Jan";
monthLables[1] = "Feb";
monthLables[2] = "Mar";
monthLables[3] = "Apr";
monthLables[4] = "May";
monthLables[5] = "Jun";
monthLables[6] = "Jul";
monthLables[7] = "Aug";
monthLables[8] = "Sep";
monthLables[9] = "Oct";
monthLables[10] = "Nov";
monthLables[11] = "Dec";*/
// regular way of defining an array

String [] monthLables = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; // inline version of defining an array (same as above)

String database = "spl_years";

msql = new MySQL( this, "tango.mat.ucsb.edu", database, user, pass );

if ( msql.connect() )
{
msql.query( "select month(ckoutDateTime), count(*) from transactions2007 where title like '%oil%' "+
"group by month(ckoutDateTime) order by month(ckoutDateTime)" );

int monthCount = 0;

while(msql.next()) {

int amonth = msql.getInt(1);
int count = msql.getInt(2);

count = count/5;

println(count);

fill(255,220,20, 50);
stroke(0 ,500);
rect( monthCount * (width/12) , height/2 , width /12 , count * -0.5);

fill(0);
textAlign(CENTER);
text(monthLables[monthCount], 30+ monthCount * (width/12) , 500);

//println("mountCount "+monthCount);
monthCount++;

}



msql.query( "select month(ckoutDateTime), count(*) from transactions2007 where title like '%water%' "+
"group by month(ckoutDateTime) order by month(ckoutDateTime)" );

monthCount = 0;

while(msql.next()) {

int amonth = msql.getInt(1);
int count = msql.getInt(2);

count = count/25;

println(count);

fill(15,195,250, 50);
stroke(0 ,500);
rect( monthCount * (width/12) , height/2 , width /12 , count * -0.5);

fill(0);
textAlign(CENTER);

//println("mountCount "+monthCount);
monthCount++;

}



}
else
{
println( "Conection Failed!! ");
}
}

void draw()
{

}

Locked