Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Time Series

By Dr. Shaheera Rashwan


Mapping-Taking data from the user(interact)
• The following code reads from the names.tsv file and asks the
user to indicate a location for each in turn, by clicking the
mouse where the user wants the data to be placed.
• Start this example as a separate sketch. It requires a map.png
file, a names.tsv file, and the Table.pde file used throughout
this chapter. The map image and names file can be replaced
with data of your choice, and this code produces a
locations.tsv file that can be added to the data folder of the
new sketch:
2
PImage mapImage;
void mousePressed( ) {
Table nameTable; if (currentRow != -1) {
int currentRow = -1; String abbrev = nameTable.getRowName(currentRow);
PrintWriter writer;
writer.println(abbrev + "\t" + mouseX + "\t" + mouseY);
}
void setup( ) { currentRow++;
size(640, 400); if (currentRow == nameTable.getRowCount( )) {
// Close the file and finish.
mapImage = loadImage("map.png"); writer.flush( );
nameTable = new Table("names.tsv"); writer.close( );
writer = createWriter("locations.tsv"); exit( );
} else {
cursor(CROSS); // make easier to pinpoint a // Ask for the next coordinate.
location String name = nameTable.getString(currentRow, 1);
println("Click the mouse to begin."); println("Choose location for " + name + ".");
}
}
}
void draw( ) {
image(mapImage, 0, 0);
}
3
Time Series-Milk, Tea, and Coffee (Acquire and Parse)

• Time Series is a type of data set that describes how some measurable feature (for
instance, population, snowfall, or items sold) has changed over a period of time.
• Dataset the U.S. Department of Agriculture (USDA) for statistics on beverage
consumption. downloaded from
http://www.ers.usda.gov/Data/FoodConsumption/FoodAvailQueriable.aspx
• Or simply a processed version from
http://benfry.com/writing/series/milk-tea-coffee.tsv
• To read this file, use this modified version of the Table class from the previous
Lecture:
• http://benfry.com/writing/series/FloatTable.pde
• The modified version handles data stored as float values
4
Cleaning the Table (Filter and Mine)
• It’s necessary to determine the minimum and maximum of each of the columns in
the pre-filtered data set.
• Sometimes, it’s also useful to calculate the minimum value, but setting the
minimum to zero provides a more accurate comparison between the three data sets.
FloatTable data;
float dataMin, dataMax;
void setup( ) {
data = new FloatTable("milk-tea-coffee.tsv");
dataMin = 0;
dataMax = data.getTableMax( );
}

