A-Z final

programming — tom on May 13, 2008 at 12:04 pm

For my final, I worked with trying to refine my idea of visualizing campaign speech texts between Obama and Clinton. While this is somehow still a relevant idea, I came to some conclusions about datavis. It’s hard to just take a mass of data and just try to “visualize” it without an agenda about what you’re doing. It’s hard to rectify my idea of having a “painterly” kind of effect with really including a lot of information about the material.

in any case, having a third dimension to scroll through, while keeping the kind of effects i want may be a way of keeping the visualization painterly while not getting it too busy with pop-up tooltips and things like that.

here’s the code:

package bayesian.visualization;

import java.lang.reflect.Array;
import java.nio.*;
import java.io.*;
import java.nio.channels.*;
import java.util.*;
import java.lang.Object.*;
import a2z.*;
import java.awt.*;

import java.util.regex.*;
import concordance.treemap.Word;
import processing.core.PApplet;
import processing.core.PFont;

public class Visualization extends PApplet
{
	private static final long serialVersionUID = 1L;
	PFont fontA = loadFont("AmericanTypewriter-100.vlw");
	PFont fontB = loadFont("AmericanTypewriter-Bold-48.vlw");

	public void setup() {
		size(1440,900,P3D);
		colorMode(RGB, 255, 255, 255, 255);
	}

