mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
add "visualizing data" examples
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
class Integrator {
|
||||
|
||||
final float DAMPING = 0.5f;
|
||||
final float ATTRACTION = 0.2f;
|
||||
|
||||
float value;
|
||||
float vel;
|
||||
float accel;
|
||||
float force;
|
||||
float mass = 1;
|
||||
|
||||
float damping = DAMPING;
|
||||
float attraction = ATTRACTION;
|
||||
boolean targeting;
|
||||
float target;
|
||||
|
||||
|
||||
Integrator() { }
|
||||
|
||||
|
||||
Integrator(float value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
||||
Integrator(float value, float damping, float attraction) {
|
||||
this.value = value;
|
||||
this.damping = damping;
|
||||
this.attraction = attraction;
|
||||
}
|
||||
|
||||
|
||||
void set(float v) {
|
||||
value = v;
|
||||
}
|
||||
|
||||
|
||||
void update() {
|
||||
if (targeting) {
|
||||
force += attraction * (target - value);
|
||||
}
|
||||
|
||||
accel = force / mass;
|
||||
vel = (vel + accel) * damping;
|
||||
value += vel;
|
||||
|
||||
force = 0;
|
||||
}
|
||||
|
||||
|
||||
void target(float t) {
|
||||
targeting = true;
|
||||
target = t;
|
||||
}
|
||||
|
||||
|
||||
void noTarget() {
|
||||
targeting = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
class Table {
|
||||
int rowCount;
|
||||
String[][] data;
|
||||
|
||||
|
||||
Table(String filename) {
|
||||
String[] rows = loadStrings(filename);
|
||||
data = new String[rows.length][];
|
||||
|
||||
for (int i = 0; i < rows.length; i++) {
|
||||
if (trim(rows[i]).length() == 0) {
|
||||
continue; // skip empty rows
|
||||
}
|
||||
if (rows[i].startsWith("#")) {
|
||||
continue; // skip comment lines
|
||||
}
|
||||
|
||||
// split the row on the tabs
|
||||
String[] pieces = split(rows[i], TAB);
|
||||
// copy to the table array
|
||||
data[rowCount] = pieces;
|
||||
rowCount++;
|
||||
|
||||
// this could be done in one fell swoop via:
|
||||
//data[rowCount++] = split(rows[i], TAB);
|
||||
}
|
||||
// resize the 'data' array as necessary
|
||||
data = (String[][]) subset(data, 0, rowCount);
|
||||
}
|
||||
|
||||
|
||||
int getRowCount() {
|
||||
return rowCount;
|
||||
}
|
||||
|
||||
|
||||
// find a row by its name, returns -1 if no row found
|
||||
int getRowIndex(String name) {
|
||||
for (int i = 0; i < rowCount; i++) {
|
||||
if (data[i][0].equals(name)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
println("No row named '" + name + "' was found");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
String getRowName(int row) {
|
||||
return getString(row, 0);
|
||||
}
|
||||
|
||||
|
||||
String getString(int rowIndex, int column) {
|
||||
return data[rowIndex][column];
|
||||
}
|
||||
|
||||
|
||||
String getString(String rowName, int column) {
|
||||
return getString(getRowIndex(rowName), column);
|
||||
}
|
||||
|
||||
|
||||
int getInt(String rowName, int column) {
|
||||
return parseInt(getString(rowName, column));
|
||||
}
|
||||
|
||||
|
||||
int getInt(int rowIndex, int column) {
|
||||
return parseInt(getString(rowIndex, column));
|
||||
}
|
||||
|
||||
|
||||
float getFloat(String rowName, int column) {
|
||||
return parseFloat(getString(rowName, column));
|
||||
}
|
||||
|
||||
|
||||
float getFloat(int rowIndex, int column) {
|
||||
return parseFloat(getString(rowIndex, column));
|
||||
}
|
||||
|
||||
|
||||
void setRowName(int row, String what) {
|
||||
data[row][0] = what;
|
||||
}
|
||||
|
||||
|
||||
void setString(int rowIndex, int column, String what) {
|
||||
data[rowIndex][column] = what;
|
||||
}
|
||||
|
||||
|
||||
void setString(String rowName, int column, String what) {
|
||||
int rowIndex = getRowIndex(rowName);
|
||||
data[rowIndex][column] = what;
|
||||
}
|
||||
|
||||
|
||||
void setInt(int rowIndex, int column, int what) {
|
||||
data[rowIndex][column] = str(what);
|
||||
}
|
||||
|
||||
|
||||
void setInt(String rowName, int column, int what) {
|
||||
int rowIndex = getRowIndex(rowName);
|
||||
data[rowIndex][column] = str(what);
|
||||
}
|
||||
|
||||
|
||||
void setFloat(int rowIndex, int column, float what) {
|
||||
data[rowIndex][column] = str(what);
|
||||
}
|
||||
|
||||
|
||||
void setFloat(String rowName, int column, float what) {
|
||||
int rowIndex = getRowIndex(rowName);
|
||||
data[rowIndex][column] = str(what);
|
||||
}
|
||||
}
|
||||
BIN
Binary file not shown.
@@ -0,0 +1,50 @@
|
||||
AL 439 270
|
||||
AK 94 325
|
||||
AZ 148 241
|
||||
AR 368 247
|
||||
CA 56 176
|
||||
CO 220 183
|
||||
CT 576 120
|
||||
DE 556 166
|
||||
FL 510 331
|
||||
GA 478 267
|
||||
HI 232 380
|
||||
ID 143 101
|
||||
IL 405 168
|
||||
IN 437 165
|
||||
IA 357 147
|
||||
KS 302 194
|
||||
KY 453 203
|
||||
LA 371 302
|
||||
ME 595 59
|
||||
MD 538 162
|
||||
MA 581 108
|
||||
MI 446 120
|
||||
MN 339 86
|
||||
MS 406 274
|
||||
MO 365 197
|
||||
MT 194 61
|
||||
NE 286 151
|
||||
NV 102 157
|
||||
NH 580 89
|
||||
NJ 561 143
|
||||
NM 208 245
|
||||
NY 541 107
|
||||
NC 519 221
|
||||
ND 283 65
|
||||
OH 472 160
|
||||
OK 309 239
|
||||
OR 74 86
|
||||
PA 523 144
|
||||
RI 589 117
|
||||
SC 506 251
|
||||
SD 286 109
|
||||
TN 441 229
|
||||
TX 291 299
|
||||
UT 154 171
|
||||
VT 567 86
|
||||
VA 529 189
|
||||
WA 92 38
|
||||
WV 496 178
|
||||
WI 392 103
|
||||
WY 207 125
|
||||
|
@@ -0,0 +1,50 @@
|
||||
AL Alabama
|
||||
AK Alaska
|
||||
AZ Arizona
|
||||
AR Arkansas
|
||||
CA California
|
||||
CO Colorado
|
||||
CT Connecticut
|
||||
DE Delaware
|
||||
FL Florida
|
||||
GA Georgia
|
||||
HI Hawaii
|
||||
ID Idaho
|
||||
IL Illinois
|
||||
IN Indiana
|
||||
IA Iowa
|
||||
KS Kansas
|
||||
KY Kentucky
|
||||
LA Louisiana
|
||||
ME Maine
|
||||
MD Maryland
|
||||
MA Massachusetts
|
||||
MI Michigan
|
||||
MN Minnesota
|
||||
MS Mississippi
|
||||
MO Missouri
|
||||
MT Montana
|
||||
NE Nebraska
|
||||
NV Nevada
|
||||
NH New Hampshire
|
||||
NJ New Jersey
|
||||
NM New Mexico
|
||||
NY New York
|
||||
NC North Carolina
|
||||
ND North Dakota
|
||||
OH Ohio
|
||||
OK Oklahoma
|
||||
OR Oregon
|
||||
PA Pennsylvania
|
||||
RI Rhode Island
|
||||
SC South Carolina
|
||||
SD South Dakota
|
||||
TN Tennessee
|
||||
TX Texas
|
||||
UT Utah
|
||||
VT Vermont
|
||||
VA Virginia
|
||||
WA Washington
|
||||
WV West Virginia
|
||||
WI Wisconsin
|
||||
WY Wyoming
|
||||
|
@@ -0,0 +1,50 @@
|
||||
AL 0.1
|
||||
AK -5.3
|
||||
AZ 3
|
||||
AR 7
|
||||
CA 11
|
||||
CO 1.5
|
||||
CT -6.7
|
||||
DE -4
|
||||
FL 9
|
||||
GA 2
|
||||
HI -3.3
|
||||
ID 6.6
|
||||
IL 7.2
|
||||
IN 7.1
|
||||
IA 6.9
|
||||
KS 6
|
||||
KY 1.8
|
||||
LA 7.5
|
||||
ME -4
|
||||
MD 0.1
|
||||
MA -6
|
||||
MI 1.7
|
||||
MN -2
|
||||
MS -4.4
|
||||
MO -2
|
||||
MT 1.0
|
||||
NE 1.2
|
||||
NV 1.6
|
||||
NH 0.5
|
||||
NJ 0.2
|
||||
NM 8.8
|
||||
NY 1.4
|
||||
NC 9.7
|
||||
ND 5.4
|
||||
OH 3.2
|
||||
OK 6
|
||||
OR -4
|
||||
PA -7
|
||||
RI -2
|
||||
SC 1
|
||||
SD 6
|
||||
TN 5
|
||||
TX -3.4
|
||||
UT 2.3
|
||||
VT 4.8
|
||||
VA 3
|
||||
WA 2.2
|
||||
WV 5.4
|
||||
WI 3.1
|
||||
WY -6
|
||||
|
@@ -0,0 +1,107 @@
|
||||
PImage mapImage;
|
||||
Table locationTable;
|
||||
Table nameTable;
|
||||
int rowCount;
|
||||
|
||||
Table dataTable;
|
||||
float dataMin = -10;
|
||||
float dataMax = 10;
|
||||
|
||||
Integrator[] interpolators;
|
||||
|
||||
|
||||
void setup() {
|
||||
size(640, 400);
|
||||
mapImage = loadImage("map.png");
|
||||
locationTable = new Table("locations.tsv");
|
||||
nameTable = new Table("names.tsv");
|
||||
rowCount = locationTable.getRowCount();
|
||||
|
||||
dataTable = new Table("random.tsv");
|
||||
interpolators = new Integrator[rowCount];
|
||||
for (int row = 0; row < rowCount; row++) {
|
||||
float initialValue = dataTable.getFloat(row, 1);
|
||||
interpolators[row] = new Integrator(initialValue, 0.9, 0.1);
|
||||
}
|
||||
|
||||
PFont font = loadFont("Univers-Bold-12.vlw");
|
||||
textFont(font);
|
||||
|
||||
smooth();
|
||||
noStroke();
|
||||
//frameRate(30);
|
||||
}
|
||||
|
||||
float closestDist;
|
||||
String closestText;
|
||||
float closestTextX;
|
||||
float closestTextY;
|
||||
|
||||
|
||||
void draw() {
|
||||
background(255);
|
||||
image(mapImage, 0, 0);
|
||||
|
||||
for (int row = 0; row < rowCount; row++) {
|
||||
interpolators[row].update();
|
||||
}
|
||||
|
||||
closestDist = width*height; // abritrarily high
|
||||
|
||||
for (int row = 0; row < rowCount; row++) {
|
||||
String abbrev = dataTable.getRowName(row);
|
||||
float x = locationTable.getFloat(abbrev, 1);
|
||||
float y = locationTable.getFloat(abbrev, 2);
|
||||
drawData(x, y, abbrev);
|
||||
}
|
||||
|
||||
if (closestDist != width*height) {
|
||||
fill(0);
|
||||
textAlign(CENTER);
|
||||
text(closestText, closestTextX, closestTextY);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void drawData(float x, float y, String abbrev) {
|
||||
// Figure out what row this is
|
||||
int row = dataTable.getRowIndex(abbrev);
|
||||
// Get the current value
|
||||
float value = interpolators[row].value;
|
||||
|
||||
float radius = 0;
|
||||
if (value >= 0) {
|
||||
radius = map(value, 0, dataMax, 1.5, 15);
|
||||
fill(#333366); // blue
|
||||
} else {
|
||||
radius = map(value, 0, dataMin, 1.5, 15);
|
||||
fill(#ec5166); // red
|
||||
}
|
||||
ellipseMode(RADIUS);
|
||||
ellipse(x, y, radius, radius);
|
||||
|
||||
float d = dist(x, y, mouseX, mouseY);
|
||||
if ((d < radius + 2) && (d < closestDist)) {
|
||||
closestDist = d;
|
||||
String name = nameTable.getString(abbrev, 1);
|
||||
String val = nfp(interpolators[row].target, 0, 2);
|
||||
closestText = name + " " + val;
|
||||
closestTextX = x;
|
||||
closestTextY = y-radius-4;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void keyPressed() {
|
||||
if (key == ' ') {
|
||||
updateTable();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void updateTable() {
|
||||
for (int row = 0; row < rowCount; row++) {
|
||||
float newValue = random(dataMin, dataMax);
|
||||
interpolators[row].target(newValue);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user