5
• Each row name specifies a year, which will be used later to draw labels on the plot.The
getRowNames( ) method inside FloatTable returns a String array that can be converted with
the int( ) casting Function
FloatTable data;
float dataMin, dataMax;
int yearMin, yearMax;
int[] years;
void setup( ) {
data = new FloatTable("milk-tea-coffee.tsv");
years = int(data.getRowNames( ));
yearMin = years[0];
yearMax = years[years.length - 1];
dataMin = 0;
dataMax = data.getTableMax( );
}
6
A Simple Plot (Represent and Refine)
FloatTable data; • dataMin = 0;
• The plotX1, plotY1, dataMax =
float dataMin, dataMax;
plotX2, and plotY2 data.getTableMax( );
variables define the float plotX1, plotY1; // Corners of the plotted time
series
corners of the plot. To float plotX2, plotY2;
plotX1 = 50;
provide a nice margin int yearMin, yearMax; plotX2 = width - plotX1;
on the left, set plotX1 to int[] years; plotY1 = 60;
50, and then set the plotY2 = height - plotY1;
void setup( ) { smooth( );
plotX2 coordinate by
subtracting this value size(720, 405);
from width. This keeps data = new FloatTable("milk-tea-
the two sides even, and coffee.tsv");
requires only a single years = int(data.getRowNames( ));
change to adjust the yearMin = years[0];
position of both. The yearMax = years[years.length - 1];
same for plotY1 and
plotY2
7
• The rect( ) function normally takes the form
rect(x, y, width, height), but
rectMode(CORNERS) changes the parameters to
rect(left, top, right, bottom), which is useful
because our plot’s shape is defined by the
corners.
void draw( ) { // Draw the data as a series of points.
background(224); void drawDataPoints(int col) {
int rowCount = data.getRowCount( );
// Show the plot area as a white box. for (int row = 0; row < rowCount; row++) {
fill(255); if (data.isValid(row, col)) {
float value = data.getFloat(row, col);
rectMode(CORNERS); float x = map(years[row], yearMin, yearMax, plotX1, plotX2);
noStroke( ); float y = map(value, dataMin, dataMax, plotY2, plotY1);
rect(plotX1, plotY1, plotX2, plotY2);
point(x, y);
}
strokeWeight(5); }
// Draw the data for the first column. }
stroke(#5679C1);
drawDataPoints(0);
}
8
Draw for Milk Consumption

9
Labeling the Current Data Set (Refine and Interact)
data = new FloatTable("milk-tea-coffee.tsv");
• Missing from the previous code is an void draw( ) {
columnCount = data.getColumnCount( ); background(224);
indicator of the currently visible
column of data (whether milk, tea, or years = int(data.getRowNames( )); // Show the plot area as a white box.
coffee) and a means to swap between yearMin = years[0];
fill(255);
each of the three FloatTable data; rectMode(CORNERS);
yearMax = years[years.length - 1]; noStroke( );
float dataMin, dataMax; dataMin = 0; rect(plotX1, plotY1, plotX2, plotY2);
float plotX1, plotY1; // Draw the title of the current plot.
dataMax = data.getTableMax( );
fill(0);
float plotX2, plotY2; // Corners of the plotted time series textSize(20);
plotX1 = 50; String title =
int currentColumn = 0;
data.getColumnName(currentColum
plotX2 = width - plotX1;
int columnCount; n);
plotY1 = 60; text(title, plotX1, plotY1 - 10);
int yearMin, yearMax;
plotY2 = height - plotY1; stroke(#5679C1);
int[] years; strokeWeight(5);
plotFont = createFont("SansSerif", 20);
drawDataPoints(currentColumn);
PFont plotFont; textFont(plotFont); }
void setup( ) { smooth( );
size(720, 405); }
10
Time series with data set labeled

11
Swapping between columns of data
void keyPressed( ) { • This method will rotate through
if (key == '[') {
the columns as the user presses
the [ and ] (bracket)keys.
currentColumn--;
if (currentColumn < 0) {
• When the number gets too big
or too small, it wraps around to
currentColumn = columnCount - 1;
the beginning or end of the list.
}
• Because columnCount is 3, the
} else if (key == ']') {
possible currentColumn values
currentColumn++; are 0, 1, and 2.
if (currentColumn == columnCount) {
• So, when currentColumn
currentColumn = 0;
reaches a value less than zero,
} it wraps around to 2
} (columnCount - 1)
}
12
Drawing Axis Labels (Refine)

• Creating the year axis is straightforward. The data ranges from 1910 to 2004, so an interval of 10 years means marking 10
individual years: 1910, 1920, 1930, and so on, up to 2000.
int yearInterval = 10;
void drawYearLabels( ) {
fill(0);
textSize(10);
textAlign(CENTER, TOP);
for (int row = 0; row < rowCount; row++) {
if (years[row] % yearInterval == 0) {
float x = map(years[row], yearMin, yearMax, plotX1, plotX2);
text(years[row], x, plotY2 + 10);
}
}
}
13
Time series with labeled x-axis

14
• The vertical axis can be handled the void drawVolumeLabels( ) {
same way as the horizontal, but it is a fill(0);
bit trickier. textSize(10);
• A quick println(dataMax) added to textAlign(RIGHT, CENTER);
setup( ) tells us that the maximum for (float v = dataMin; v < dataMax; v +=
value is 46.4. Intervals of 10 will again volumeInterval) {
suffice, this time producing only 5 float y = map(v, dataMin, dataMax, plotY2,
divisions plotY1);
text(floor(v), plotX1 - 10, y);
• int volumeInterval = 10; }
• Use ceil( ), which rounds a float up to }
the next int value (in this case, 5), the floor( ) function removes decimals from
called the ceiling of a float the
number value because there’s no need to
• dataMax = ceil(data.getTableMax( ) /
write 10.0, 20.0, 30.0, etc. when 10, 20,
volumeInterval) * volumeInterval; and 30 will suffice.

15
• With the vertical centering, the label drawn at 0 is visually a little too close to the year
markers below. The same goes for the top value, which gets close to the title. To leave
these out, alter the first value drawn by adding a volumeInterval to dataMin, and end the
loop at v < dataMax instead of v <= dataMax so that the 50 won’t be drawn:
void drawVolumeLabels( ) {
fill(0);
textSize(10);
textAlign(RIGHT, CENTER);
float dataFirst = dataMin + volumeInterval;
for (float v = dataFirst; v < dataMax; v += volumeInterval) {
float y = map(v, dataMin, dataMax, plotY2, plotY1);
text(floor(v), plotX1 - 10, y);
}
}
16
• In other cases, it might not be appropriate to remove upper and lower values
void drawVolumeLabels( ) {
fill(0);
textSize(10);
for (float v = dataMin; v <= dataMax; v += volumeInterval) {
float y = map(v, dataMin, dataMax, plotY2, plotY1);
if (v == dataMin) {
textAlign(RIGHT); // Align by the bottom
} else if (v == dataMax) {
textAlign(RIGHT, TOP); // Align by the top
} else {
textAlign(RIGHT, CENTER); // Center vertically
}
text(floor(v), plotX1 - 10, y);
}
}
17
int volumeIntervalMinor = 5; // Add this above setup( )
void drawVolumeLabels( ) {
• Instead of gridlines, small tick fill(0);
textSize(10);
marks near the labels on the stroke(128);
vertical axis can be produced strokeWeight(1);
with the same technique, by for (float v = dataMin; v <= dataMax; v +=
volumeIntervalMinor) {
drawing a short line just if (v % volumeIntervalMinor == 0) { // If a tick mark
outside the edge of the plot. float y = map(v, dataMin, dataMax, plotY2, plotY1);
if (v % volumeInterval == 0) { // If a major tick mark
• Minor gridlines or tick marks if (v == dataMin) {
can be drawn by including a textAlign(RIGHT); // Align by the bottom
} else if (v == dataMax) {
variable for a second interval textAlign(RIGHT, TOP); // Align by the top
that’s a multiple of the first } else {
and incrementing by that textAlign(RIGHT, CENTER); // Center vertically
}
interval in the loop. text(floor(v), plotX1 - 10, y);
• The following modification to line(plotX1 - 4, y, plotX1, y); // Draw major tick
} else {
drawVolumeLabels( ) adds line(plotX1 - 2, y, plotX1, y); // Draw minor tick
major and minor tick marks to }
the volume axis: }
}
} 18
Tick marks on the vertical axis

19
References
• Book of the course-Chapter 4

20
End of Lecture

21

You might also like