Code
// The number of knitters who are a part of the Ravelry Community per state. I had to divide the radius by 100 because there are so many knitters!
int nPoints = 0;
int[] xPoints;
int[] yPoints;
float[] myData; // for your data
PImage USA;
//-------------------------------
void setup(){
size(640,400);
USA = loadImage("usa.png");
// The loadStrings() function loads in data from a text file.
// From this data file, it puts each line of text into a String.
// Then it groups together all of these Strings into a String array.
// Here, the data file called "states.txt" is loaded into linesOfData[].
String[] linesOfData = loadStrings("states.txt");
println("There are " + linesOfData.length + " lines of data.");
// Look carefully at the states.txt data file. You will see that,
// in the specific case of this file, each line (String) of data
// combines a state acronym, an X String, and a Y String. These are bundled
// together into a single String (per line) which makes things a little tricky.
// To access the data as actual numbers, we have to extract these numbers out of
// the line-String. This activity is called "parsing". See how this is done, below.
//
nPoints = linesOfData.length;
xPoints = new int[nPoints];
yPoints = new int[nPoints];
// For each line of data from the text file,
for (int i=0; i < nPoints; i++) {
// Fetch the i'th String;
String someLineString = linesOfData[i];
// Knowing that this String is delimited by tab characters (\t),
// we can split this line String into smaller String pieces;
String[] dataPiecesFromSomeLine = split(someLineString, "\t");
// Now we fetch each of the component Strings from this line.
// Each of these components stores one small piece of data (in a String);
String abbrev = dataPiecesFromSomeLine[0]; // the state abbreviation (not used here)
String xString = dataPiecesFromSomeLine[1]; // the 1'th String contains the X value
String yString = dataPiecesFromSomeLine[2]; // the 2'nd String contains the Y value
// Now we extract an int value from a String, using Integer.parseInt(String s).
// Note: A similar trick exists for floats: float f = Float.parseFloat(String s);
int x = Integer.parseInt(xString); // extract int from String
int y = Integer.parseInt(yString); // extract int from String
xPoints[i] = x;
yPoints[i] = y;
}
String[] linesOfData2 = loadStrings("knitters.txt");
myData = new float[nPoints];
for (int i=0; i < nPoints; i++){
String someLineString2 = linesOfData2[i];
String[] dataPiecesFromSomeLine2 = split(someLineString2, "\t");
String data = dataPiecesFromSomeLine2[0];
float d = Float.parseFloat(data);
myData[i] = d;
}
}
//=====================================
void draw(){
background(255);
image(USA,0,0);
drawMyData();
}
//=====================================
void drawMyData(){
smooth();
fill(0, 64);
stroke(0, 128);
for (int i=0; i < nPoints; i++){
int x = xPoints[i];
int y = yPoints[i];
float R = myData[i]/100;
ellipse(x,y, R,R);
stroke (255,0,0);
strokeWeight(3);
point (x,y);
}
}
0604 - Data Visualization: Loading + displaying numeric data
Statement:
// In this exercise, you will create a data visualization about the United States.
//
// (1) Get the program working, below. Check how Placebo did it.
// Your sketch will need to have a "data" folder,
// and inside this folder you will need to put the following 2 files:
// http://artscool.cfa.cmu.edu/~levin/courses/dmc/intro08/downloads/states.txt
// http://artscool.cfa.cmu.edu/~levin/courses/dmc/intro08/downloads/usa.png
//
// (2) Go to http://www.statemaster.com/ and find some data about the USA which interests you.
// This site has a variety of useful information, such as:
// -- number of Starbucks stores per capita per state:
// http://www.statemaster.com/graph/lif_sta_sto_percap-lifestyle-starbucks-stores-per-capita
// -- number of Iraqi-war military casualties per state:
// http://www.statemaster.com/graph/mil_ira_war_cas-military-iraqi-war-casualties
//
// (3) You now need to load your own data, so that ultimately it is in your myData[] array.
// Your data loading will replace the small block of code labeled "BOGUS", in setup() below.
// -- You can either add your data to the "states.txt" file, OR make a new data file of your own.
// -- Note that the states are ordered alphabetically by their full name (NOT their abbreviation!).
// -- If you add your data to "states.txt", separate it from the other data with tab characters.
// -- No points for hard-coding your data into the code itself; you must load data from a file.
//
// (4) Render your data using the drawMyData() function.
// By default, this function maps numeric quantities to the radii of circles.
// You may need to add a scaling factor to your data here, if your circles are too big or small.
// You can also render your data differently, if you wish; that's optional.
// Don't forget to let us know which data set you used.
// Put your data name in a comment at the top of your code.
// MY DATA IS... (???)
int nPoints = 0;
int[] xPoints;
int[] yPoints;
float[] myData; // for your data
PImage USA;
//-------------------------------
void setup(){
size(640,400);
USA = loadImage("usa.png");
// The loadStrings() function loads in data from a text file.
// From this data file, it puts each line of text into a String.
// Then it groups together all of these Strings into a String array.
// Here, the data file called "states.txt" is loaded into linesOfData[].
String[] linesOfData = loadStrings("states.txt");
println("There are " + linesOfData.length + " lines of data.");
// Look carefully at the states.txt data file. You will see that,
// in the specific case of this file, each line (String) of data
// combines a state acronym, an X String, and a Y String. These are bundled
// together into a single String (per line) which makes things a little tricky.
// To access the data as actual numbers, we have to extract these numbers out of
// the line-String. This activity is called "parsing". See how this is done, below.
//
nPoints = linesOfData.length;
xPoints = new int[nPoints];
yPoints = new int[nPoints];
// For each line of data from the text file,
for (int i=0; i < nPoints; i++) {
// Fetch the i'th String;
String someLineString = linesOfData[i];
// Knowing that this String is delimited by tab characters (\t),
// we can split this line String into smaller String pieces;
String[] dataPiecesFromSomeLine = split(someLineString, "\t");
// Now we fetch each of the component Strings from this line.
// Each of these components stores one small piece of data (in a String);
String abbrev = dataPiecesFromSomeLine[0]; // the state abbreviation (not used here)
String xString = dataPiecesFromSomeLine[1]; // the 1'th String contains the X value
String yString = dataPiecesFromSomeLine[2]; // the 2'nd String contains the Y value
// Now we extract an int value from a String, using Integer.parseInt(String s).
// Note: A similar trick exists for floats: float f = Float.parseFloat(String s);
int x = Integer.parseInt(xString); // extract int from String
int y = Integer.parseInt(yString); // extract int from String
xPoints[i] = x;
yPoints[i] = y;
}
//-----------------------------------
// THIS CODE BIT RIGHT HERE IS BOGUS!
// YOU GOTTA REPLACE IT WITH REAL DATA
// THAT YOU LOAD FROM AN EXTERNAL FILE.
myData = new float[50];
for (int i=0; i < 50; i++){
myData[i] = 40.0;
}
//-----------------------------------
}
//=====================================
void draw(){
background(255);
image(USA,0,0);
drawMyData();
}
//=====================================
void drawMyData(){
smooth();
fill(0, 64);
stroke(0, 128);
for (int i=0; i < nPoints; i++){
int x = xPoints[i];
int y = yPoints[i];
float R = myData[i]; // radius based on data value
// You may need to apply a scaling factor to R,
// so that your data look goods on the map.
// Consider multiplying or dividing R by some constant.
ellipse(x,y, R,R);
}
}
hide statement