We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello there! I'm very new to Processing. I need some help with my graph issue. It's a bar graph that I am making for a project and somehow I'm having a hard time fixing the bars' height (it's going pass the window whenever I run the code). Also one bar graph is not showing anything other than one bar. Can anyone help me with this? Please and thank you!
Here's my code: int[] v1; int[] v2; int[] v3;
float x;
float y;
float delta;
float w;
int value = 0;
void setup(){
size(800, 800);
smooth();
String[] lines = loadStrings("data.txt");
v1 = new int[lines.length];
v2 = new int[lines.length];
v3 = new int[lines.length];
for(int i=0; i<lines.length; i++) {
String[] pieces = split(lines[i], ",");
v1[1] = int(pieces[0]);
v2[i] = int(pieces[1]);
v3[i] = int(pieces[2]);
}
println("Data Loaded: "+v1.length+" entries. ");
delta = width*0.8/v1.length;
w = delta*0.8;
}
void draw() {
background(255);
fill(0);
if (value == 0) {
myGraphFunction(v1, "Software Revenue");
}
else if (value == 1){
myGraphFunction(v2,"Accessories Revenue");
}
else {
myGraphFunction(v3,"Hardware Revenue");
}
}
void myGraphFunction (int[] data, String header) {
background(255);
fill(0);
text(header, width/2-50, 50);
x = width*0.1;
y = height*0.95;
for (int i=0; i<data.length; i++) {
float h = map(data[i], 0, max(data)+10, 0, height);
fill(#DB2C2C);
rect(x, y-h, w, h);
text(data[i],x,y-h-10);
x = x + delta;
}
}
void mouseClicked() {
value++;
if(value > 2) {
value = 0;
}
}
Answers
Format your code. Press the gear icon to edit your post, then select your code and press ctrl+o. Leave an empty line above and below your code.
Kf
gotcha
Also, here are the coordinates:
Typo in line 24.
Kf
Hahahaha, serious! That was the issue. well what about some of the bars going pass the window's border?
For alternative plotting, check the graphica library.
For that last post, do this in the myGraphFunction():
float h = map(data[i], 0, max(data)*1.20, 0, height);
The factor of 1.20 is saying that it should add 20% of the maximum to the upper range when doing the mapping.
Kf
Thank you for the help!