• fullscreen
  • Table.pde
  • opdrachth2.pde
  • class Table {
      String[][] data;
      int rowCount;
       
       
      Table() {
        data = new String[10][10];
      }
     
       
      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);
      }
       
       
      // Write this table as a TSV file
      void write(PrintWriter writer) {
        for (int i = 0; i < rowCount; i++) {
          for (int j = 0; j < data[i].length; j++) {
            if (j != 0) {
              writer.print(TAB);
            }
            if (data[i][j] != null) {
              writer.print(data[i][j]);
            }
          }
          writer.println();
        }
        writer.flush();
      }
    }
    
     Table aTable;
     Table nameTable;//test
    
     float dataMin, dataMax;
     PImage mapImage;// achtergrond
    int rowCount;
    
    void setup() {
      size (640, 250);
      //acquire the data
      aTable = new Table("zetelverdeling.csv");
      nameTable = new Table("names.csv");
      rowCount = aTable.getRowCount();
      
      PFont font = loadFont("Verdana-12.vlw");
      textFont(font);
      mapImage = loadImage("kamer.jpg");
      //dataMin = 0;
      //dataMax = data.getTableMax();
    }
     
    void draw() {
      background(255);
      smooth();
      noStroke();
      //determine bar width
      float barWidth = width/rowCount;
        image(mapImage, 0,0);// achtergrondplaatje 
       
      //find max and min value (from the origin);
      float maxValue = 0;
      float minValue = 0; //minValue is always 0 or lower
       
      for (int row = 0; row < rowCount; row++) {
    
        maxValue = max(maxValue, aTable.getFloat(row, 0));
        minValue = min(minValue, aTable.getFloat(row, 0));
      }
       
      //set the origin y
      float originY = height * maxValue / (maxValue - minValue);
       
      //determine scaleY
      float scaleY = height / (maxValue - minValue);
       
      //draw the chart
      //draw the chart
      for (int row = 0; row < rowCount; row++) {
        float y = aTable.getFloat(row, 0);
        if (y>maxValue*0.99) {
          fill(#4f67cb);//vvd
        } else if (y>maxValue*0.8) {
         fill(#ee0202);//pvda
        } else if (y>maxValue*0.7) {
         fill(#2e9ddb);//pvv
        } else if (y>maxValue*0.6) {
         fill(#0ac27b);//cda
        } else if (y>maxValue*0.4) {
         fill(#de1c6e);//sp
        } else if (y>maxValue*0.3) {
         fill(#009753);//GL
        }else if (y>maxValue*0.2) {
         fill(#24c1cb);
        }else if (y>maxValue*0.1) {
         fill(#24c1cb);//GU
        } else {
           fill(#ebc30a);
        }
        
        rect(row * barWidth+2, originY, barWidth-4, -y*scaleY);
        fill(#000000);
        text ("VVD", 10, 10);
        text ("PVDA", 80, 30);
        text ("PVV", 150, 80);
        text ("CDA", 210, 100);
        text ("SP", 280, 150);
        text ("GL", 340, 190);
        text ("D66", 400, 190);
        text ("CU", 465, 225);
        text ("PVVD", 525, 245);
        text ("SGP", 585, 245);
        
        
        //en toen wist ik het even niet
        //for (int lul = 0; row < rowCount; row++) {
          //text(nameTable.getString(0,0), 10, 10);
        //}
        //drawDataPoints(0);
      }
    }
    //void drawDataPoints(int col) {
    //  int rowCount = data.getRowCount();
    //  for (int row = 0; row < rowCount; row++) {
    //    if (data.isValid(row,col)) {
    //      float value = data.getFloat(row, col);
    //      float x = map(years[row], yearMin, yearMax, plotX1, plotX2);
    //      float y = map(value, dataMin, dataMax, plotY2, plotY1);
    //      point(x, y);
    //    }
    //  }
    //}
      
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Timo van Tilburg

    kamer opdracht 2

    Add to Faves Me Likey@!
    You must login/register to add this sketch to your favorites.

    kamer opdracht 2

    You need to login/register to comment.