09.04.2016 Views

www.ebook777.com

Make_Getting_Started_with_Processing_Second_Edition

Make_Getting_Started_with_Processing_Second_Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

homeRuns is used three times with the code homeRuns.length, to<br />

count the number of for loop iterations.<br />

homeRuns is used first in setup() to define how many times to get<br />

an integer from the table data. Second, it is used to place a vertical<br />

mark on the graph for each data item in the array. Third, it<br />

is used to read each element of the array one by one and to stop<br />

reading from the array at the end. After the data is loaded inside<br />

setup() and read into the array, the rest of this program applies<br />

what was learned in Chapter 11.<br />

This example is the visualization of a simplified version of Boston<br />

Red Sox player David Ortiz’s batting statistics from 1997 to<br />

2014 drawn from a table:<br />

int[] homeRuns;<br />

void setup() {<br />

size(480, 120);<br />

Table stats = loadTable("ortiz.csv");<br />

int rowCount = stats.getRowCount();<br />

homeRuns = new int[rowCount];<br />

for (int i = 0; i < homeRuns.length; i++) {<br />

homeRuns[i] = stats.getInt(i, 1);<br />

}<br />

}<br />

void draw() {<br />

background(204);<br />

// Draw background grid for data<br />

stroke(255);<br />

line(20, 100, 20, 20);<br />

line(20, 100, 460, 100);<br />

for (int i = 0; i < homeRuns.length; i++) {<br />

float x = map(i, 0, homeRuns.length-1, 20, 460);<br />

line(x, 20, x, 100);<br />

}<br />

// Draw lines based on home run data<br />

Data 169

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!