วันจันทร์ที่ 10 พฤศจิกายน พ.ศ. 2557

try to load 2D array data from text file [ Then plot an appropriate graph ]

void setup () {
  size (500, 500);
  background (255);
  String Data [] = loadStrings ("http://m.uploadedit.com/b042/141569888757.txt");
  println ("row in data is "+(Data.length));
  String pick1 = " ";
  String [] collect = new String [Data.length];
  for (int i =0; i<Data.length; i++) {
    pick1 = Data[i];
    collect[i] = pick1;
  }
  Manage_String(collect);
}
/////////////////////////////////////////
void Manage_String (String [] a ) {
  int count = 0;
  String pick2 = "";
  String [][] data = new String [a.length][2];
  for (int i = 0; i<a.length; i++) {
    pick2 = a[i];
    if (pick2.charAt(i)!=' ') {
      count++;
    } else {
      break;
    }
  }
  for (int i = 0; i<a.length; i++) {
    pick2 = a[i];
    int x = pick2.indexOf("", count);
    int y = pick2.indexOf("", pick2.length());
    String colum1 = a[i].substring(0, x);
    String colum2 = a[i].substring(x+1, y);
    for (int j =0; j<data[0].length; j++) {
      data[i][0] = colum1;
      data[i][1] = colum2;
    }
  }
  int [][] convert = new int [a.length][2];
  for (int i = 0; i<data.length; i++) {
    for (int j =0; j<data[0].length; j++) {
      convert[i][j] = int (data[i][j]);
      print ("\t" +convert[i][j]);
    }
    println();
  }
  plot_graph(convert);
}

void plot_graph (int [][] b) {
  int y =100;
  int space = 50;
  line (b[0][1], y, b[0][1], height-y);
  line (b[0][1], height-y, width-b[0][1], height-y);
  fill(0);
  text ("X", b[0][1]-10, y-10);
  text ("y", (width-b[0][1])+10, (height-y)-10);
  for (int i =0; i<b.length; i++) {
    fill(204, 0, 0);
    rect ((b[i][1]+(space-40)), (height-y), 30, -((y)+b[i][0]));
    text((b[i][0]), (b[i][1]+(space-40)), (height-y)+20);
    space =space-5;
  }
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น