Help with a Bar Graph creation

edited November 2014 in How To...

Okay, I feel really bad for even asking this but I'm overwhelmed and I don't know where else to even ask.

I'm trying to make a bar graph and I watched so many tutorials, read so many documents but I can't seem to figure out how to make my bar graph come out the way I want it to. I have my data saved in excel but it needs to be a .txt file and it's ready but I get past the String[] and I'm lost.

This is honestly all I have and its pretty pathetic. I'm still new at this.

String[] dclass = ("dclass.txt");

void setup() size(500,500);

And then I'm stuck. All I want are three bars based on my arena and when you pass over one of them, the color of bar changes to correspond with the class it belongs to. I can usually figure it out when the data is put in int[] above the void setup() but this requires importing and then the lines of code I read get me confused and overwhelmed.

Here's the txt file the way it was seen in excel but converted.

      Class 1 Class 2 Class 3

Arena 1 5 8 11 Arena 2 19 17 30 Arena 3 11 11 11

Please, can someone help me get started? I'm kind of in a rush but at this point, I just want to understand how to get started and then understand what each point means afterwards.

Answers

  • edited November 2014 Answer ✓

    I wonder whether you had taken a look at Processing's reference page already?
    https://processing.org/reference/

    Particularly: https://processing.org/reference/loadStrings_.html + https://processing.org/reference/split_.html

    Also, if you got a ".csv" or a ".tsv" file, you can directly load them as a Table:
    https://processing.org/reference/loadTable_.html
    https://processing.org/reference/Table.html

  • I actually have and the reference page helps but I believe my issue is I don't understand what each line of code means. I had this issue with calculus functions and have been struggling the same here.

    I found the barGraph() function but I don't see how a .txt file could work. Additionally, I have tried a table, even by loading a different file just to test it out with no luck.

  • pls show your code and then ask a specific question

    // fixed distances to border
    int distX = 100;
    int distY = 250;
    
    // data  
    int b       = 21;
    int bb      = 12;
    int bbb     = 88;
    int bbbb    = 30;
    int bbbbb   = 40;
    //
    void setup () {
      size (390, 350);
      // frameRate(2); // slow down
    }
    //
    void draw () {
    
      background (0);
    
      //  red line 
      stroke(255, 2, 2);
    
      // vertical line 
      line (distX+10, 252, 
      distX+10+5*10, 252);
    
      stroke(0);
    
      //
      // vertical output 
      fill(255);
      rect (10+distX, distY, 10, -  b);
      rect (20+distX, distY, 10, - bb);
      rect (30+distX, distY, 10, - bbb);
      rect (40+distX, distY, 10, - bbbb);
      rect (50+distX, distY, 10, - bbbbb);
    }
    
  • your text file is confusing...

    shouldn't it have like same number of items each line...?

    Here's the txt file the way it was seen in excel but converted.
    1
    
    Class 1 Class 2 Class 3
    
    Arena 1 5 8 11 Arena 2 19 17 30 Arena 3 11 11 11
    
  • here

    //array maken met woorden
    String[] cat=new String[7];
    int[] correctAnswers=new int[7];
    PFont f;
    PFont font;
    
    size(900, 900);
    //lettertype en grootte
    font = createFont("Arial", 12);
    
    cat[0]="blijheid";
    cat[1]="angst";
    cat[2]="walging";
    cat[3]="verdriet";
    cat[4]="verassing";
    cat[5]="verachting";
    cat[6]="woede";
    
    correctAnswers[0]=3;
    correctAnswers[1]=9;
    correctAnswers[2]=0;
    correctAnswers[3]=7;
    correctAnswers[4]=3;
    correctAnswers[5]=2;
    correctAnswers[6]=12;
    
    //rectangle tekenen, eerst variabele aanmaken
    int rectangleY= 150;
    rectMode(CORNER);
    textFont(font, 30);
    text("Your Answers", width/2-120, 90);
    //keep track of the answers people give
    for (int i=1; i<8; i=i+1) {
      fill(236, 237, 218);
      rect(45, rectangleY, 150, 45);
      fill(255, 2, 2);
      rect(205, rectangleY, correctAnswers[i-1]*20, 45);
      text (correctAnswers[i-1], 205 + 10 + correctAnswers[i-1]*20, rectangleY+35);
      fill(0);
      text (cat[i-1], 50, rectangleY+35);
      rectangleY =rectangleY+45;
    }
    
  • or this where you can replace some lines to load a file

    final int indexForX=0;
    final int indexForY=1;
    //
    String loadDataOne[] 
    = {
      "1, 40", 
      "2, 55", 
      "3, 56", 
      "4, 39"
    };
    
    
    String myDataOne[][];
    float xPosDataOne;
    float yPosDataOne;
    int fieldNum = 2;
    //
    int margin = 100;
    float rectX = 1; 
    int xMin = 10;  
    int xMax = 200; 
    int yMin = 0; 
    int yMax = 150;  
    color c1 = color(226, 229, 204);
    // 
    void setup() {
      size(1200, 600);
      smooth();
      // loadDataOne = loadStrings("protwo.xls");
      myDataOne = new String[loadDataOne.length][fieldNum];
      for (int i=0; i < loadDataOne.length; i++) {
        myDataOne[i] = loadDataOne[i].split(",");
      } // for 
      for (int n=0; n < loadDataOne.length; n++) {
        myDataOne[n][indexForX]=trim(myDataOne[n][indexForX]);
        myDataOne[n][indexForY]=trim(myDataOne[n][indexForY]);
        print("Week " + myDataOne[n][indexForX] + ": " );
        println(myDataOne[n][indexForY]);
      } // for
    } // func 
    
    void draw() {
      background(40);
      noStroke();
      for (int n=0; n < loadDataOne.length; n++) {
    
        xPosDataOne = map(int(myDataOne[n][indexForX]), xMin, xMax, 0+margin, width-margin);   
        yPosDataOne = map(int(myDataOne[n][indexForY]), yMin, yMax, 0, height-(margin+50));
    
        fill(c1);
        rectMode(CORNER);
        rect(xPosDataOne, height-margin, rectX, -1*yPosDataOne); // draw
      } // for
    } // func 
    //
    
Sign In or Register to comment.