How to Fix my graph.

edited February 2017 in Questions about Code

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

  • Also, here are the coordinates:

    257,158,184          
    205,151,153
    403,129,313
    221,161,202
    253,113,190
    581,179,313
    385,160,270
    1170,277,1120
    1350,641,1240
    213,235,157
    304,246,292
    426,285,253
    204,163,142
    242,149,138
    322,121,181
    210,117,141
    274,107,178
    453,131,234
    506,121,215
    955,260,724
    
  • Typo in line 24.

    Kf

  • Hahahaha, serious! That was the issue. well what about some of the bars going pass the window's border?

  • Answer ✓

    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!

Sign In or Register to comment.