Skip to content

Commit d749167

Browse files
Add files via upload
1 parent 0c329c2 commit d749167

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Fruit Data Visualization
2+
// The Coding Train / Daniel Shiffman
3+
// Processing Intro Series
4+
float[] fruitInventory = new float[5];
5+
String[] fruitNames = {"mango", "strawberry", "kiwi", "plum", "blueberry"};
6+
color[] colorArray = new int[]{#D9A407, #EE0E00, #3CB03E, #4B2473, #4188FF};
7+
8+
void setup() {
9+
size(640, 360);
10+
11+
for (int i = 0; i < fruitInventory.length; i++) {
12+
fruitInventory[i] = random(25, 145);
13+
}
14+
}
15+
16+
void mousePressed() {
17+
//randomize when mouse pressed
18+
for (int i = 0; i < fruitInventory.length; i++) {
19+
fruitInventory[i] = random(25, 145);
20+
}
21+
}
22+
23+
void draw() {
24+
background(240);
25+
strokeWeight(24);
26+
strokeCap(SQUARE); //Line has squared end, not rounded
27+
28+
textAlign(CENTER);
29+
textSize(24);
30+
31+
float sum = 0;
32+
for (int i = 0; i < fruitInventory.length; i++) {
33+
fill(colorArray[i]);
34+
stroke(colorArray[i]);
35+
float x = 100 + i * 100;
36+
//line height is number of fruit inventory
37+
line(x, height/2, x, height/2 - fruitInventory[i]);
38+
text(fruitNames[i], x, height/2 + 24); //fruit name labels
39+
40+
/*We declared the fruit inventory array as having floats
41+
which means the random values we assign will be decimal
42+
numbers, but if I want to display a whole number I can use
43+
int() to convert a float to an integer. See what happens
44+
when you remove it!*/
45+
text(int(fruitInventory[i]), x, height/2 - fruitInventory[i] - 24/2); //display number of each fruit
46+
47+
sum += fruitInventory[i]; //add the value of the inventory each time through the loop
48+
}
49+
50+
fill(0);
51+
text("Total Fruit Inventory: " + int(sum), width/2, height/2 + 100);
52+
53+
float average = sum/fruitInventory.length;
54+
text("Average Number of Fruits: " + int(average), width/2, height/2 + 150);
55+
}

0 commit comments

Comments
 (0)