Guest User

Untitled

a guest
Jul 7th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.42 KB | None | 0 0
  1.  
  2.  
  3. /**
  4. * An initial attempt at visualizing Admissions/Registration data
  5. * This will connect to the DB, get some data about gender vs. enrolment and visualize
  6. **/
  7.  
  8. // ADMIT_STATUS,START_TERM,FED_COHORT_GROUP,GENDER,STATE,ETHNIC_DESC,DENOM_DESC,BIRTH_DATE,CAREER_GOAL1,CAREER_GOAL2,CAREER_GOAL3,INTEREST1,INTEREST2,INTEREST3,SAT_BEST,ACT_BEST,STUDENT_TYPE,HS_COUNTY_DESC,HS_RANK_NUMERATOR,HS_RANK_DENOMINATOR,HS_EXT_CREDITS,HS_EXT_GPA
  9.  
  10. import de.bezier.data.sql.*;
  11. import au.com.bytecode.opencsv.CSVReader;
  12. import java.util.ArrayList;
  13. import java.sql.*;
  14.  
  15.  
  16. ArrayList startTerms;
  17. ArrayList genderCounts;
  18. PFont smallFont;
  19. PFont bigFont;
  20. Connection conn;
  21. int bar_width; // width of each bar
  22. int x_buff=50;
  23. int y_buff=50; // x and y margins for the plot
  24.  
  25. // String semesters[] = {"JA", "SP", "FA", "S1", "S2", "S3"};
  26.  
  27. void setup() {
  28. size(1100, 700);
  29. // noStroke();
  30. smallFont = loadFont("CartoGothicStd-Bold-14.vlw");
  31. bigFont = loadFont("CartoGothicStd-Bold-18.vlw");
  32. smooth();
  33. frameRate(10);
  34.  
  35. connectDB();
  36. getSomeData();
  37. bar_width = (width-2*x_buff)/(startTerms.size());
  38. }
  39.  
  40.  
  41. void draw() {
  42. background(200);
  43. textAlign(LEFT);
  44. textFont(bigFont, 18);
  45. fill(0);
  46. text("Admissions Data Viz", 20, 30);
  47. text("____________________", 20, 40);
  48.  
  49. // drawHistograms();
  50. drawGraphs();
  51. }
  52.  
  53. void drawGraphs() {
  54. textFont(smallFont, 7);
  55.  
  56. beginShape();
  57. stroke(100, 200, 100);
  58. strokeWeight(2);
  59. for (int i = 0; i < startTerms.size(); i++) {
  60. float x1 = (i*bar_width - bar_width/2) + x_buff;
  61. float y1 = height - 2*getMaleHeight(i) - 2*y_buff;
  62. String x = (String)startTerms.get(i);
  63. textAlign(CENTER);
  64. fill(0);
  65. text(x, x1, height-y_buff);
  66. noFill();
  67. vertex(x1, y1);
  68. if(i != 0 || i == startTerms.size()-1)
  69. vertex(x1, y1);
  70. }
  71. endShape();
  72. strokeWeight(0.5);
  73. stroke(100, 100);
  74. fill(100, 200, 100);
  75. for (int i = 0; i < startTerms.size(); i++) {
  76. float x1 = (i*bar_width - bar_width/2) + x_buff;
  77. float y1 = height - 2*getMaleHeight(i) - 2*y_buff;
  78. fill(100, 200, 100, 255);
  79. ellipse(x1, y1, 8, 8);
  80. line(x1, height-2*y_buff, x1, 2*y_buff);
  81. textFont(smallFont, 12);
  82. fill(230);
  83. int[] a = (int[]) genderCounts.get(i);
  84. if(dist(x1, y1, mouseX, mouseY) < 5)
  85. text("Male: "+a[0], x1, y1+20);
  86. }
  87.  
  88. beginShape();
  89. stroke(200, 100, 100);
  90. strokeWeight(2);
  91. noFill();
  92. for (int i = 0; i < startTerms.size(); i++) {
  93. float x1 = (i*bar_width - bar_width/2) + x_buff;
  94. float y1 = height - 2*getFemaleHeight(i) - 2*y_buff;
  95. vertex(x1, y1);
  96. if(i != 0 || i == startTerms.size()-1)
  97. vertex(x1, y1);
  98. }
  99. endShape();
  100.  
  101.  
  102. strokeWeight(0.5);
  103.  
  104. for (int i = 0; i < startTerms.size(); i++) {
  105. float x1 = (i*bar_width - bar_width/2) + x_buff;
  106. float y1 = height - 2*getFemaleHeight(i) - 2*y_buff;
  107. fill(200, 100, 100);
  108. ellipse(x1, y1, 8, 8);
  109. textFont(smallFont, 12);
  110. fill(170);
  111. int[] a = (int[]) genderCounts.get(i);
  112. if(dist(x1, y1, mouseX, mouseY) < 5)
  113. text("Female: "+a[1], x1, y1-20);
  114. }
  115. }
  116.  
  117. /**
  118. * Draws the data as histograms
  119. */
  120. void drawHistograms() {
  121. for (int i = 0; i < startTerms.size(); i++) {
  122. float maleHeight = getMaleHeight(i);
  123. float femaleHeight = getFemaleHeight(i);
  124.  
  125. fill(150);
  126. rect(i*bar_width, height-maleHeight, bar_width, maleHeight);
  127.  
  128. fill(210);
  129. rect(i*bar_width, 70, bar_width, femaleHeight);
  130. rotate(3*PI/2.0);
  131. fill(40);
  132. textFont(smallFont, 12);
  133. String x = (String) startTerms.get(i);
  134. int a[] = (int[]) genderCounts.get(i);
  135. text(x, -height/2, i*bar_width+bar_width/2);
  136. rotate(PI/2.0);
  137.  
  138. if(mouseX>i*bar_width && mouseX<(i*bar_width+bar_width)) {
  139. stroke(40, 70);
  140. line(mouseX, 0, mouseX, height);
  141. // && mouseY>(height-maleHeight-femaleHeight
  142. String printLabelStr = "Semester: " + x + "\nM: " + a[0] + "\nF: " + a[1];
  143. textFont(smallFont, 14);
  144. text(printLabelStr, 20, width/2);
  145. }
  146. }
  147. }
  148.  
  149. float getMaleHeight(int i) {
  150. int[] a = (int[]) genderCounts.get(i);
  151. return map(a[0], 0, 60000, 0, (height)/2);
  152. }
  153.  
  154. float getFemaleHeight(int i) {
  155. int[] a = (int[]) genderCounts.get(i);
  156. return map(a[1], 0, 60000, 0, (height)/2);
  157. }
  158.  
  159.  
  160. void getSomeData() {
  161. try {
  162. startTerms = new ArrayList();
  163. genderCounts = new ArrayList();
  164. Statement stmt = conn.createStatement();
  165. // only get Fall enrollments
  166. ResultSet rs = stmt.executeQuery("SELECT DISTINCT START_TERM FROM admissions_main WHERE START_TERM LIKE '%FA'" );
  167. while (rs.next()) {
  168. String x = rs.getString(1);
  169. startTerms.add(x);
  170. }
  171. Collections.sort(startTerms, new termsCompare());
  172.  
  173. for (int i = 0; i < startTerms.size(); i++) {
  174. String x = (String)startTerms.get(i);
  175. Statement stmt_g = conn.createStatement();
  176. ResultSet rs_g = stmt_g.executeQuery("SELECT COUNT(GENDER) FROM admissions_main WHERE START_TERM='" +x+ "' AND GENDER='M'" );
  177. rs_g.next();
  178. int m = rs_g.getInt(1);
  179. rs_g = stmt_g.executeQuery("SELECT COUNT(GENDER) FROM admissions_main WHERE START_TERM='" +x+ "' AND GENDER='F'" );
  180. rs_g.next();
  181. int f = rs_g.getInt(1);
  182. int[] a = {m, f};
  183. // println(x+ ":males " + m + ", females: " +f);
  184.  
  185. genderCounts.add(a);
  186. stmt_g.close();
  187. rs_g.close();
  188. }
  189.  
  190. } catch (Exception e) {
  191. e.printStackTrace();
  192. }
  193. }
  194.  
  195. /**
  196. void testDB2() {
  197. try {
  198. genderCounts = new ArrayList();
  199.  
  200. boolean done = false;
  201. int year_initls = 90;
  202. Statement stmt;
  203. ResultSet rs_m;
  204. while (!done) {
  205. for (int i = 0; i < semesters.length; i++) {
  206. String start_term = year_initls+"/"+semesters[i];
  207. stmt = conn.createStatement();
  208. rs_m = stmt.executeQuery("SELECT COUNT(START_TERM) FROM admissions_main WHERE GENDER='M' AND START_TERM='" + start_term +"'" );
  209. rs_m.next();
  210. int m = rs_m.getInt(1);
  211. stmt.close();
  212. rs_m.close();
  213.  
  214. stmt = conn.createStatement();
  215. rs_m = stmt.executeQuery("SELECT COUNT(START_TERM) FROM admissions_main WHERE GENDER='F' AND START_TERM='" + start_term +"'" );
  216. rs_m.next();
  217. int f = rs_m.getInt(1);
  218. stmt.close();
  219. rs_m.close();
  220. println("start_term: " +start_term+ "-> f:" +f+ ", m:" +m);
  221. }
  222.  
  223. year_initls++;
  224. if(year_initls==100)
  225. year_initls = 00;
  226. else if (year_initls==10)
  227. done=true;
  228. }
  229. } catch (Exception e) {
  230. e.printStackTrace();
  231. }
  232. }
  233.  
  234. */
  235.  
  236. // helper methods
  237. /**
  238. * Connect to the database
  239. */
  240. void connectDB() {
  241. conn = null;
  242. try
  243. {
  244. String userName = "root";
  245. String password = "raheelms";
  246. String url = "jdbc:mysql://localhost:8889/admissionsviz";
  247. Class.forName ("com.mysql.jdbc.Driver").newInstance ();
  248. conn = DriverManager.getConnection (url, userName, password);
  249. System.out.println ("Database connection established");
  250. }
  251. catch (Exception e)
  252. {
  253. System.err.println ("Cannot connect to database server");
  254. e.printStackTrace();
  255. }
  256.  
  257. }
  258.  
  259. /**
  260. * For loading the CSV file. Abandoning this since will need data in DB for ease
  261. * of querying
  262. */
  263.  
  264. /*
  265. void loadCSV() {
  266. int linenum=1;
  267. try {
  268. CSVReader reader = new CSVReader(new FileReader("/Users/rahmad/Documents/Programming/Processing/AdmissionsViz/data/data.txt"));
  269. String [] nextLine;
  270. students = new ArrayList();
  271. while ((nextLine = reader.readNext()) != null) {
  272. try {
  273. Student s = new Student();
  274. s.admit_status = nextLine[0];
  275. String[] stterm = split(nextLine[1], '/');
  276. s.startterm_year = int(stterm[0]);
  277. s.startterm_semester = stterm[1];
  278. s.gender = nextLine[3];
  279. s.state = nextLine[4];
  280. s.ethnic_desc = nextLine[5];
  281. s.denom_desc = nextLine[6];
  282. s.birth_date = nextLine[7];
  283. s.hs_ext_gpa = nextLine[21];
  284. //System.out.println("gpa: " + s.admit_status);
  285. students.add(s);
  286. linenum++;
  287. } catch (Exception e) {
  288. //System.out.println("got an exception while creating student, moving on");
  289. }
  290.  
  291. }
  292. } catch (Exception e) {
  293. System.out.println("Exception while loading CSV: at line no. " + linenum );
  294. e.printStackTrace();
  295. }
  296. System.out.println("loaded " + students.size() + " students");
  297. for (int i = 0; i < students.size(); i++) {
  298. Student s = (Student) students.get(i);
  299. println("student " + i + "'s gender: " + s.gender);
  300. }
  301. }
  302. */
  303.  
  304. /**
  305. * A class to sort the terms (stored in the form 99/FA, 00/SP)
  306. */
  307. private class termsCompare implements java.util.Comparator {
  308. public int compare(Object a, Object b) {
  309. String[] aSplit = ((String) a).split("/");
  310. String[] bSplit = ((String) b).split("/");
  311. int aYear = Integer.parseInt(aSplit[0]);
  312. String aSem = aSplit[1];
  313. int bYear = Integer.parseInt(bSplit[0]);
  314. String bSem = bSplit[1];
  315.  
  316. int sdif = 0;
  317. if(aYear > 80 && bYear <= 30) // i.e., the earliest year in 20th century is 1980, and latest year in 21st is 2030
  318. sdif = -1;
  319. else if (bYear > 80 && aYear <= 20)
  320. sdif = 1;
  321. else {
  322. if(aYear < bYear)
  323. sdif = -1;
  324. else if(aYear > bYear)
  325. sdif = 1;
  326. else {
  327. sdif = compareSemesters(aSem, bSem);
  328. }
  329. }
  330. return sdif;
  331. }
  332.  
  333. private int compareSemesters(String s1, String s2) {
  334. if(s1.equals(s2))
  335. return 0;
  336. if(s1.equals("JA"))
  337. return 1;
  338. else if(s1.equals("SP")) {
  339. if(s2.equals("JA"))
  340. return -1;
  341. else
  342. return -1;
  343. } else if(s1.equals("S1")) {
  344. if(s2.equals("JA") || s2.equals("SP"))
  345. return -1;
  346. else
  347. return 1;
  348. } else if(s1.equals("S2")) {
  349. if(s2.equals("JA") || s2.equals("SP") || s2.equals("S1"))
  350. return -1;
  351. else
  352. return 1;
  353. } else if(s1.equals("S3")) {
  354. if(s2.equals("JA") || s2.equals("SP") || s2.equals("S1") || s2.equals("S2"))
  355. return -1;
  356. else
  357. return 1;
  358. } else if(s1.equals("FA")) {
  359. return -1;
  360. }
  361.  
  362. return 0;
  363. }
  364.  
  365. }
Add Comment
Please, Sign In to add comment