	public void draw() {
		background(0,0,0);

		try {
		A2ZFileReader data = new A2ZFileReader("data.txt");
		String datacontent = data.getContent();

		String wordtokens = "[.\\s]";		//period here makes it work, relate the number to the decimal?
		//breaks into "word" "0" "value after decimal sign"

		String[] words = datacontent.split(wordtokens);
		//String[] numbers = datacontent.split(numtokens);

		// me trying to see what actually is in the array
		/*System.out.println("+++"+words[0]+"+++");
		System.out.println("+++"+words[1]+"+++");
		System.out.println("+++"+words[2]+"+++");
		System.out.println("+++"+words[3]+"+++");*/

		//instantiate a new array of arrays with more space than we'll need (i hope)
		int[][] location = new int[2][4000]; // 2 rows for x and y, 4000 words allowed

		int counter = 0;
		for(int i = 0; i < 4000; i=i+3){ //note: there is a waste 0 in the array, need to increment by 3
			//String current = lines[0];

			/*System.out.println(counter);					//debug
			 *System.out.println("word: "+words[i]);		//debug
			 *System.out.println("number: "+words[i+2]);	//debug
			 */

			/* by having the for loop iterate with i+2 each time, I can use a single array to hold all of
			 * the interesting stuff
			 */

			// pull out the number associated with the words
			// the number is the third of the word/number pairs, hence i+2 as the array value	

			String shortnumber = words[i+2].substring(0, 2);
			int temp = Integer.parseInt(shortnumber);
				System.out.println("temp: "+temp);

			String temp2  = "0."+temp;
			float bayesnumber = Float.valueOf(temp2).floatValue();
				System.out.println("bayesnumber: "+bayesnumber);

			/* possible to round the numbers to just two digits? maybe even just truncate them?
			 * then it's easy to take the 2 digit values and divide by 100 to get a nearly on
			 * decimal that can be used by the rest of the code that I've written
			 */

			// Set up a size for text, proportional to interestingness
			float value = Math.abs(0.5f - bayesnumber);
			System.out.println(value);

			System.out.println("value: "+value);

			//testing
			if (value < .03){
				value = 0;
			}

			//float logvalue = log(value);
			//int size = (int)((value) * 150);
			//int size = (int)(30*(1-value));
			int size = 12;

			//give it a minimum, for display purposes
			if (size < 12){
				size = 12;
			}

			// Set up a minimum transparency for text to be somewhat visible
			textFont(fontA, size);
			int alpha = (int) (value*255);
			if (alpha < 32){
				alpha = 32;
			}
			// Set color with transparency, fiddle with presentation

			/* To do: need to get the center of each word based on word length
			 * and then center each word's display
			 */

			noStroke();

			if (bayesnumber > 0.5){
				// Right side is red
				int c1 = color(255,(255-(2*alpha)),(255-(2*alpha)),(255-alpha));
				fill(c1);

				//scale location for size of screen
				int xpos = ((int)(width*bayesnumber)-65);
				int ypos = (int)(.31*counter);

				//draw words themselves
				int textwidth = (int) textWidth(words[i]);
				int modwidth = 2*(textwidth+30);
				text(words[i], xpos-textwidth, ypos);

				//add location of the word to the location 2D array
				location[0][counter] = xpos;
				location[1][counter] = ypos;

				//on mouseover of quad, draw box
				if ((xpos-textwidth < mouseX) && (mouseX < xpos) && (ypos < mouseY) &&( mouseY < (ypos+size))){

					//get the information used in the box
					String ranking = "Bayesian Score: "+bayesnumber;
					int size2 = 12;
					textFont(fontA, size2);
					int boxwidth = (int) textWidth(ranking);

					if (boxwidth > modwidth){
						fill(0,0,0);
						stroke(255,255,255,64);
						quad(xpos,ypos+15,xpos-boxwidth-30,ypos+15,xpos-boxwidth-30,ypos-50,xpos,ypos-50);

						//draw the word in a legible size
						fill(c1);
						size = 30;
						textFont(fontA,size);
						text(words[i],xpos-15-boxwidth,ypos-15);

						//draw the bayesian ranking for the word
						fill(c1);
						size2 = 12;
						textFont(fontA, size2);
						text(ranking,xpos-boxwidth-15,ypos+10);
					}
					else{
						fill(0,0,0);
						stroke(255,255,255,64);
						quad(xpos,ypos+15,xpos-modwidth,ypos+15,xpos-modwidth,ypos-50,xpos,ypos-50);

						//draw the word in a legible size
						fill(c1);
						size = 30;
						textFont(fontA,size);
						text(words[i],xpos+15-modwidth,ypos-15);

						//draw the bayesian ranking for the word
						fill(c1);
						size2 = 12;
						textFont(fontA, size2);
						text(ranking,xpos-modwidth+15,ypos+10);
					}
				}
			}
			if (bayesnumber < 0.5){
				// Left side is blue
				int c1 = color((255-(2*alpha)),(255-(2*alpha)),255,(255-alpha));
				fill(c1);

				//scale location for size of screen
				int xpos = ((int)(width*bayesnumber)-65);
				int ypos = (int)(.31*counter);

				//draw words themselves
				int textwidth = (int) textWidth(words[i]);
				int modwidth = 2*(textwidth+30);
				text(words[i], xpos, ypos);

				//add location of the word to the location 2D array
				location[0][counter] = xpos;
				location[1][counter] = ypos;

				//on mouseover of quad, draw information box, fill it with info
				if ((xpos < mouseX) && (mouseX < xpos+textwidth) && (ypos < mouseY) &&( mouseY < (ypos+size))){

					//get the information used in the box
					String ranking = "Bayesian Score: "+bayesnumber;
					int size2 = 12;
					textFont(fontA, size2);
					int boxwidth = (int) textWidth(ranking);

					if (boxwidth > modwidth){
						//draw the information box to fit boxwidth
						fill(0);
						stroke(255,255,255,64);
						quad(xpos,ypos+15,xpos+boxwidth+30,ypos+15,xpos+boxwidth+30,ypos-50,xpos,ypos-50);
					}
					else{
						//draw the information box to fit modwidth
						fill(0);
						stroke(255,255,255,64);
						quad(xpos,ypos+15,xpos+modwidth,ypos+15,xpos+modwidth,ypos-50,xpos,ypos-50);
					}

					//draw the word in a legible size
					fill(c1);
					size = 30;
					textFont(fontA,size);
					text(words[i],xpos+15,ypos-15);

					//draw the bayesian ranking for the word
					fill(c1);
					size2 = 12;
					textFont(fontA, size2);
					text(ranking,xpos+15,ypos+10);
				}
			}
			if (bayesnumber == .05){
				int c1 = color(255,255,255,(255-alpha));
				fill(c1);

				//scale location for size of screen
				int xpos = ((int)(width*bayesnumber)-130);
				int ypos = (int)(.31*counter);

				//draw words themselves
				text(words[i], xpos, ypos);

				fill(255,255,255,32);
				quad(xpos,ypos,xpos+30,ypos,xpos+30,ypos-10,xpos,ypos-10);

				//add location of the word to the location 2D array
				location[0][counter] = xpos;
				location[1][counter] = ypos;

				//on mouseover of quad, draw box
				if ((xpos < mouseX) && (mouseX > xpos+30) && (ypos < mouseY) &&( mouseY > ypos-20)){
					fill(0);
					stroke(255,255,255,64);
					quad(xpos,ypos,xpos+150,ypos,xpos+150,ypos-50,xpos,ypos-50);

					fill(c1);
					size = 30;
					textFont(fontA,size);
					text(words[i],xpos+15,ypos-15);

					noLoop();
				}
				else{
					loop();
				}
			}

			counter = counter + 2;
		}
		}
		catch (IOException e) {
			System.out.println("File I/O Error");
			e.printStackTrace();
		}
	}
}

1 Comment

  1. [...] – bookmarked by 4 members originally found by vdub144 on 2008-09-10 AZ final http://thomasjenkins.net/blog/?p=13 – bookmarked by 3 members originally found by miyuu on [...]

    Pingback by Bookmarks about Datavis — September 29, 2008 @ 7:30 pm

RSS feed for comments on this post. TrackBack URI

Sorry, the comment form is closed at this time.